I define each actions in my controller this way (in routes.rb):
resources :home do
collection do
get "home/index"
get "home/about_me"
get "home/contact"
end
end
If I would use a match for the action about_me, I have to use
resources :home do
collection do
get "home/index"
get "home/about_me"
get "home/contact"
end
end
match 'about-me' => 'home#about_me'
Exist any way, how to add match rule direct into a collection? I mean something like this:
resources :home do
collection do
get "home/index"
get "home/about_me", match => "about-me"
get "home/contact"
end
end
And I have one question yet - when I use in the routes.rb the second block of code, so when I set the URL address about-me, so the address works fine, but when I type there home/about_me, so I get the error
Unknown action: The action 'show' could not be found for HomeController.
What caused this error?
I think the problem here is that your routes have the home/ prefix when they are nested inside resources :home. Try this:
resources :home do
collection do
get :index
get :about_me
get :contact
end
end
Also, when you have trouble setting your routes, type rake routes in your console. This will generate the routes of your app and the corresponding paths and controllers.
EDIT: Here's the answer to your other question.
resources :home do
collection do
get 'about_me' => 'about-me'
end
end
Related
Route defined as follows
resources :purchases do
collection do
put :wirecardtest
end
end
Controller actions redirect in one of the following manners with associated error generated
format.html { redirect_to wirecardtest_purchase_path(#purchase)
undefined method `wirecardtest_purchase_path'
format.html { redirect_to wirecardtest_purchases_path(#purchase)
/purchases/wirecardtest.44
Behaviour is identical when putting code in view.
The ressource is defined in plural mode, as it ought to be. The redirect, as it is supposed to call a specific ressource should call the singular model-action mode (in plural it would generate the period).
I don't understand how I got into this damned-if-you-do, damned-if-you-don't position.
wirecardtest_purchases PUT /purchases/wirecardtest(.:format) purchases#wirecardtest
That's your mistake right there.. the path is generated as 'wirecardtest_purchases' but you are using 'wirecardtest_purchase' note the missing 's' to pluralize the 'purchase'.
Remember its a collection. So the path method is pluralized by rails.
When in doubt rake routes :)
---Update---
Improving the answer (check comments). Need here is to actually define a route as :member and not a :collection if you want to act upon a single object. Referring to Rails Docs,
resources ::purchases do
member do
get 'wirecardtest'
end
end
Ive been trying to create a simple route on rails, following this instructions
http://guides.rubyonrails.org/routing.html
my problem is that when I want to enter to my method I get a weird error.
I have a controler user and on my routes I wrote something like this
resources :users do
match "/custom/" => "user#custom"
end
So, at my controller I add this code
def custom
#user = User.find(params[:user_id])
end
but when I try to enter doing localhost:3000/users/1/custom I get an error like
uninitialized constant UserController
doing rake routes I can see
user_custom /users/:user_id/custom(.:format) user#custom
Any idea how to solve this problem?
I want this route to submit a form... is it possible to use this route (if i make it run) for use ajax? I want to submit a form.
Thanks
Change your route to:
resources :users do
match "/custom/" => "users#custom"
end
You should avoid the use of match though, since it will be deprecated in Rails 4. Try this instead
resources :users do
get :custom, on: :member
end
get is the verb, :custom the route and on: :member means that you are looking for a /users/:id/custom route instead of a /users/custom one. If you are looking for the latter, do this:
resources :users do
get :custom, on: :collection
end
Another way to do it is like this, which I prefer:
resources :users do
get 'custom', on: :collection
end
That gives you a route of /users/custom. If you were do use on: :member, then it would give you a route of /users/:id/custom.
You can also use a block for defining multiple custom actions for collections or members.
For example:
resources :users do
collection do
get 'custom'
post 'some_other_method'
end
member do
get 'some_action'
end
end
I want to add a custom action named "campaign" acting in a similar fashion like REST's "new" in the same controller, but it's purpose is different, so I wanted to separate them. Because, this campaign form will have some additional fields. One other alternative would be a passing an extra parameter to "new" action and render different templates for regular "new" action and custom "campaign". But, I wanna figure out why it didn't work out.
So, I come up with the following route ("messages" is the controller having both "new" and "campaign" actions):
get 'users/:user_id/messages/campaign', as: :campaign_user_message
or
resources :users do
resources: messages do
member do
get 'campaign'
end
end
end
At the console output, I'm getting ActiverRecord:RecordNotFound since it does this:
Started GET "/users/1/messages/campaign" for 127.0.0.1 at 2012-12-22 00:14:38 -0800
Processing by MessagesController#show as HTML
Parameters: {"user_id"=>"1", "id"=>"campaign"}
I'm calling the action in this way:
link_to campaign_user_message_path(#user)
if you want to have route such as "/users/1/messages/campaign" you should write smth like that:
resources :users do
resources :messages do
collection do
get 'campaign'
end
end
end
If you write in your way(with member do ... end) you code will generate url "/users/:user_id/messages/:id/campaign". and you should pass #user and #message:
link_to campaign_user_message_path(#user, #message)
I've did something like this overcome to the mentioned issue:
resources :users do
get 'messages/new_campaign' => 'messages#new_campaign'
post 'messages/create_campaign' => 'messages#create_campaign'
end
So, I can use the url helper "user_messages_new_campaign" to GET the action /users/:user_id/messages/new_campaign"
i would like to add a route that is only available in test environment, so i would prefer not to pollute routes.rb file. i cannot seem to find a working way to add a route dynamically after original routes were drawn. i tried this https://gist.github.com/1351762 but that didn't quite work
How can I add a new route after routes.rb has already loaded and processed all the routes?
The with_routing test helper redefines routes within a block.
with_routing do |map|
map.draw do
resources :test, only: [:show]
end
get :show
assert assigns(:test)
end
After some tries and errors, I found that:
Rails.application.routes.eval_block(Proc.new do
get "/backdoor", :to => "backdoors#backdoor"
end)
I am trying to add a route to a controller that has been set as a resource in the 'admin' namespace like this:
namespace :admin do
resources :books do
collection do
post :process_new
end
end
end
I have added an action int the Admin::BooksController for process_new, but whenever I try to access this action using the url: .../admin/books/process_new I get the following error:
Couldn't find Book with ID=process_new
It looks like it's routing to the show action and trying to use process_new as the id. Can someone shed some light on what I may be doing wrong?
**Edit:
I changed my redirects to use the helper functions and it seems to be working.
Add get :process_new to your resources :books routes:
namespace :admin do
resources :books do
collection do
get :process_new
post :process_new
end
end
end