无法使用has_many:通过关联将id的数组插入到连接表中

时间:2015-02-11 03:20:11

标签: ruby-on-rails ruby-on-rails-4 strong-parameters jointable

我正在使用rails 4.2,我有一个表单,它试图在collect_check_boxes的每个复选框中插入Response模型。当我这样做时,我没有任何错误和其中一个属性,adjective_id,它没有被保存。

在这里,您可以看到当我选择58个复选框中的4个时,参数的样子如何:

"response"=>{"relation"=>"family", "adjective_id"=>["2", "14", "16", "28", ""]}, "commit"=>"Answer", "survey_sruid"=>"d5e7b675370614d1331f"}

此处为插入内容:

INSERT INTO "responses" ("relation", "survey_id", "created_at", "updated_at") VALUES ($1, $2, $3, $4) RETURNING "id"  [["relation", "3"], ["survey_id", 1], ["created_at", "2015-02-11 02:39:41.409276"], ["updated_at", "2015-02-11 02:39:41.409276"]]

我需要将adjective_id放入insert语句中,如下所示:

INSERT INTO "responses" ("relation", "survey_id", "adjective_id", "created_at", "updated_at")
  • 如何才能正确插入?
  • 有任何方法可以在Responses表中使用相同的"关系"," survey_id"," created_at"和" updated_at"对于每个" adjective_id"在params数组?

模型

class Survey < ActiveRecord::Base
 has_many :responses
 has_many :adjectives, through: :responses
 belongs_to :nruser
end

class Response < ActiveRecord::Base
 belongs_to :survey
 belongs_to :adjective
end

class Adjective < ActiveRecord::Base
 has_many :responses
 has_many :surveys, through: :responses
end

Schema.rb: here

控制器

class ResponsesController < ApplicationController

def new
    @survey = Survey.find_by(sruid: params[:survey_sruid])
    @response = @survey.responses.build
end

def create
    @survey = Survey.find_by(sruid: params[:survey_sruid])
    @response = @survey.responses.create(response_params)
    redirect_to root_path
end

private

    def response_params
        params.require(:response).permit(:relation, { :adjective_id => [] })
    end
end

路线

Rails.application.routes.draw do

 root                'static_pages#home'
 resources :surveys, only: [:new, :create, :show, :destroy]
 resources :surveys, param: :sruid, only: [:create] do
    resources :responses, only: [:new, :create]  
 end
 resources :adjectives, only: [:index]
end

表格

<%= form_for @response, url: survey_responses_path do |r| %>
    <aside class="col-md-3">
        <%= r.label :relation, @survey.nruser.name.titleize + " es un:" %>
        <p><%= r.select :relation, options_for_select([['familiar', 3], ['amigo', 2], ['colega', 1], ['conocido',0]]) %></p>
        </br>
        <%= r.submit "Responder YA!", class: "btn btn-success" %>
    </aside>

    <aside class="col-md-9">
        <div class="btn-group" data-toggle="buttons">
            <%= r.collection_check_boxes :adjective_id, Adjective.all, :id, :adjective do |b| 
                    b.label(class: 'btn btn-primary') {b.check_box + b.text}
            end %>
        </div>
    </aside>
<% end %>

我希望这是一个明确的问题。我是铁杆上的红宝石的菜鸟,所以我很感激任何回答来澄清我的问题。

谢谢!

1 个答案:

答案 0 :(得分:0)

我不知道这是否是最好的答案,但现在是我唯一的答案。

    def create

     @survey = Survey.find_by(sruid: params[:survey_sruid])

     #Because collection_check_boxes adds a blank value at the end of the array.
     adjectives_ids = params[:response][:adjective_id].reject! { |c| c.empty? }

     adjectives_ids.each do |adj_id|
         @response = adjectives_ids.map { |adj_id| @survey.responses.create(response_params adj_id)
     end

     redirect_to root_path
    end

 private

    def response_params(adj_id)
        params.require(:response).permit(:relation).merge(adjective_id: adj_id)
    end

然后我得到了我正在寻找的结果: http://imgur.com/vXnnwdb

如果有人知道更好的方法,请告诉我。感谢

相关问题