I've got a problem with my vote methods for comments. Im a begginer at RoR and waiting for your suggestions.
The error I get is:
ActionController::RoutingError at /posts/51f7d1279fefa5405a000003 No route matches {:controller=>"comments", :action=>"vote(1)", :class=>"post__button--edit"}
My code:
comment.rb
class Comment
include Mongoid::Document
field :name, type: String
field :email, type: String
field :body, type: String
field :up_vote, type: Integer, default: "0"
field :down_vote, type: Integer, default: "0"
belongs_to :post
validates_presence_of :name, :email, :body
def self.add_up_vote
self.increment(:up_vote, 1)
end
def self.add_down_vote
self.decrement(:down_vote, 1)
end
end
comment_controller.rb
.
.
.
def vote(a)
@comment = Comment.find(params[:comment_id])
@post = Post.find(params[:post_id])
if a == 1
comment.add_up_vote
redirect_to @post
elsif a == -1
comment.add_down_vote
redirect_to @post
else
redirect_to @post
end
end
routes.rb
Easyblog::Application.routes.draw do
authenticated :user do
root :to => 'home#index'
end
root :to => "home#index"
devise_for :users
resources :users
resources :posts do
resources :comments
member do
post :mark_archived
end
end
end
Im waiting for your help :)
What is a
here? I guess it's vote direction
You have to remove argument a
from vote
action and pass direction via params
for link path.
Fox example:
vote_comment_path(@comment.id, dir: 1) # or dir: -1
More than that there is no route for vote
action. You can describe it like so:
resources :comments do
put :vote, as: :member
end
upd I would recommend you to read following guide http://guides.rubyonrails.org/routing.html
action
in your path is not valid. You link should looks like
= link_to 'Yes', vote_comment_path(comment, dir: 1), method: :put
vote_comment_path
can be different you could check it by rake routes
command:
$ rake routes