Ruby on rails multi level route path - ruby-on-rails

I have defined a route as per the below but I am getting some errors. Below is the code outlining what is happening
namespace :shopping do
resources :merchants, only: [:index, :show] do
Which I was hoping I would be able to use = link_to shopping_merchant_path(merchant)
But this is giving me the following error
No route matches {:action=>"show", :controller=>"shopping/merchants", :id=>nil, :locale=>#<Merchant id: 1, name: "52e9689bc89a", featured: false>} missing required keys: [:id]
rake routes output
Pauls-Air% rake routes | grep shopping_merchant
shopping_merchant_products GET (/:locale)/shopping/merchants/:merchant_id/products(.:format) shopping/products#index {:locale=>/en/}
shopping_merchant_product GET (/:locale)/shopping/merchants/:merchant_id/products/:id(.:format) shopping/products#show {:locale=>/en/}
shopping_merchants GET (/:locale)/shopping/merchants(.:format) shopping/merchants#index {:locale=>/en/}
shopping_merchant GET (/:locale)/shopping/merchants/:id(.:format) shopping/merchants#show {:locale=>/en/}

As onikron suggested you need to pass parameter to your route
= link_to shopping_merchant_path(#merchant)
I think you are trying something like
= link_to shopping_merchant_path(locale: #merchant)
:locale=>#< Merchant id: 1, name: "52e9689bc89a", featured: false>
No need of :locale in link_to

Related

Rails route not found even when its present

My following API route is not found when hit with CURL or POSTMAN client
Started POST "/api/users/register_handheld" for 181.74.100.34 at 2017-05-11 11:27:42 +0000
DEBUG: Chewy strategies stack: [2] <- atomic # /home/deploy/boold/shared/bundle/ruby/2.2.0/gems/chewy-0.8.4/lib/chewy/railtie.rb:17
ActionController::RoutingError (No route matches [POST] "/api/users/register_handheld"):
But when I run rake routes, this URL is present.
api_users_register_handheld POST /api/users/register_handheld(.:format)
crowd/api/v1/handheld_users#create {:format=>"json"}
This URL actually works if I hit in rails console:
app.post "/api/users/register_handheld"
Started POST "/api/users/register_handheld" for 127.0.0.1 at 2017-05-11 11:21:03 +0000
DEBUG: Chewy strategies stack: [3] <- atomic # /home/deploy/boold/shared/bundle/ruby/2.2.0/gems/chewy-0.8.4/lib/chewy/railtie.rb:17
Processing by Crowd::Api::V1::HandheldUsersController#create as JSON
Routes file is
namespace :api, defaults: {format: 'json'} do
scope module: :v1 do
resources :dashboard, controller: "account/dashboard"
post '/account/settings/create_notification' => 'account/settings#create_notification'
post '/users/register' => 'users#create'
post 'users/register_handheld' => 'handheld_users#create'
put 'users/update_profile' => 'handheld_users#update_profile'
post 'users/login' => 'handheld_users#login'
NOTE: this repo is Rails Engine
try below code:
/api/users/register_handheld.json
as you specified format type json
Maybe this helps someone.
Using POSTMAN i accidentally uncheck Host Key in headers (POSTMAN autogenerate it each time you send any request). It was a nightmare to find what's the problem until i compared my request with a similiar one and found the missing key.

path not recognized by Rails

My app is calling a resource trougth a path helper show_popular_hashtags_path, but Rails seems to not recognize it. This is my partial:
.col-xs-12.no-padding.blue-title{style: "page-break-inside: avoid !important;"}
.col-xs-12.text-center.global-map.no-padding
%h1
= t('uniq.hashtag.title')
.js-column-chart-hashtags{data: {url: show_popular_hashtags_path(id: #project.id, provider: provider), provider: provider}}
.column-chart-hashtags.statistics-chart{class: "#{provider}", style: "width: 100%"}
%h2.empty-message.column-chart-hashtags{class: "#{provider}", style: "padding-top: 15px; padding-bottom: 30px; display: none;"}
= t('unique.there_is_no_info')
this is my routes.rb:
namespace :user do
resources :projects, except: [:delete] do
member do
get :show_users, to: 'projects#js_show_users_data', as: :show_users_data
get :show_popular_hashtags, to: 'projects#js_show_popular_hashtags', as: :show_popular_hashtags
get :show_activity_data, to: 'projects#js_show_activity_data', as: :show_activity_data
For the another routes everything goes very well, even when I run rake routes | grep show_popular_hashtags, the output is:
$ rake routes | grep show_popular_hashtags
show_popular_hashtags_user_project GET /user/projects/:id/show_popular_hashtags(.:format) user/projects#js_show_popular_hashtags
So, this looks like the route is well, but when I visit the view which contains the partial this error appears:
undefined method `show_popular_hashtags_path' for #<#<Class:0x007fa20d865d10>:0x007fa20dc5a6f0>
I already restarted the server, but it did not work.
The name of your routes helper is actually show_popular_hashtags_user_project, not show_popular_hashtags. You can see it in the first column of the rake routes output:
$ rake routes | grep show_popular_hashtags
show_popular_hashtags_user_project GET /user/projects/:id/show_popular_hashtags(.:format) user/projects#js_show_popular_hashtags
Just change the name of the show_popular_hashtags routes helper to show_popular_hashtags_user_project in your view, and it will work.

rspec chokes on named path in rails 3

Re-writing a photography portfolio site, and urls have to follow the format:
[site].com/portfolio/[category]/[image]
to match current implementation.
[site].com and [site].com/portfolio should resolve to the root. my routes file is below:
root :to => 'portfolio#index'
match '/portfolio', :to => 'portfolio#index'
match '/portfolio/*category/*photo', :to => 'portfolio#photo'
match '/portfolio/*category', :to => 'portfolio#photo'
I added a link in my header to:
%li= link_to "Portfolio", root_path
and this works fine, but when I add:
%li= link_to "Editorial", portfolio_photo_path
my pages resolve fine in-browser, but rspec chokes out on me; even the simplest http success tests that would previously run fine now return:
1) PortfolioController GET 'index' returns http success
Failure/Error: get 'index'
ActionView::Template::Error:
undefined local variable or method `portfolio_photo_path' for #<#<Class:0x572d210>:0x5733d68>
# ./app/views/layouts/_header.html.haml:7:in `_app_views_layouts__header_html_haml__420220390_28357236'
# ./app/views/layouts/application.html.haml:10:in `_app_views_layouts_application_html_haml__156489427_30065868'
# ./spec/controllers/portfolio_controller_spec.rb:8:in `block (3 levels) in <top (required)>'
(when I rake routes, i get the following)
$ rake routes
root / portfolio#index
portfolio /portfolio(.:format) portfolio#index
/portfolio/*category/*photo(.:format) portfolio#photo
/portfolio/*category(.:format) portfolio#photo
You should add ":as => :portfolio" statement:
match '/portfolio', :to => 'portfolio#index', :as => :portfolio
Answered in my comment above; I'd defined variable-globbed routes, and brain-fartedly didn't realize that i'd have to construct the links directly and dynamically.

Path helper doesn't automatically get object's id anymore?

for some reason this one doesn't work anymore
admin_club_path(club)
but following does work
admin_club_path(club.id)
Anyone know what the reason could be?
This is the error message:
No route matches {:action=>"show", :controller=>"admin/clubs", :id=>#<Club id: 98, name: "1. FSV Mainz 05 II", image: "mainz05.gif", photo_id: 3, league_id: 5, approved: false, permalink: "1-fsv-mainz-05-ii", created_at: "2011-11-22 16:43:13", updated_at: "2011-11-22 16:43:13">}
Apparently it takes the whoe object as id but why?
The routes seem to be alright:
admin_clubs GET /admin/clubs(.:format) {:action=>"index", :controller=>"admin/clubs"}
POST /admin/clubs(.:format) {:action=>"create", :controller=>"admin/clubs"}
new_admin_club GET /admin/clubs/new(.:format) {:action=>"new", :controller=>"admin/clubs"}
edit_admin_club GET /admin/clubs/:id/edit(.:format) {:action=>"edit", :controller=>"admin/clubs"}
admin_club GET /admin/clubs/:id(.:format) {:action=>"show", :controller=>"admin/clubs"}
PUT /admin/clubs/:id(.:format) {:action=>"update", :controller=>"admin/clubs"}
DELETE /admin/clubs/:id(.:format) {:action=>"destroy", :controller=>"admin/clubs"}
club GET /clubs/:id(.:format) {:action=>"show", :controller=>"clubs"}
Lucapette gave me the right hint. I'm using permalink gem which had following in the model:
permalink :name, :unique => true, :to_param => %w(id name)
after I removed the :to_param parameter it all works fine. I will report it to the developer of them gem!
Thanks lucapette and dombesz!

Turning resources into custom routes (Ruby/rails)

Started building an application here. client and server style architecture sending active resources across the wire and storing as activeRecord server side. Managed to get it up and running with a nice example in an O Reilly book except its using scaffold.
Rails routing - custom routes for Resources
is using map.resources from rails 2-
I'm using rails 3 so Its not really applicable and while I did post a question about routes from 2 to 3, I still cant convert this.
So here whats im looking at:
Rake routes with
resources :user_requests
gives:
user_requests GET /user_requests(.:format) {:controller=>"user_requests", :action=>"index"}
POST /user_requests(.:format) {:controller=>"user_requests", :action=>"create"}
new_user_request GET /user_requests/new(.:format) {:controller=>"user_requests", :action=>"new"}
edit_user_request GET /user_requests/:id/edit(.:format) {:controller=>"user_requests", :action=>"edit"}
user_request GET /user_requests/:id(.:format) {:controller=>"user_requests", :action=>"show"}
PUT /user_requests/:id(.:format) {:controller=>"user_requests", :action=>"update"}
DELETE /user_requests/:id(.:format) {:controller=>"user_requests", :action=>"destroy"}
I'd like to remove this and the resources and have my own routes pointing to my own defs.
Heres a quick attempt
match '/user_requests(.:format)' => 'user_requests#create , :via =>:post'
match '/user_requests/:id(.:format)' =>"user_requests#show"
returns almost the exact same as above
/user_requests(.:format) {:controller=>"user_requests", :action=>"create"}
/user_requests/:id(.:format) {:controller=>"user_requests", :action=>"show"}
With the exception of the REST nouns at the start and the links. Its the same yet my own routes dont work.
What do I need to add to my routes to make them do the same thing as resources?
I'm not keeping scaffold as I've been told its never used in the real world. And I will be changing the names of my defs, but one step at a time.
Error that server shows:
Started POST "/user_requests.xml" for 127.0.0.1 at Tue Jul 12 17:13:32 +0100 2011
Processing by UserRequestsController#create as XML
Parameters: {"method"=>"POST", "user_request"=>{"depth"=>3000000, "url"=>"www.stackoverflow.com"}}
SQL (0.1ms) SELECT 1 FROM "user_requests" WHERE ("user_requests"."url" = 'www.stackoverflow.com') LIMIT 1
AREL (0.3ms) INSERT INTO "user_requests" ("updated_at", "depth", "url", "created_at") VALUES ('2011-07-12 16:13:32.765392', 3000000, 'www.stackoverflow.com', '2011-07-12 16:13:32.765392')
Completed 404 Not Found in 17ms
ActionController::RoutingError (No route matches {:controller=>"user_requests", :id=>#<UserRequest id: 6, url: "www.stackoverflow.com", depth: 3000000, created_at: "2011-07-12 16:13:32", updated_at: "2011-07-12 16:13:32">, :action=>"show"}):
app/controllers/user_requests_controller.rb:19:in `create'
app/controllers/user_requests_controller.rb:16:in `create'
Rendered /Library/Ruby/Gems/1.8/gems/actionpack-3.0.9/lib/action_dispatch/middleware/templates/rescues/routing_error.erb within rescues/layout (0.8ms)
It would guess that line 19 of your UserRequestsController has something like
redirect_to #user_request
which tries to guess a URL for showing a UserRequest object. Rails no longer knows how to do this; you've lost the helper methods generated by resources (such as user_request_path, new_user_request_path, etc).
You can tell Rails to generate the "show" helper method by adding a :as option to your show route, without the _path postfix:
match '/user_requests/:id(.:format)' => "user_requests#show", :as => 'user_request'
You'll now have access to a user_request_path and user_request_url, which Rails can use to find the URL to "show" a UserRequest.

Resources