无法为控制器找到Rails操作

时间:2015-12-29 21:16:08

标签: ruby-on-rails

我有一个评论控制器用户可以在哪里发表评论(现在点击它会闪烁一个句子,稍后会添加功能)。我遇到的问题是页面正在加载但是当我点击链接时没有发生任何事情。我去控制台时收到此错误:

AbstractController::ActionNotFound - The action 'tip' could not be found for CommentsController

但我在控制器中有动作:

def tip      
  redirect_to :back, :notice => "Thank you for the Tip. The User will Love it"
end

这是提示的路线:

get "/:id/tip" => "comments#tip", :as => "tip"

这里的Link_to也是"

= link_to(tip_path(comment), :class => "story-likes-link", :remote => true, :title => "Tip comment" ) do
  %i.fa.fa-money.fa-lg
  Tip

非常感谢您的帮助:)

编辑:整个控制器

class CommentsController < ApplicationController
  before_action :authenticate_user!
  before_action :set_user
  before_action :set_resource, :except => [:destroy]
  before_action :set_parent, :except => [:destroy]
  before_action :set_comment, :only => [:update, :destroy]
  respond_to :js

  # Create the comments/replies for the books/comics
  def create
    @comment = current_user.comments.new(comment_params)
    if @comment.save
      @comment.move_to_child_of(@parent) unless @parent.nil?
    end
    respond_with @comment, @resource
  end

  # Update the comments for the user
  def update
    @comment.update_attributes(comment_params)
    respond_with @comment, @resource
  end

  # Delete the comments for the books/comics
  def destroy
    @resource = @comment.commentable
    @comment.destroy
    respond_with @resource
  end

  private

  # Permitted parameters
  def comment_params
    params.require(:comment).permit(:body, :commentable_id, :commentable_type, :parent_id)
  end 

  # Set the parent resource for the comments and replies
  def set_resource
    if params[:comment][:commentable_type].eql?("Book")
      @resource = Book.find(params[:comment][:commentable_id])
    else
      @resource = Comic.find(params[:comment][:commentable_id]) 
    end
  end

  # Set the parent for the comments to make then as the child of the parent
  def set_parent
    @parent = params[:comment].has_key?(:parent_id) ? Comment.find(params[:comment][:parent_id]) : nil
  end

  # Set the comment for the source
  def set_comment
    @comment = Comment.find(params[:id])
  end

  def tip
      redirect_to :back, :notice => "Thank you for the Tip. The User will Love it"
  end

  def set_user
    @user = User.find(params[:user_id])
  end

end

1 个答案:

答案 0 :(得分:1)

问题是操作#tip方法隐藏在私有部分中,因此路由器根本看不到方法。

好吧,然后将方法代码移到private关键字上方:

def tip
   redirect_to :back, :notice => "Thank you for the Tip. The User will Love it"
end

private

注意:该操作方法不应放在privateprotected部分,而应放在public,这是ruby类定义流程的默认部分