繁体   English   中英

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

[英]Rails 4 adding attribute on form submission

我有一个表单,我想与当前用户的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,表格:

<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 

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

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

添加用户和问题之间的关联。 如果已经存在并且它是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

注意:您似乎在new操作中使用了strong_parameters 在这里使用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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM