No route matches [PUT] error in Rails 4.2.6 - ruby-on-rails

I am facing this error while running my application:
No route matches [PUT] "/accounts/1/payment"
This is my form syntax;
<%= semantic_form_for(#account, :url => payment_path, :html => { :method => :put }) do |f| %>
......
<% end %>
routes.rb:
get "/accounts/:id/payment" => "accounts#payment", :as => "payment"
controller method:
def billing
#account = Account.find(params[:id])
if request.put?
// some code here
end
end

Replace
get "/accounts/:id/payment" => "accounts#payment", :as => "payment"
with
match "/accounts/:id/payment", to: "accounts#payment", :as => "payment", via: [:get, :put]
and let me know if this works.
EDIT
If your controller action is billing, then you may have to do
match "/accounts/:id/payment", to: "accounts#billing", :as => "payment", via: [:get, :put]

your form is trying to find a route matches method put while your route has a get method.
Use put in routes instead of get
put "/accounts/:id/payment" => "accounts#payment", :as => "payment"

In your form you are telling this form to be treated as PUT request but your routes says it is GET request. You should use same request type in both routes.rb and your form definition.

Try this
put "/accounts/:id/payment" => "accounts#billing", :as => "payment"

Related

RAILS: Rendering views from the same controller based on the route

I have two routes (defined in my config/routes.rb)
match 'compare' => 'front_office#search_by_id', :via => :get, :as => :front_office_compare
match 'full_report' => 'front_office#search_by_id', :via => :get, :as => :front_office_full_report
I would like to know how I can tell my controller to render the view based on my route, without adding a parameter on my URL.
Based on this Question&Answer I Managed to get the result I want with
match 'compare' => 'front_office#search_by_id', :via => :get, :as => :front_office_compare, :defaults => { :render => 'compare' }
match 'full_report' => 'front_office#search_by_id', :via => :get, :as => :front_office_full_report, :defaults => { :render => 'full_report' }
And in my controller I defined my action as:
def search_by_id
render :action => (params[:render] || "full_report")
end
But is this a Good Practice or is there a better way to do it?
Instead of creating different routes for each category you are making for simplifying you can write it like:
# config/routes.rb
get ":category", to: "front_office#search_by_id", as: 'front_office', constraints: {category: /(compare|full_report)/}
the above routes looks for /compare and /full_report and this will call search_by_id action in front_office controller.
then inside the controller do as follows:
def search_by_id
render params[:category]
end
params[:category] will hold the slug values which we passed through the URL

Rails 4 update model with subdomain and custom route

I can't update one user model with my custom route (without resources) and one subdomain.
My form is:
<%= form_for :user, :url => services_users_update_url(#user), :html => { :method => 'put', :multipart => true } do |f| %>
while my subdomain is "services".
My routes file:
constraints :subdomain => "services" do
scope :module => "services", :as => "services" do
match '/users/update/' => 'users#update', via: [:put]
end
end
When I display my form page, the url generated in my form is:
http://services.website.dev:3000/8296/users/update
When I confirm my form, the error message is:
No route matches [PUT] "/8296/users/update"
What's wrong in my routes ?
If I try:
match '/users/:id' => 'users#update', via: [:put]
the form generate a route error too.
I also tried:
match '/users/update/:id' => 'users#update', via: [:put]
without success.
EDIT: I want to add more infos.
My form page is located in the subdomain 'cloud' and my update action is located in the subdomain 'services'.
According to your url:
http://services.website.dev:3000/8296/users/update
^^^^/^^^^/^^^^^
:id/users/update
Your routes should be:
constraints :subdomain => "services" do
scope :module => "services", :as => "services" do
put ':id/users/update' => 'users#update', as: :services_users_update
end
end
Read documentation
You can use the built in resources method to generate your route:
constraints subdomain: "services" do
scope module: "services", as: "services" do
resources :users, only: [:update]
end
end

Custom Controller action not working

Hoping someone can point out the error of my ways here. I've got a form with a custom action with a standard submit button:
= form_for( #property, :html => {:data => {:abide => ''}, :id => 'property-edit-form'},:url => url_for(:action => 'update_promo')) do |f|
The route is as follows:
post 'properties/update_promo', :as => 'update_promo'
The controller action is:
def update_promo
#property = Property.find(params[:id])
if #property.update(property_params)
respond_to do |format|
format.html
format.js
end
else
render 'edit'
end
end
The problem is that it's still calling the default Update action. And I get the error:
Couldn't find Property with 'id'=update_promo
Can anyone help with this please?
Full routes...
get 'users/index'
get 'home/index'
get 'properties/update_regions', :as => 'update_regions'
get 'properties/update_places', :as => 'update_places'
get 'properties/update_map', :as => 'update_map'
get 'properties/update_promo', :as => 'update_promo'
root 'home#index'
post "versions/:id/revert" => "versions#revert", :as => "revert_version"
resources :properties
resources :users do
collection do
get :properties # add this line
end
end
Update your routes.rb file's line:
get 'properties/update_promo', :as => 'update_promo'
to this:
post 'properties/update_promo', :as => 'update_promo'
Also, you can change your form to look like this:
= form_for( #property, :html => {:data => {:abide => ''}, :id => 'property-edit-form'},:url => update_promo_path)) do |f|
You can use the url helpers instead of url_for(:action => ...).
You are probably GET methoding the submit so the show method is being called with the id update_promo, try adding
:method => :post
to the form_for.
To check, look at the log when you submit,
if your log starts with
Started GET
and not
Started POST
you need to add said paramter to form_for

Custom path for resourceful route

I'd like to be able to do ...
resources :posts
... and be able to customize the contextual url to be ...
:year/:month/:day/:id
... and still be able to do ...
post_path post
This would have it generate /2012/1/1/something-something.
However it appears I have to ...
get ':year/:id' => 'posts#show', as: 'posts'
Then in the view I have to ...
post_path post.year, post.id instead of post_path post
Is there anyway to have the post_path helper pick up the extra parameters it needs for the route?
If not this seems like it might be worth a feature request.
Sounds like you need something like:
match "posts/:year/:month/:day/:id" => "posts#show", :constraints => { :year => /\d{4}/, :month => /\d{2}/, :day => /\d{2}/ }

Rails3 Routing with Member Routes

I tried following the advice in this Rails3 Routes - Passing parameter to a member route
I have:
_health.html (partial)
<%= link_to image_tag("icons/pro_gray.png", :border =>'none'), flag_contact_flags_path(#id), :action => "flag", :id => #id, :remote => true, :class => 'flag' %>
routes.rb
resources :contact_flags do
get 'flag/:id', :action => 'flag', :on => :collection
end
When I do this I get this error:
ActionView::Template::Error (undefined method `flag_contact_flags_path' for #<#:0x00000101430358>):
When I rake routes I show:
GET /contact_flags/flag/:id(.:format) {:action=>"flag", :controller=>"contact_flags"}
Why doesn't think work?
I really need to pass the :id parameter.. any ideas?
Thanks
Looks like you need to change up the route you're pointing to. Try contact_flags_path(#id) instead of flags_contact_flags(#id).
Any luck?

Resources