代码之家  ›  专栏  ›  技术社区  ›  Ben

button_o-helper for Rails 4中的无效注释抛出“No route matches”

  •  0
  • Ben  · 技术社区  · 10 年前

    在这个系统中,您可以发布问题并对其进行评论,它使用 acts_as_votable gem,以便用户可以对评论进行上/下投票。我想显示一个向上/向下投票的按钮,而不是一个链接,所以我在视图中这样做:

    <h2>Comments</h2>
    <% @question.comments.order('cached_votes_up DESC').each do |comment| %>
      <% unless comment.errors.any? %>
      <p><strong>Commenter:</strong> <%= comment.user.username %></p>
      <p><strong>Comment:</strong><%= comment.body %></p>
    
      <%= button_to_if !comment.new_record?, 'Upvote', {
        :action => 'upvote',
        :controller => 'comments',
        :question => {
          :question_id => @question.id
        },
        :comment => comment.id
      },
      :class => 'btn btn-default' %>
    
      <%= button_to_if !comment.new_record?, 'Downvote', {
        :action => 'downvote',
        :controller => 'comments',
        :question => {
          :question_id => @question.id
        },
        :comment => comment.id
      },
      :class => 'btn btn-default' %>
      <% end %>
    <% end %>
    
    <h2>Add a comment</h2>
    
    <% if @comment && @comment.errors.any? %>
      <% @comment.errors.full_messages.each do |msg| %>
        <li><%= msg %></li>
      <% end %>
    <% end %>
    <%= form_for([@question, @question.comments.build]) do |f| %>
      <p>
        <%= f.label :body %><br>
        <%= f.text_area :body %>
      </p>
      <p>
        <%= f.submit %>
      </p>
    <% end %>
    

    现在,当评论 有效的 。但是,当注释已提交但 无效的 :

    No route matches {
        :action=> "upvote",
        :comment=> 3,
        :controller=> "comments",
        :question=> {
            :question_id=> 2
        },
        :question_id=> "2-test-question"
    }
    

    这是因为问题的注释还没有ID,因为它是无效的,因此尚未保存到数据库中。然而,它仍然被计算在视图中呈现的评论集合中。用 <% unless comment.errors.any? %> 似乎什么都没做。

    最初,我有 button_to helper来创建按钮,但由于它不起作用,我尝试用 button_to_if helper,以便它可以在渲染按钮之前评估条件。不幸的是,我所尝试的一切都是真实的。助手代码为:

    module ApplicationHelper
      # Render the button only if the condition evaluates to true
      def button_to_if (condition, name = nil, options = nil, html_options = nil, &block)
        if condition
          button_to(name, options, html_options, &block)
        end
      end
    end
    

    注释控制器中创建注释的相关方法:

    class CommentsController < ApplicationController
      before_action :authenticate_user!
    
      def create
        @question = Question.find(params[:question_id])
        @comment = @question.comments.build(comment_params)
        @comment.userid = current_user.id
        if @comment.save
          redirect_to @question
        else
          render 'questions/show'
        end
      end
    
      private
        def comment_params
          params.require(:comment).permit(:author, :body)
        end
    end
    

    注释模型很简单 presence length 验证。我知道这很好。问题似乎在 按钮_to 但就我的一生而言,我想不出出了什么问题。如有任何建议,将不胜感激。

    1 回复  |  直到 10 年前
        1
  •  0
  •   Ben    10 年前

    原来这是一个路由错误; :id 未在中定义 button_to 哈希,因此Rails无法正确路由它。奇怪的是,它只是在评论发布后才显示出来。