Hi Jon Rowe, This `url_for(controller: 'gadgets', action: :index, user_id: params[:user_id])` worked in my tests.
Can you please explain why it rendered the correct path when I did not provide user_id inside #url_for? Does the method #url_for assumes user_id exist in params hash and it will use that by default? Thanks! Chemi On Tuesday, February 2, 2016 at 2:05:26 PM UTC+5:30, Jon Rowe wrote: > > You need to add the relevant `:user_id` to the `url_for` call. e.g. > > `url_for(controller: 'gadgets', action: :index, user_id: params[:user_id] > )` > > But it would be better to use `user_gadgets_path(user_id: > params[:user_id])` (note you don’t need to use params to pass this in, it > could be any source of User (e.g `current_user.id` if you’re using > Devise)). > > Jon Rowe > --------------------------- > [email protected] <javascript:> > jonrowe.co.uk > > On Tuesday, 2 February 2016 at 19:18, Tenzin Chemi wrote: > > Please ignore the typo made in previous reply: > navs << { > active: true > text: 'View', > href: url_for(controller: 'gadgets', action: :index), > } > > > On Tuesday, February 2, 2016 at 1:46:54 PM UTC+5:30, Tenzin Chemi wrote: > > Inside Helper: > navs << { > active: true > text: 'View', > href: url_for(controller: 'gadgets, action: :index), > } > > View Spec: > require 'spec_helper' > > RSpec.describe '/shared/nav', type: :view do > > > before do > allow(view).to receive(:params).and_return(user_id: 'test-id', id: > 'id_1') > render template: 'shared/_menu' > end > > context 'when set' do > it 'works' do > end > end > end > > > On Tuesday, February 2, 2016 at 10:31:44 AM UTC+5:30, Jon Rowe wrote: > > Thats not the way to specify params, can you share your helper code and > spec? > > Jon Rowe > --------------------------- > [email protected] > jonrowe.co.uk > > On Tuesday, 2 February 2016 at 14:15, Tenzin Chemi wrote: > > I have a helper used for views which calls url_for(controller: > 'controller_name', action: :index) and my rake route for the controller > looks like this: > > Prefix Verb URI Pattern > Controller#Action > > user_gadgets GET /users/:user_id/gadgets(.:format) gadgets#index > > user_gadget GET /users/:user_id/gadgets/:id(.:format) gadgets#show > > I provided the both id and user_id in params by method stub: > allow(view).to receive(:params).and_return(user_id: 'test-id', id: 'id_1') > > But it still throws the same error. Thanks :) > > On Tuesday, February 2, 2016 at 4:25:12 AM UTC+5:30, Jon Rowe wrote: > > Yes, you need to provide `:user_id` in your params for the route to match. > You can see this by running `rake routes` which will show you the > parameters required for each route. > > Jon Rowe > --------------------------- > [email protected] > jonrowe.co.uk > > On Tuesday, 2 February 2016 at 03:12, Tenzin Chemi wrote: > > I have a nested resources in my route, is it because of that? > > resources :users, only: [:index, :show] do > resources :gadgets, only: [:index, :show] > end > > > > > On Sunday, January 31, 2016 at 5:39:32 AM UTC+5:30, Jon Rowe wrote: > > Without seeing your routes file it’s hard to guess what’s going wrong > here, the common scenarios for this involve not giving rspec required parts > of the route, (for example params for any scoping you’ve applied to the > route, e.g. if the route is company/:id/gadgets you need to provide the > company id into the params) that would be present in the view by default > > Jon Rowe > --------------------------- > [email protected] > jonrowe.co.uk > > On Friday, 29 January 2016 at 19:11, Tenzin Chemi wrote: > > I am getting no route matches error when I test a view which uses > #url_for. My application runs fine when I load the view but rspec tests > can't find the route. I don't want to mock #url_for. > > Is there a way to make this work without mocking? Thanks in advance. > > > > 1) shared/menu when auth is disabled displays > Failure/Error: render template: 'shared/_menu' > > ActionView::Template::Error: > > No route matches {:action=>"index", :controller=>"gadgets"} > > # > /Users/tc/.rvm/gems/ruby-2.2.3/gems/actionpack-4.2.4/lib/action_dispatch/journey/formatter.rb:46:in > > `generate' > > # > /Users/tc/.rvm/gems/ruby-2.2.3/gems/actionpack-4.2.4/lib/action_dispatch/routing/route_set.rb:729:in > > `generate' > > # > /Users/tc/.rvm/gems/ruby-2.2.3/gems/actionpack-4.2.4/lib/action_dispatch/routing/route_set.rb:760:in > > `generate' > > # > /Users/tc/.rvm/gems/ruby-2.2.3/gems/actionpack-4.2.4/lib/action_dispatch/routing/route_set.rb:803:in > > `url_for' > > # > /Users/tc/.rvm/gems/ruby-2.2.3/gems/actionpack-4.2.4/lib/action_dispatch/routing/url_for.rb:156:in > > `url_for' > > # > /Users/tc/.rvm/gems/ruby-2.2.3/gems/actionview-4.2.4/lib/action_view/routing_url_for.rb:94:in > > `url_for' > > -- > You received this message because you are subscribed to the Google Groups > "rspec" group. > To unsubscribe from this group and stop receiving emails from it, send an > email to [email protected]. > To post to this group, send email to [email protected]. > To view this discussion on the web visit > https://groups.google.com/d/msgid/rspec/884080c5-7dc2-4e89-b3d8-7dfa7916a586%40googlegroups.com > > <https://groups.google.com/d/msgid/rspec/884080c5-7dc2-4e89-b3d8-7dfa7916a586%40googlegroups.com?utm_medium=email&utm_source=footer> > . > For more options, visit https://groups.google.com/d/optout. > > > -- > You received this message because you are subscribed to the Google Groups > "rspec" group. > To unsubscribe from this group and stop receiving emails from it, send an > email to [email protected]. > To post to this group, send email to [email protected]. > To view this discussion on the web visit > https://groups.google.com/d/msgid/rspec/79ddc31c-2060-4134-8e71-09a83aa358b9%40googlegroups.com > > <https://groups.google.com/d/msgid/rspec/79ddc31c-2060-4134-8e71-09a83aa358b9%40googlegroups.com?utm_medium=email&utm_source=footer> > . > For more options, visit https://groups.google.com/d/optout. > > > -- > You received this message because you are subscribed to the Google Groups > "rspec" group. > To unsubscribe from this group and stop receiving emails from it, send an > email to [email protected]. > To post to this group, send email to [email protected]. > To view this discussion on the web visit > https://groups.google.com/d/msgid/rspec/bfca9f42-3a68-4bc3-9432-1c963d57e96a%40googlegroups.com > > <https://groups.google.com/d/msgid/rspec/bfca9f42-3a68-4bc3-9432-1c963d57e96a%40googlegroups.com?utm_medium=email&utm_source=footer> > . > For more options, visit https://groups.google.com/d/optout. > > > -- > You received this message because you are subscribed to the Google Groups > "rspec" group. > To unsubscribe from this group and stop receiving emails from it, send an > email to [email protected] <javascript:>. > To post to this group, send email to [email protected] <javascript:>. > To view this discussion on the web visit > https://groups.google.com/d/msgid/rspec/8731c42c-c6d1-4ea3-b718-dd9b4c37973b%40googlegroups.com > > <https://groups.google.com/d/msgid/rspec/8731c42c-c6d1-4ea3-b718-dd9b4c37973b%40googlegroups.com?utm_medium=email&utm_source=footer> > . > For more options, visit https://groups.google.com/d/optout. > > > -- You received this message because you are subscribed to the Google Groups "rspec" group. To unsubscribe from this group and stop receiving emails from it, send an email to [email protected]. To post to this group, send email to [email protected]. To view this discussion on the web visit https://groups.google.com/d/msgid/rspec/1184333c-6721-4349-8399-0d329bdcd869%40googlegroups.com. For more options, visit https://groups.google.com/d/optout.
