0

我正在使用rails 4.2,并且我有一个表单试图为每个复选框的collection_check_boxes插入一个响应模型。当我这样做时,我没有任何错误和其中一个属性,adjective_id,它不会被保存。不能使用has_many将ID数组插入到连接表中:通过关联

在这里你可以看到帕拉姆的模样时,我选择4 58复选框:

"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 INTO "responses" ("relation", "survey_id", "adjective_id", "created_at", "updated_at") 
  • 我怎样才能得到这个插入正确?
  • 有什么办法可以在响应表中为params数组中的每个“adjective_id”使用相同的“关系”,“survey_id”,“created_at”和“updated_at”做出1行?

模型

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 %> 

我希望这是一个清晰的问题。我在轨道上用红宝石小白,所以我会很乐意回答任何问题,以澄清我的问题。

谢谢!

+0

你见过[this](https://stackoverflow.com/questions/28049879/strong-parameters-unpermitted-parameters-tags-in-has-many-through-relation)问题及其答案吗? – pdoherty926 2015-02-11 04:40:34

+0

好吧,我刚刚阅读该帖子,感谢您的答案。所以在我的情况下,param:adjective_id是允许的。现在,唯一的方法来分配它正在做我的数组拆分的值,就像在那个答案?为什么rails不能识别adjective_id是对象响应的属性? – 2015-02-11 15:38:59

回答

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

如果有人知道一个更好的方式来得到这个,请让我知道。谢谢

+0

当有多个'adjective_id'时,'@ response'是不是不正确?您可能需要考虑使用:'@response = adjective_ids.map {| adj_id | @ survey.responses.create(response_params adj_id)}'。 – pdoherty926 2015-02-11 19:54:48

+0

是的你是正确的,我会更新我的答案。感谢您的更正 – 2015-02-12 00:19:33

相关问题