Rails3 Routes - Passing parameter to a member route - ruby-on-rails

I would like to pass an extra parameter to a member route of a resource
something like:
resources :events do
member do
get 'register/:participant_type_id'
end
end
I could only accomplish it using a static match statement
Looking around the internet I saw that this might be possible in Rails 3.0.2. I'm using 3.0.1 and it certanlly is not.
Am I doing something wrong? or is it really not possible?
thanks

Try this:
resources :events do
member do
get 'register/:participant_type_id', :action => 'register'
end
end

Just to complete the answer with my little findings. It also confused me for quite a while.
In Rails3, the member route with parameters will not have the automatic generated xx_yy_path helper. You need to add it providing the :as => part, omitted the resources name.
Regarding the example provided, to get register_event_path and register_event_url, you need to define it like the following:
resources :events do
member do
get 'register/:participant_type_id', :action => 'register', :as => 'register'
end
end

Related

Missing :action key on routes definition Rails

I am getting the following error:
Missing :action key on routes definition, please check your routes.
For this route
resources :groups do
post '/groups/:id/add', on: :member
end
I have looked at several other SO answers on this but am not able to find one that helps me. What action is it missing?
This is not the right way to define routes. Actually router's member/collection accepts a key-value pair to generate the route. As you are defining a member function it will be like:
resources :groups do
member do
post :add
end
end
It will generate a route like: groups/1/add
If you use collection then it will generate: /groups/add
resources :groups do
collection do
post :add
end
end
Hope you get the idea.
Or
You can define specific route for this particular action like below:
match "/groups/:id/add" => "groups#add", via: :post
Key action here means controller action. as in
resources :groups do
post '/groups/:id/add', on: :member, action: "add"
end
Rails can't infer the action from a path. But you can define the action instead and rails will automatically figure out the path:
resources :groups do
post :add, on: :member
end

Is there a way to change the Routes URL without Impacting the controller its is pointing and paths used in the VIEWS

Routes.rb
scope :module => :abc do
namespace :old_namespace do
resources :posts
end
end
How Can I change the old_namespace to new_namespace, So that in my URLS I should see the new_namespace. I have too many views where I have used the previous routes with *_path and *_url methods. I dont want to change them for now. Is there any Rails Way to do this.
Things I have Tried,
scope :module => :abc do
namespace :new_namespace,:as => :old_namespace do
resources :posts
end
end
This Gives me the change in the URLS I need but Also, Gives me and Error
uninitialized constant Abc:NewNamespace
This is expecting me to have constant Abc:NewNamespace, ALthough I want this to use the Old Constant, Abc:OldNamespace, Something Similiar to :controller option in the resources for the namespace
You Simply do this:
scope module: 'abc/OldNamespace' do
resources :posts, path: 'new_namespace/posts'
end
here you are saying,
use abc::OldNamespace
use new_namespace/posts as URL path for posts resource.
This should work too, let me if this doesn't
I used this,
namespace :new_namespace,:as => :old_namespace, :module => :old_namespace do
This is working now.

Rails routes for get request with query params

I need a route to accept a request reports#show with an attached :query parameter and I can't figure out how to write it. It needs to respond to this link in my view:
= link_to report_path(query: params[:query]) do
config/routes.rb
resources :reports do
resources :chapters
resources :pages
end
Tried variations of: get '/reports/:id/:query', :as => 'reports_query' but I keep getting:
Routing Error
No route matches {:action=>"show", :controller=>"reports", :query=>"europe"}
Project is mostly RESTful but I'll take anything that works at this point. Thanks for any help.
You should define your route to query with code like this
# routes.rb
resources :reports do
get ':query', to: 'reports#show', on: :member, as: :query
end
It will generate path helper you can use that way
= link_to 'Query Report', query_report_path(#report, query)
I went through the same problem here, and I solved it using the default param while defining my routes.
get :twitter_form, defaults: { form: "twitter" }, as: :twitter_form, to: "campaigns#show"

How to modify url in routes.rb

I want to map a url '/admin/update_admin/1' but my url maps to '/admin/1/update_admin' for the provided resources
resources :admin
member do
post :update_admin
end
end
How to get the expected url? Because of wrong url I am getting the error
The action '1' could not be found for AdminController
If you want such url - you should try to use collection instead:
resources :admin
collection do
match "update_admin/:id" => "admin#update_admin", :via => :post
end
end
I am not sure for correct syntax (You can look it here: Routing in Rails)
Hope it will help you.

customize rails url with username

I want to copy the twitter profile page and have a url with a username "http://www.my-app.com/username" and while I can manually type this into the address bar and navigate to the profile page I can't link to the custom URL.
I think the problem is in the routes - here's the code in my routes.rb
map.connect '/:username', :controller => 'users', :action => 'show'
Also, I have Question and Answer models and I want to link to them with the customized URL like so:
http://www.my-app.com/username/question/answer/2210
There's nothing wrong with your route. Just remember to define it at the end, after defining all other routes. I would also recommend using RESTful routes and only if you want to have better looking URLs use named routes. Don't use map.connect. Here's some good reading about Rails routes.
Here's how this could look:
map.resources :questions, :path_prefix => '/:username' do |question|
question.resources :answers
end
map.resources :users
map.user '/:username', :controller => 'users', :action => 'show'
Just a draft you can extend.
To create urls you need to define to_param method for your user model (read here).
class User < ActiveRecord::Base
def to_param
username
end
end
I know this questions is old but it will help someone.
You could try the below. I've used it in a rails 4 project and all seems to be working great. The reason for the as: :admin is I also had a resources posts outside of this scope. It will add a admin to the helper calls e.g. admin_posts_path
scope ":username", module: 'admin', as: :admin do
get '', to: 'profiles#show'
resources :posts
end
I have used like this
In View part
portfolio.user.name,:id =>portfolio) %>
and in rout.rb
map.show_portfolio "portfolios/:username", :action => 'show_portfolio', :controller => 'portfolios'

Resources