ruby on rails - No route matches {:action=>"up_vote", :controller=>"votes", :post_id=>"1"} -
i've been receiving following error in rspec when trying upvote method pass:
failures: 1) votescontroller#up_vote adds up-vote post failure/error: post( :up_vote, post_id: @post.id ) actioncontroller::urlgenerationerror: no route matches {:action=>"up_vote", :controller=>"votes", :post_id=>"1"}
now can upvote , downvote when i'm on server, it's test won't work.
here's code:
votes_controller_spec.rb
require 'rails_helper' describe votescontroller include testfactories include devise::testhelpers describe '#up_vote' "adds up-vote post" request.env["http_referer"] = '/' @user = authenticated_user @post = associated_post sign_in @user expect { post(:up_vote, post_id: @post.id) }.to change { @post.up_votes }.by 1 end end end
votes_controller.rb
class votescontroller < applicationcontroller before_action :load_post_and_vote def up_vote update_vote!(1) redirect_to :back end def down_vote update_vote!(-1) redirect_to :back end def update_vote!(new_value) if @vote authorize @vote, :update? @vote.update_attribute(:value, new_value) else @vote = current_user.votes.build(value: new_value, post: @post) authorize @vote, :create? @vote.save end end private def load_post_and_vote @post = post.find(params[:post_id]) @vote = @post.votes.where(user_id: current_user.id).first end end
routes.rb
rails.application.routes.draw devise_for :users resources :users, only: [:update] resources :questions resources :advertisements resources :topics resources :posts, except: [:index] resources :summaries, only: [:create, :new, :show] resources :comments, only: [:create, :destroy] post '/up-vote', to: 'votes#up_vote', as: :up_vote post '/down-vote', to: 'votes#down_vote', as: :down_vote end end 'about' => 'welcome#about' 'contact' => 'welcome#contact' root to: 'welcome#index' end
module providing methods within specs:
module testfactories def associated_post(options={}) post_options = { title: 'post title', body: 'post bodies must pretty long.', topic: topic.create(name: 'topic name'), user: authenticated_user }.merge(options) post.create(post_options) end def authenticated_user(options={}) user_options = {email: "email#{rand}@fake.com", password: 'password'}.merge(options) user = user.new(user_options) user.skip_confirmation! user.save user end end
relevant rake routes
topic_post_up_vote post /topics/:topic_id/posts/:post_id/up-vote(.:format) votes#up_vote topic_post_down_vote post /topics/:topic_id/posts/:post_id/down-vote(.:format) votes#down_vote
any ideas why i'm getting url generation error?
i think need pass topic_id
post method this: post( :up_vote, post_id: @post.id, topic_id: @post.topic.id )
Comments
Post a Comment