No route matches a named route - ruby-on-rails

My route file is like this
match '^movies\?.*\&commit=Refresh$', :to =>'movies#filter', :via => :get
resources :movies
match 'movies/sort_by/:criteria', :to => 'movies#sort_by', :as => 'sort_by'
where I want to match the 1st route with
movies?utf8=✓&ratings[PG-13]=1&ratings[PG]=1&commit=Refresh
. And in the view, I define a submit_tag that will execute the 1st route: = submit_tag 'Refresh', filter_by_path(). But I keep getting:
No route matches {:controller=>"movies", :action=>"filter"}
I don't know how to solve this since I try to check my route on rubular.com and it matches the link perfectly.

Try using :as in the first route
match '^movies\?.*\&commit=Refresh$', :to =>'movies#filter', :as => 'filter_by'
Then in your view,
= submit_tag 'Refresh', filter_by_url

Related

Rails route to get a resource using query string

I would like to have a custom route querystring based, to access a specified resource. For example:
/opportunities/rent/san-miguel-de-tucuman?id=45045
That route should map to the action OpportunitiesController#show, with the resource id 45045.
Thanks in advance!!!
Updated
This are my current routes:
get 'oportunidades/alquiler/san-miguel-de-tucuman/:id', to: "opportunities#show"
get 'oportunidades/alquiler/san-miguel-de-tucuman', to: "opportunities#index"
So, if I navigate to the /oportunidades/alquiler/san-miguel-de-tucuman?id=123456 route, it go to the Opportunities#index action.
P/S: sorry, I forget to mention that I have a similar route for the index action.
Make your custom routes as:
resources: opportunities, except: :show
get '/opportunities/rent/san-miguel-de-tucuman/:id' => 'opportunities#show', :as => 'opportunities_show'
and pass your 'id' as opportunities_show_path(id)
EDIT
Change your routes as:
get 'oportunidades/alquiler/san-miguel-de-tucuman/:id' => "opportunities#show", :as => 'opportunities_show'
get 'oportunidades/alquiler/san-miguel-de-tucuman' => "opportunities#index", :as => "opportunities_index"
Now when you want to access your show page just use opportunities_show_path(:id =>123456 )
And for index page use opportunities_index_path
Use this
match '/opportunities/rent/san-miguel-de-tucuman/:id', :to => 'opportunities#show', :via => :get
and pass a object to the path so created. Eg:-
something_path(#object), here #object is object that with id which will be passed in routes
Option 1
Query string parameter
// /opportunities/rent/san-miguel-de-tucuman?id=45045
match '/opportunities/rent/san-miguel-de-tucuman', :to => 'opportunities#show', :as => "show_opportunity", :via => :get
Option 2
Add id like new parameter. More friendly.
// /opportunities/rent/san-miguel-de-tucuman/45045
match '/opportunities/rent/san-miguel-de-tucuman/:id', :to => 'opportunities#show', :as => "show_opportunity", :via => :get
In both cases, you need to call the route like this:
show_opportunity_url(:id => 45045)
In your controller you will get the id in params[:id]

rails translated paths example

routes.rb
match "about/how_it_works" => "about#how_it_works", :as => "about_how_it_works", :via => :get
match "about/we_are" => "about#we_are", :as => "about_we_are", :via => :get
match "about/what_is" => "about#what_is", :as => "about_what_is", :via => :get
I read this rails guide and changed my code.
new routes.rb
scope(path_names: { about_we_are: 'translated-about-we-are', about_what_is: 'translated-about-what-is' }) do
resources :about, path: 'translated-about'
end
But when I enter localhost:3000/about/translated-about-we-are, I encounter no route matches error.Do you know how can handle with this problem?
Since you've specified path for resources about your path becomes translated-about/.... So you need to use:
http://localhost:3000/translated-about/translated-about-we-are
then you should not get the error.
You can check all the routes generated by issuing rake routes from within your application directory.

Not getting a [:id] parameter from url, Default route is not working

When I request a URL as follows:
http://localhost:3000/password_resets/edit/4RghIKJNygEDswIuuCo
I'm not getting the [:id] parameter, i.e. 4RghIKJNygEDswIuuCo.
Here is my route file, are there any modifications required for this?
ActionController::Routing::Routes.draw do |map|
match 'primary', :to => 'pages#primary', :as => "primary"
match 'admins', :to => 'admin_users#list', :as => "admins"
match 'login', :to => 'user_sessions#new', :as => "login"
match 'logout', :to => 'user_sessions#destroy', :as => "logout"
root :to =>"public#index"
match 'HFA/:id/' => 'public#show'
match 'HFA/:id/:uid' =>'public#show'
match 'public/projectview/:projectid/' => 'public#projectview'
map.connect ':controller/:action/:id.:format'
map.connect ':controller/:action/:id'
resources :users
resources :usertypes
resources :user_sessions
end
The default is /controller/:id/edit, following the REST architecture.
Do you really want to change this?
If so, verify the order of the declarations:
Rails routes are matched in the order they are specified, so if you
have a resources :photos above a get 'photos/poll' the show action’s
route for the resources line will be matched before the get line. To
fix this, move the get line above the resources line so that it is
matched first.
http://guides.rubyonrails.org/routing.html#crud-verbs-and-actions
and check your availables routes with the rake routes command:
http://guides.rubyonrails.org/routing.html#inspecting-and-testing-routes
if you put match instead of 'get' you will get all http (GET, POST, PUT, DELETE) route verbs
get 'password_resets/:id/edit', to: 'password_reset#create', as: :send_password_reset
the 'to:' defines the controller method, 'as:' defines the path name, 'get' defines the route verb and ':id' is the token created by this line in app/views/user_mailer/password_reset.text.erb
<%= send_password_reset_url(#user.password_reset_token) %>
run "rake routes" command and check the routes for "password_resets/edit/4RghIKJNygEDswIuuCo".

Rails 3 "No Route Matches" error With Custom Named Route

I have this custom route in my routes.rb
match '/businesses/:permalink', :to => 'businesses#show', :as => :business_permalink
resources :businesses
And I have constructed a link like this:
<%= link_to business.name, business_permalink_path %>
However, whenever I visit the page with that link, I get this error:
No route matches {:controller=>"businesses", :action=>"show"}
I tried inverting the route order:
resources :businesses
match '/businesses/:permalink', :to => 'businesses#show', :as => :business_permalink
This does not work. It works if I change the link to this:
The show action exists and is defined in the file controllers/businesses_controller.rb.I want to create a custom URL using my permalink.
I am new in Rails and I know I am just missing something. What am I missing?
Try this:
<%= link_to business.name, business_permalink_path(business.permalink) %>
Try this:
match '/businesses/:permalink' => 'businesses#show', :as => :business_permalink
More here: http://railscasts.com/episodes/203-routing-in-rails-3

Rails 3: Replace /users/username/something for /username/something

I already have a route to match /username for the users show page. However, when I add another action, for example: followers and following as below:
resources :users, :only => [:show] do
get :following, :followers
end
I get the URL /users/username/following instead of /username/following.
How can I make all the /users/username URL's be matched as /username/etc.. ?
Here's my /username route:
match '/:id' => 'users#show', :constraints => { :id => /[a-zA-Z0-9\-_]*/ }, :as => "user_profile"
thank you.
Edit:
I have fixed this by adding
match '/:id/following' => 'users#show/following', :as => "user_following"
match '/:id/followed' => 'users#show/following', :as => "user_followers"
But I'm not sure if that's the best solution. What I'd really want is a default route that would match all /:id/:action to the appropriate action omitting the /users.
I have fixed this by adding
match '/:id/following' => 'users#show/following', :as => "user_following"
match '/:id/followed' => 'users#show/following', :as => "user_followers"
But I'm not sure if that's the best solution. What I'd really want is a default route that would match all /:id/:action to the appropriate action omitting the /users.
Near the bottom
match ':id/:action', :controller => :users

Resources