我正在尝试让rails填充inbetween表。中间表是: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
表单(使用集合选择(目的是选择与任务绑定的属性的空间并填入inbetween表中)
<%= 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 %>
所以我需要帮助确保在选择房间时填充我的inbetween表。当f。或者t。在collection_select上加前缀,rails会抛出错误。 我非常困惑。非常感谢帮助,如果需要任何其他信息,请告诉我。
添加f时收到此错误。或者t。
未定义的方法`merge'for:number:Symbol
显示并在没有f的情况下正常工作。或者t。但不填充表格。