Rails 4在表单提交上添加属性

时间:2014-04-23 14:18:13

标签: ruby-on-rails ruby-on-rails-4

我有一个表单,我想与当前用户的user_id一起提交。我是强大的障碍的新手。我试过这个,但user_id不是POSTing,也没有任何错误。

在Rails 3中我会这样做:

params[:question][:user_id] = current_user.id
@question= Question.new(params[:question])

我希望将current_user.id添加到新Question.user_id

下的提交中
def new_mc
@question = Question.new(user_params)
4.times { @question.answers.build }
end

def user_params
  params.
    permit(:user_id).
    merge(user_id: current_user.id)
end

我想放require(:question).,但它是零。这是日志:

SQL (2.7ms)  INSERT INTO "questions" ("active", "category", "content", "created_at", "product_id", "question_type", "updated_at") VALU
ES (?, ?, ?, ?, ?, ?, ?)  [["active", false], ["category", "ip_voice"], ["content", "what is going on"], ["created_at", Wed, 23 Apr 2014
14:12:07 UTC +00:00], ["product_id", 1], ["question_type", "MC"], ["updated_at", Wed, 23 Apr 2014 14:12:07 UTC +00:00]]
SQL (0.2ms)  INSERT INTO "answers" ("content", "correct", "created_at", "question_id", "updated_at") VALUES (?, ?, ?, ?, ?)  [["conten
t", "this "], ["correct", true], ["created_at", Wed, 23 Apr 2014 14:12:07 UTC +00:00], ["question_id", 25], ["updated_at", Wed, 23 Apr 2
014 14:12:07 UTC +00:00]]

所以你可以看到没有尝试INSERT user_id

控制器操作是上面的new_mc,Form:

<h1>New Multiple Choice Question</h1>

<%= form_for @question, url: new_mc_question_path(@question) do |f| %>

<%= render 'shared/error_questions' %>

<%= f.label :content, "Question" %><br>
<%= f.text_field :content, class: "input-lg" %>

<%= f.label :category %><br>
<%= f.select :category, [ ["IP Voice Telephony", "ip_voice"], ["IP Video Surveillance", "ip_video_surveillance"], ["IP Video Telephony", "ip_video_telephony"], ["Enterprise Gateways", "enterprise_gateways"], ["Consumer ATAs", "consumer_atas"], ["IP PBX", "ip_pbx"] ], {prompt: "Select Category"}, class: "input-lg" %>

<%= f.label :product_id %><br>
<%= f.collection_select :product_id, Product.all, :id, :name, {prompt: "Select a product"}, {class: "form-control input-lg"} %>

<%= f.label :active %><br>
<%= f.check_box :active %>

<%= f.select :question_type, [["Multiple Choice", "MC"]], {class: "form-control input-lg"}, style: "visibility: hidden" %>

<h1>Answers</h1>
  <%= f.fields_for :answers do |builder| %>

<%= render 'four_answers', :f => builder %>

  <% end %>
            
    
<script>
$(document).ready(function(){
    $("#question_active").bootstrapSwitch('onText', 'Active');
    $("#question_active").bootstrapSwitch('offText', 'Off');
    $("#question_active").bootstrapSwitch('size', 'large');
});
</script>

精确解决方案:

def create
@question = Question.new(question_params)

def question_params
  params.require(:question).permit(:content, :question_type, :category, :product_id, :active, :user_id, answers_attributes: [ :content, :correct, :question_id ] ).
  merge user_id: current_user.id
end 

2 个答案:

答案 0 :(得分:2)

以下是我通常会在控制器中添加额外参数的方法。

class QuestionsController

  # NOTE: you don't need to use strong_parameters in the 'new' action because no
  # values have been submitted by the form yet
  def new
    @question = Question.new
    4.times { @question.answers.build }
  end

  # the 'create' action is where strong_parameters are needed
  def create
    @question = Question.new(question_params_with_user_id)
    if @question.save
      # ...
    else
      # ...
    end
  end

  protected

  def question_parameters
    params.require(:question).permit(<put your acceptable form attributes here>)
  end

  def question_parameters_with_user_id
    question_parameters.merge user_id: current_user.id
  end

end

答案 1 :(得分:1)

在用户和问题之间添加关联。如果已经有一个并且它是has_many关联,那么您应该可以执行以下操作

class User < ActiveRecord::Base
  has_many :questions
end

#controller
def new
  @question = current_user.questions.build(question_params)
  4.times { @question.answers.build }
end

def question_params
  params.require(:question).permit(<insert question attributes here>)
end

注意:您似乎在strong_parameters操作中使用了new。在这里使用strong_parameters没有意义。它通常仅用于createupdate操作。

另一种选择,虽然我不喜欢它首先将current_user id合并到params[:question]

# controller
before_action :merge_current_user_id, only: [:create, :update]

def create
  @question = Question.new(question_params)
  ...
end

private

def merge_current_user_id
  params[:question].merge!(user_id: current_user.id)
end

def question_params
  params.require(:question).permit(:user_id, <additional question attributes here>)
end