2

I am new to ROR and I'm trying hard to solve the issue by going through all the stackoverflow and github pages, I promise I have gone through all of them but still couldn't find a solution to my problem. I hope you guys will help me out.

The issue is that after I implemented thumbs_up gem, I followed the instructions by Brady here: Clarification on how to use "thumbs_up" voting gem with Rails 3

But my view page will have an error message:

No route matches {:action=>"vote_up", :controller=>"posts", :id=>nil}

I have checked rake routes and the vote_up_post path is there, so I tried going to

http://0.0.0.0:3000/posts/vote_up and this shows up:

ActiveRecord::RecordNotFound in PostsController#show
Couldn't find Post with id=vote_up

Here is my views - index.html.erb

<%= link_to('vote for this post!', vote_up_post_path(@post), :method => :post) %>

posts_controller.rb

def vote_up
begin
current_user.vote_for(@post = Post.find(params[:id]))
render :nothing => true, :status => 200
rescue ActiveRecord::RecordInvalid
render :nothing => true, :status => 404
end
end

model - user.rb

# Include default devise modules. Others available are:
# :token_authenticatable, :confirmable,
# :lockable, :timeoutable and :omniauthable
devise :database_authenticatable, :registerable,
:recoverable, :rememberable, :trackable, :validatable

# Setup accessible (or protected) attributes for your model
attr_accessible :email, :password, :password_confirmation, :remember_me
# attr_accessible :title, :body

acts_as_voter

model - post.rb

attr_accessible :title, :source, :description, :imageurl, :link

validates :title, presence: true, uniqueness:true, length: { maximum: 70 }
validates :source, presence: true, length: { maximum: 20 }
validates :description, presence: true, length: { maximum: 240 }
validates :imageurl, presence: true
validates :link, presence: true

default_scope order: 'posts.created_at DESC'

acts_as_voteable

routes.rb

devise_for :users

resources :posts do
member do
post :vote_up
end
end

devise_scope :user do
get "sign_in", :to => "devise/sessions#new"
get "sign_out", :to => "devise/sessions#destroy"
get "sign_up", :to => "devise/registrations#new"
end

resources :submissions

match '/submit', to: 'submissions#new'
match '/post', to: 'posts#new'

Sorry for asking such a stupid problem, would really appreciate help from you guys.

Community
  • 1
  • 1
Kevon
  • 137
  • 2
  • 8

2 Answers2

0

The error you get

No route matches {:action=>"vote_up", :controller=>"posts", :id=>nil}

Comes from the line

<%= link_to('vote for this post!', vote_up_post_path(@post), :method => :post) %>

The router try to find a route with an id nil, here your id is "@post". The first thing to do is to search where come your @post variable, and check its not nil. Delete the link_to and set a simple

<%= @post.inspect %>

just on the top of your view will answer you : if your page show "nil", @post is not initialized on your controller :)

pierallard
  • 3,326
  • 3
  • 21
  • 48
  • Hi sir, I'm really new to the framework. I understand the part where the router is looking for an id nil, but I still dont know how to tell it to look for the appropriate post id, say 1,2,3,4 etc. My full index.html.erb in views is like this below, which is wrapped by <% @posts.each do |post| %>. And my posts_controller is just generated from basic scaffold framework, I didn't change anything in the post_controller.rb other than adding paginate and limit to the number of posts showing. Thank you for your answer, and look forward to get more advice from you! – Kevon Feb 28 '13 at 14:16
  • <% @posts.each do |post| %>
    <% end %>
    – Kevon Feb 28 '13 at 14:18
0

Change your routes.rb to have:

resources :posts do
  collection do
    get :vote_up
  end
end

The error Couldn't find Post with id=vote_up says the vote_up is the member of your posts controller which is not.

So if you make your action vote_up as a collection type instead of member than it will be all fine.

Now if you hit the url with:

http://0.0.0.0:3000/posts/vote_up 

and this shows up:

vote_up will no longer be treated as a id.

ActiveRecord::RecordNotFound is because the vote_up is treated as a member id of your posts model. And trying to find the vote_up as a id in your post model is giving you record not found as it will never be your id.

sjain
  • 23,126
  • 28
  • 107
  • 185
  • Hi Saurabh, I have tried changing member to collection, but nothing changes to the errors I'm getting. Is there any other things I should change? – Kevon Feb 28 '13 at 14:15
  • you are accessing the vote_up through url get request and not through form post request. So do one change- `resources :posts do collection do get :vote_up end end`. I updated the same in my answer. – sjain Feb 28 '13 at 14:19
  • I changed from post to get, but the error is still here. Home page `Routing Error No route matches {:action=>"vote_up", :controller=>"posts", :id=>nil}` – Kevon Feb 28 '13 at 14:48
  • Is this the error that you are getting after hitting `http://0.0.0.0:3000/posts/vote_up` from url? – sjain Feb 28 '13 at 14:51
  • for http://0.0.0.0:3000/posts/vote_up, now i'm getting this `ActiveRecord::RecordNotFound in PostsController#vote_up Couldn't find Post without an ID` – Kevon Feb 28 '13 at 14:53
  • i think the problem now links to the comments by ForgetTheNorm below..its that the id is not found. is that right? – Kevon Feb 28 '13 at 14:55
  • See the line `current_user.vote_for(@post = Post.find(params[:id]))`. This expects the id which is not provided by you. So you are getting the error. – sjain Feb 28 '13 at 15:01
  • Sorry, it will not affect your this code. I removed that comment already.See my last comment. The post requires the id which is not there in url. – sjain Feb 28 '13 at 15:01
  • I think you're right. Because I wrap the code with <% @posts.each do |post| %> i thought it will auto associate an id to it, but it doesn't. How can I assign an ID? sorry I'm so bad in this.... please see my answer to the above answer – Kevon Feb 28 '13 at 15:04
  • Would it help if I share my github? – Kevon Feb 28 '13 at 15:06
  • ok share and just point out that how you are passing the params id to the `vote_up` method. Any code or indication? – sjain Feb 28 '13 at 15:09
  • wow amazing. suddenly the homepage works, but the "vote!" button links to http://0.0.0.0:3000/vote_up_post_path(@post).... which is not right. – Kevon Feb 28 '13 at 15:16
  • Thanks a lot, I can't upvote because of my low reputation. I marked useful :) I guess I don't get the logic of the link I should be writing because the function of the "Vote!" button should be writing a vote into the database, not showing a vote_up.html.erb page. I still can't get the concept clear.. – Kevon Feb 28 '13 at 15:27
  • This is probably the matter of clearing what you want to perform at first and then move on like that. – sjain Feb 28 '13 at 15:32
  • remember you can also pass the id along with the link like `<%= link_to('vote for this post!', vote_up_post_path(@post.id), :method => :post) %>.` Then you will get params[:id] also in the vote_up action. – sjain Feb 28 '13 at 15:35
  • The function I want to perform is the function Thumbs Up provides. Each post has a button "Vote!" and when the current_user clicks it, then it adds a vote to that post with a specific id in the database. and next to the button it shows how many vote it has already received. – Kevon Feb 28 '13 at 15:36