1

レールに中間テーブルを設定させようとしています。中間テーブルは次のとおりです。 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. またはt。が collection_select の前に付いていると、Rails はエラーをスローします。私は非常に混乱しています。助けていただければ幸いです。追加情報が必要な場合はお知らせください。

f を追加すると、このエラーが発生します。またはt。

:number:Symbol の未定義のメソッド `merge'

f がなくても正常に表示および動作します。またはt。ただし、テーブルにはデータを入力しません。

4

0 に答える 0