Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/ruby-on-rails/54.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ruby on rails 如何防止一个分部从同一视图中的另一个分部劫持局部变量?_Ruby On Rails_Simple Form_Erb_Ruby On Rails 5 - Fatal编程技术网

Ruby on rails 如何防止一个分部从同一视图中的另一个分部劫持局部变量?

Ruby on rails 如何防止一个分部从同一视图中的另一个分部劫持局部变量?,ruby-on-rails,simple-form,erb,ruby-on-rails-5,Ruby On Rails,Simple Form,Erb,Ruby On Rails 5,因此,我有一个问题#Show.html.erb,它有以下内容: <% @question.answers.each do |ans| %> <%= render partial: "questions/answer", locals: {ans: ans} %> <% end %> <div class="row"> <div class="col-lg-8 col-lg-offset-2">

因此,我有一个
问题#Show.html.erb
,它有以下内容:

  <% @question.answers.each do |ans| %>
    <%= render partial: "questions/answer", locals: {ans: ans} %>
  <% end %>

  <div class="row">
    <div class="col-lg-8 col-lg-offset-2">
        <div class="ibox">
          <div class="ibox-title">
              <h5>Your Refactor Suggestion</h5>
          </div>

          <div class="ibox-content">
            <%= render partial: "answers/form", locals: {answer: @question.answers.build(user: current_user)} %>
          </div>
        </div>
    </div>
  </div>
导致此错误的原因:

ActionController::UrlGenerationError at /questions/42
No route matches {:action=>"vote_up", :controller=>"answers", :id=>nil} missing required keys: [:id]
这是我的
questions/_-answer.html.erb
partial(为简洁起见,将其截断):

这是我的
问题#显示
控制器:

  def show
    @question = Question.includes(:answers).find(params[:id])
  end

是什么导致了这种奇怪的行为?

我认为问题在于首先构建相关的答案(就像代码的这一部分:
@question.answers.build(用户:当前用户)
)。构造新的关联对象(答案),将外键链接设置为主对象(问题),并将其添加到关联集合(问题的所有答案)。但它还没有保存它,因此它确实还没有ID

因此,当您为问题答案协会的所有成员呈现部分下一步时,这也包括上面新创建的答案。在呈现片段时,您可能应该跳过新创建的答案更新:要做到这一点,您可以执行以下操作(有许多方法可以实现相同的效果,这只是一个示例):


这是Rails 5的一个特定问题吗?你在Rails 4.x上试过吗?@Pavan我还没有在Rails 4.x上测试过。我正在使用Rails 5构建我的应用程序,因此我遇到了这个问题。很有趣。如果新创建的答案没有ID,我如何跳过它?我不能做
回答。last
或类似的事情,因为记录没有保存…对吗?你有没有关于我如何做到这一点的代码示例?谢谢在遍历answers集合时,只需跳过新创建的记录即可。有关示例,请参阅答案更新。完美。工作起来很有魅力。谢谢
        <%= link_to vote_up_answer_path(ans), method: :post do %>
            <i class="fa fa-chevron-up"> </i>
        <% end %>
        <%= ans.cached_votes_total %>
        <%= link_to vote_down_answer_path(ans), method: :post do %>
            <i class="fa fa-chevron-down"> </i>
        <% end %>
            <%= ans.body %>
                        <%= link_to ans.user.try(:email), user_path(ans.user) %>
<%= simple_form_for(answer) do |f| %>
  <%= f.error_notification %>
  <%= f.input :question_id, as: :hidden %>
  <%= f.input :user_id, as: :hidden %>
  <div class="form-group">
    <%= f.input_field :body, as: :text, class: "form-control", rows: 8 %>
  </div>
  <div class="form-group">
    <%= f.input_field :language, collection: ["ruby", "python", "php"], as: :select, selected: "ruby", class: "form-control" %>
  </div>

  <div id="new-post-submission-button">
    <%= f.button :submit, class: "btn btn-lg btn-primary pull-right" %>
  </div>
<% end %>
  def show
    @question = Question.includes(:answers).find(params[:id])
  end
@question.answers.select { |ans| ans.persisted? }.each do |ans|
  ...
end