【问题标题】:passing extra values into a partial and to a create action through a form通过表单将额外的值传递给部分和创建操作
【发布时间】:2013-03-26 20:38:30
【问题描述】:

在我的 rails 应用程序中,我有一个 views/questions/show.html.erb 页面,除了预期的 @question 变量之外,它还可以访问 @answers 变量以显示特定问题的所有答案.

在显示特定问题的所有答案的循环内,我希望显示一个区域供用户评论(在答案而不是问题上),所以我创建了一个评论部分,其中,按照建议在另一个 SO 答案中,我创建了一个“locals”哈希来传递变量。在 Comments 控制器的创建操作中,我希望能够访问答案(评论属于答案)和问题(其中 has_many :answers ),所以我将答案和问题 id 传递到部分中,就像这样

   <% for answer in @answers %>
    ....(code ommitted)
    <%= render :partial => 'comments/form', :locals => { :answer_id => answer.id, :question_id => @question.id } %>
    ...(code ommitted)

    <% end %> 

我通过 cmets 部分中的隐藏字段传递 answer_id 和 question_id 像这样

   <%= simple_form_for @comment do |f| %>
   <%= f.input :content, as: :text, label: 'comment'%>
   <%= f.hidden_field :user_id, :value => current_user.id %>
   <%= f.hidden_field :answer_id, :value => answer_id %>
   <%= f.hidden_field :question_id, :value => question_id %>
   <%= f.button :submit %>
   <% end %>

在 cmets 控制器的创建动作中,我这样做

def create
    @question = Question.find(params[:comment][:question_id])
    @answer = Answer.find(params[:comment][:answer_id])
    @comment = @answer.comments.build(params[:comment])
    if @comment.save
        flash[:notice] = "Successfully created comment"
        redirect_to root_path
        # redirect_to question_answers_path(@question) (will eventually want to redirect to the question)
    else
        render :action => 'new'
    end 
end

第一个问题,我觉得我做了一些尴尬的事情,必须以以下方式提取问题 ID(和答案 ID)

@question = Question.find(params[:comment][:question_id])

但是,这些是提交表单后可用的参数

 Parameters: {"utf8"=>"✓", "authenticity_token"=>"VtogfCsI137lbk2l64RXtrfRn/+Rt1/jM8pfDVY29gM=", "comment"=>{"content"=>"test", "user_id"=>"12", "answer_id"=>"25", "question_id"=>"22"}, "commit"=>"Create Comment"}

所以我必须像params[:comment][:question_id]这样挖掘出question_id

第二个更具挑战性的问题(对我来说很有挑战性)是 Rails 告诉我

Can't mass-assign protected attributes: question_id

我没有理由将 question_id 存储在评论模型上,所以我没有在数据库中为它创建一个列。但是,当我这样做时

@comment = @answer.comments.build(params[:comment])

由于 question_id 是 :comment 中的参数之一(我在表单中为其创建了隐藏字段),Rails 试图将其保存在 cmets 表中。但是,我真正想要在 cmets_controller.rb 的创建操作中访问问题的唯一原因是使用它在保存后重定向回问题。

您能建议我可以做些什么来解决这个问题吗?我觉得因为我没有太多使用 Rails 的经验,所以我做的每件事都很尴尬,这可能是它不起作用的原因。我想解决这个问题的简单方法是在评论模型中添加一个 question_id 列,但是它们之间没有“关联”,所以我认为这是错误的解决方案。

【问题讨论】:

    标签: ruby-on-rails


    【解决方案1】:

    你的第一个问题不是问题。 没有太多方法可以做到这一点。

    第二个问题,你必须这样做

    @comment = @answer.comments.build(params[:comment][:content].merge(user_id: params[:comment][:user_id], answer_id: params[:comment][:answer_id])) 因为否则您将尝试分配表中没有的属性

    或者你可以换一种方式

    params[:comment].delete('question_id')
    

    然后

    @comment = @answer.comments.build(params[:comment])
    

    【讨论】:

    • 谢谢,但我希望保存的不仅仅是 :content。我还需要将 user_id 保存在 cmets 表中。那我该怎么做呢?像这样? @comment = @answer.cmets.build({params[:comment][:content], params[:comment][:user_id]})
    • 其实也需要保存 answer_id 因为 cmets belongs_to :answer 模型。你能再更新一次你的答案吗?我基本上需要保存除了 question_id 之外的所有内容
    • 实际上,它给了我一个错误。注释(它是一个字符串)不能对其调用合并。这是错误消息:“这是一个测试评论”的未定义方法“合并”:字符串
    • 您只需要构建新的@comment。然后您可以分配您的值,例如@comment.user_id = params[:comment][:user_id],然后保存
    猜你喜欢
    • 1970-01-01
    • 2019-07-25
    • 2013-07-25
    • 2011-09-07
    • 2018-10-29
    • 1970-01-01
    • 2016-03-01
    • 2021-02-12
    • 2014-02-02
    相关资源
    最近更新 更多