Reputation: 5629
I've got some problems with my routes at the moment.
I have a controller house with function add. Also I have a view in houses/add.html.erb
I call it with domain/houses/add so I got this error: No route matches [GET] "/houses/add"
routes.rb looks like this:
resources :api_users, :as => :users
get '/:controller(/:action(/:id))'
post '/:controller(/:action(/:id))'
Upvotes: 0
Views: 33
Reputation: 842
If you are going to use only get and post method,
do not use resources
due to the memory usage.
match "houses/add" => "houses#add", via: [:get, :post]
and never use single quote in routes.rb
get '#{action}' <- this is not working
get "#{action" <- this works.
YOURCONTROLLER.action_methods.each do |action|
get "CONTROLLER_NAME/#{action}", to: "CONTROLLER_NAME##{action}"
end
Upvotes: 1
Reputation: 4404
Change it to this:
resources :api_users, as: :users
# empty for memory concerns
resources :houses, only: [] do
collection do
get :add
post :another_action
end
end
OR if you are only trying to rename new to add then you can do something like this:
resources :houses, path_names: { new: 'add' }
# Which will now path /domain/houses/new --> /domain/houses/add
# NOTE* This does not change the actual action name, it will still look for houses#new
Something to note about match
protocols to single route:
guides.rubyonrails.org/routing 3.7 HTTP Verb Constraints
In general, you should use the get, post, put, patch and delete methods to constrain a route to a particular verb. You can use the match method with the :via option to match multiple verbs at once:
match 'photos', to: 'photos#show', via: [:get, :post]
You can match all verbs to a particular route using via: :all:
match 'photos', to: 'photos#show', via: :all
Routing both GET and POST requests to a single action has security implications. In general, you should avoid routing all verbs to an action unless you have a good reason to.
'GET' in Rails won't check for CSRF token. You should never write to the database from 'GET' requests, for more information see the security guide on CSRF countermeasures.
Upvotes: 0