Jakxna360
Jakxna360

Reputation: 885

Rails Action Can't Be Found For Controller

I have A comments Controller Where a User can go tip the comment (right now clicking it will flash a sentence, will add functions later). The problem I'm having is that the page is loading but when I click the link nothing is happening. I'm getting this error when I go to the console:

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

But I have the action in our controller here:

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

Here's the route for the tip:

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

Here's the Link_to also"

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

Thank you so much for the help : )

edit: whole Controller

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

Upvotes: 0

Views: 659

Answers (1)

Малъ Скрылевъ
Малъ Скрылевъ

Reputation: 16507

The problem is the action #tip method is hidden in private section, so the router sees not the method at all.

Well, then move the method's code above the private keyword:

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

private

NOTE: that action method should not be place to private or protected sections, only to public, which is default section for ruby class definition flow.

Upvotes: 3

Related Questions