Question

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 :)

Was it helpful?

Solution

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

OTHER TIPS

Try changing like this

Easyblog::Application.routes.draw do

  resources :posts do
    resources :comments do
      match :vote
    end
    member do
      post :mark_archived
    end
  end
end

you could try something like this in youre routes

resources :posts do
  resources :comments do
    member do
      post 'vote'
  end
end 
 member do
  post :mark_archived
end

No need to build your own voting system. Take a look at the voteable_mongo gem

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top