我正在尝试让导轨填充中间表。中间表是: room_task 我使用的表格是新的任务表格,它是嵌套的。
这是模型:
class Property < ActiveRecord::Base (property is parent nest)
attr_accessible :brand_id, :name, :user_property_code, :total_rooms, :property_addresses_attributes
belongs_to :brand
has_many :rooms
has_many :nonrooms
end
class Room < ActiveRecord::Base (room)
attr_accessible :number, :out_of_order, :property_id, :room_type_id, :taskable_id
belongs_to :room_type
belongs_to :property
has_many :room_tasks
has_many :tasks, through: :room_tasks
accepts_nested_attributes_for :tasks
accepts_nested_attributes_for :room_tasks
#add_index :room, :property_id
end
class Room < ActiveRecord::Base (task)
attr_accessible :number, :out_of_order, :property_id, :room_type_id, :taskable_id
belongs_to :room_type
belongs_to :property
has_many :room_tasks
has_many :tasks, through: :room_tasks
accepts_nested_attributes_for :tasks
accepts_nested_attributes_for :room_tasks
#add_index :room, :property_id
end
class RoomTask < ActiveRecord::Base (in between table)
attr_accessible :room_id, :task_id
has_one :room
has_one :task
belongs_to :room # foreign key - room id
belongs_to :task # foreign key - task id
end
控制器(任务 - 我想在其中创建新任务以及中间表中所需的关系)
class TasksController < ApplicationController
before_filter :find_property
def find_property
@property = Property.find(params[:property_id])
@room = @property.rooms
end
# GET /tasks
# GET /tasks.json
def index
@tasks = Task.all
respond_to do |format|
format.html # index.html.erb
format.json { render json: @tasks }
end
end
# GET /tasks/1
# GET /tasks/1.json
def show
@task = Task.find(params[:id])
respond_to do |format|
format.html # show.html.erb
format.json { render json: @task }
end
end
# GET /tasks/new
# GET /tasks/new.json
def new
@task = Task.new
respond_to do |format|
format.html # new.html.erb
format.json { render json: @task }
end
end
# GET /tasks/1/edit
def edit
@task = Task.find(params[:id])
end
# POST /tasks
# POST /tasks.json
def create
@task = Task.new(params[:task])
@task.time_assigned = Time.now
respond_to do |format|
if @task.save
format.html { redirect_to property_tasks_path(@property), notice: 'Task was successfully created.' }
format.json { render json: @task, status: :created, location: @task }
else
format.html { render action: "new" }
format.json { render json: @task.errors, status: :unprocessable_entity }
end
end
end
# PUT /tasks/1
# PUT /tasks/1.json
def update
@task = Task.find(params[:id])
respond_to do |format|
if @task.update_attributes(params[:task])
format.html { redirect_to @task, notice: 'Task was successfully updated.' }
format.json { head :no_content }
else
format.html { render action: "edit" }
format.json { render json: @task.errors, status: :unprocessable_entity }
end
end
end
# DELETE /tasks/1
# DELETE /tasks/1.json
def destroy
@task = Task.find(params[:id])
@task.destroy
respond_to do |format|
format.html { redirect_to tasks_url }
format.json { head :no_content }
end
end
end
表单(使用集合选择(目的是选择任务绑定到的属性的房间并填充到中间表中)
<%= form_for([@property, @task]) do |f| %>
<% if @task.errors.any? %>
<div id="error_explanation">
<h2><%= pluralize(@task.errors.count, "error") %> prohibited this task from being saved:</h2>
<ul>
<% @task.errors.full_messages.each do |msg| %>
<li><%= msg %></li>
<% end %>
</ul>
</div>
<% end %>
<div>
<%= fields_for(:room_task) do |t| %>
<%= collection_select(:room_task, :room_id, @room, :id, :number) %> #without t. works but does not assign
<% end %>
<%= @room %> #debug collection pull - good
<div class="actions">
<%= f.submit %>
</div>
<% end %>
因此,我需要帮助确保在选择房间时填充我的中间表。当 f。或吨。以 collection_select 为前缀,rails 会抛出错误。我非常困惑。非常感谢您的帮助,如果需要任何其他信息,请告诉我。
添加 f 时收到此错误。或吨。
:number:Symbol 的未定义方法“合并”
在没有 f 的情况下显示和工作正常。或吨。但不填充表格。