Routing Error Ruby on Rails -- Voting system - ruby-on-rails

I'm implementing a voting system to my blog's comments, using the acts_as_votable gem.
However, I'm getting a routing error: no route matches. missing required keys: [:id]
#routes.rb
resources :articles do
resources :comments do
member do
put "like", to: "comments#upvote"
end
end
end
# comments controller
def upvote
#comment.upvote
redirect_to :back
end
# comments/show.html.haml
= link_to like_article_comment_path(#comment), method: :put do
= #comment.get_upvotes.size

If you use rake routes | grep like (to fatch that route), you'll get:
like_article_comment PUT /articles/:article_id/comments/:id/like(.:format) comments#upvote
So you're missing the first parametar - :article_id in your link. Should be:
= link_to like_article_comment_path(#article, #comment), method: :put do
= #comment.get_upvotes.size
Also add the #article logic in your upvote method.

Related

Rails 4 - How to route delete path

I have been using Rails 3 and am trying to use Rails 4. I am following a ToDo tutorial here: http://www.codelearn.org/ruby-on-rails-tutorial/introduction-views-layouts-helpers-assets-pipeline
I am trying to delete the ToDo item using the Rails 'link_to' helper method, and have been looking up online and have not been able to get it to work for the past 90 minutes so I decided to ask for help.
Here is the error message I am getting:
Routing Error
No route matches [DELETE] "/todos/delete_path"
Here is my 'rake routes' from the terminal:
Prefix Verb URI Pattern Controller#Action
todos_index GET /todos/index(.:format) todos#index
todos_delete DELETE /todos/delete(.:format) todos#delete
Here is the 'link_to' helper method from inside of the:
index.html.erb
<%= link_to 'Delete last todo', 'delete_path', method: :delete %>
Here is my:
routes.rb
Rails.application.routes.draw do
get 'todos/index'
# get 'todos/delete'
match 'todos/delete' => 'todos#delete', via: :delete
Here is my:
todos_controller.rb
class TodosController < ApplicationController
def index
# #todo_array = ['Buy Milk', 'Buy Soap', 'Pay bill', 'Draw Money']
#todo_items = Todo.all
render :index
end
def delete
#put delete logic here
#todo_items = Todo.last
#todo_items.destroy
end
end
Thank you for taking the time to look at my problem!
This is how you would write a destroy method:
def destroy
#todo = Todo.last
#todo.destroy
redirect_to todos_index_path #or wherever you want to redirect user after deleting the tod
end
Also, I would simply use RESTful routes:
Rails.application.routes.draw do
resources :todos, only: %i(index destroy)
end

acts_as_votable redirecting when trying to vote up/vote down

I've problem with acts_as_votable gem. I have simple forum app and I want to have feature to vote up and down posts. I used for this acts_as_votable gem. I've added to posts controllers two methods:
def upvote
#post = post.find(params[:id])
#post.liked_by current_user
redirect_to forum_topic_path(#post.topic.forum, #post.topic)
end
def downvote
#post = post.find(params[:id])
#post.downvote_from current_user
redirect_to forum_topic_path(#post.topic.forum, #post.topic)
end
My routes is :
resources :topics, except: :index do
resources :posts do
member do
put "like", to: "posts#upvote"
put "dislike", to: "posts#downvote"
end
end
end
And in my topic show action view i have the followings links:
= link_to "Upvote", like_topic_post_path(post.topic.id,post.id, method: :put)
= link_to "Downvote", dislike_topic_post_path(post.topic.id,post.id, method: :put)
When I'm trying to click upvote or downvote i have been redirected to:
http://localhost:3000/topics/104/posts/55/like?method=put and i have following error:
No route matches [GET] "/topics/104/posts/55/like"
What's wrong?
Try "GET" method instead of "PUT" as shown below,
resources :topics, except: :index do
resources :posts do
member do
get "like", to: "posts#upvote"
get "dislike", to: "posts#downvote"
end
end
end
I'd change the PostsController code for redirect to:
redirect_to :back
If you are using Rails 5 this is better:
redirect_back(fallback_location: root_path)

Thumbs up gem routing error

I seek clarification on using the thumbs up gem with rails 4. I current have a user resource and a post resource and have set up thumbs up as follows.
add gem to gemfile and installed it using bundler.
Generated require migration
User model
class User < ActiveRecord::Base
acts_as_voter
end
Post model
class Post < ActiveRecord::Base
acts_as_voteable
end
Post controller
def vote_up
#post = Post.find(params[:id])
current_user.vote_for(#post)
respond_to do |format|
format.js
end
end
View
<%= link_to('vote for this post!', vote_up_post_path(#post) , :method => :post) %>
Route file
resources :posts do
member do
post :vote_up
end
end
However i keep getting this error
No route matches [POST] "/posts/vote_up"
And after running rake routes I can see that the following route is available to me:
vote_up_post POST /posts/:id/vote_up(.:format) posts#vote_up
any ideas what could be the cause of this error ?
Could you please show us your view.
Apparently, you are calling
/posts/vote_up
instead of
/posts/:id/vote_up
"vote_up" acts like a RESTful action, similar to "post/1/delete", "post/1/edit". So you need to add this custom RESTful action in route.
Change your route like this at first.
resources :posts do
member do
post 'vote_up'
end
end
Then, in your view, to use this path, add resource as arg
vote_up_post_path #post
Reference: http://guides.rubyonrails.org/routing.html#adding-more-restful-actions
Other people reading this notes should realise that post is not a singluar of his controller posts... but the post http verb
resources :contenders do
member do
post 'vote_up'
end
end
Not sure but in your view, instead of #post try giving #post.id to vote_up_post_path.
<%= link_to('vote for this post!', vote_up_post_path(#post.id) , :method => :post) %>

Routing Error - custom controller

I have a has many through association.
Firms have many Users through Follows.
I want Users to be able to Follow Firms. - I am using Devise for the users.
I have the following action in my firms controller.
def follow
#firm.users << current_user
end
in my routes.rb
resources :firms do
post :follow, on: :member
end
and in my firms view
<%= link_to "Follow", follow_firm_path(#firm), method: :post %>
However when I keep getting the following Routing Error in the browser
No route matches {:action=>"follow", :controller=>"firms"}
Rake Routes confirms the following
follow_firm POST /firms/:id/follow(.:format) firms#follow
Any ideas what the problem may be?
Many thanks
Edit: Controller code
class FirmsController < ApplicationController
before_filter :authenticate_user!, :except => [:show, :index]
def index
#firm_names = Firm.all.map &:name
direction = params[:direction]
direction ||= "ASC"
#firms = Firm.order("name #{direction}")
respond_to do |format|
format.html # index.html.erb
format.js
end
end
def follow
#firm.users << current_user
end
I am using the follow action in a partial in the index view.
everything looks good and this should work perfectly. Except that I see a typo in the following line
<%= link_to "Follow", follow_firm_path(#firm), method: :post %>
after the :method there should an => not a : . this will make the link a get request not a post request, that might be the issue, try using a simple link and replace post will get in your routes.rb just to test if the issue is arising due to this.
you can also test route methods from the console
rails c
app.follow_firm_path(2)
I noticed you also have an error in your routes, there should be an => not a : after :on
resources :firms do
post :follow, :on => member
end
You should define methods like this...
resources :firms do
collection
post :follow, on: :member
end
end
I think if this method does not create anything its type should be get.
Try it

Link to resource

I have created model, view and controller:
$ rails generate scaffold Post name:string title:string content:text
I run server and I see list of posts if I open in the browser http:\localhost:3000\posts.
Now I need to create link to this page. Something like:
<%= link_to("settings", { :controller => 'groups', :action => 'index'}) %>
But I get error on opening this page:
Couldn't find Group with ID=index
How ca I create link to http:\localhost:3000\posts and which action do I use in this case?
I think the path helpers are excellent in these cases. You could do it like this:
<%= link_to("Posts", posts_path) %>
posts_path in this case will link to http://localhost:3000/posts
When you use use resources :posts in your routes.rb you automatically get a few path helpers. For example:
posts_path # /posts
post_path(#post) # /posts/1
edit_post_path(#post) # /posts/1/edit
new_post_path # /posts/new
If you have a route such as:
resources :groups
In config/routes.rb then you will have the helper groups_path. You can use rake routes to see all of your routes and helpers, but in this case you will have:
groups_path
group_path(#group)
edit_group_path(#group)
Polymorphic Routes Documentation

Resources