I have a simple_form_for that triggers a custom route instead of the traditional update method and i am getting this error.
No route matches [PATCH] "/ft_update")
I don't understand where this "/" is comming from. Can anyone help me?
here is my declaration of my simple_form_for
<%= simple_form_for #time,:url=>{:action=>"ft_update", :controller=> "experiencetables"},
remote: true do |f| %>
part of my routes file looks like this
resources :experiencetables do
member do
patch :vol_update, :formats => "js"
patch :ft_update, :formats => "js"
patch :pt_update, :formats => "js"
patch :employ_update, :formats => "js"
end
end
my experiencetables_controller.rb file looks like
def ft_update
#user = current_user
#exp_vol = Experiencetable.find_by(:user_id => #user.user_id,:full_time => true)
#exp_vol.update_attributes(work_params)
respond_to do |f|
f.js
end
end
As suggested by #DaveNewton in his comment, you can directly use a path helper.
Looking at your defined routes, ft_update_experiencetable_path would route to ExperiencetablesController's action named ft_update. You can directly do:
<%= simple_form_for #time,:url => ft_update_experiencetable_path(#time),
remote: true do |f| %>
Related
I have this in my routes file:
resources :payments do
member do
post :add_payment
end
end
With rake routes looking like this:
add_payment_payment POST /payments/:id/add_payment(.:format) payments#add_payment
And my form_for tag has this:
<%= form_for #loan, :url => add_payment_payment_path, :html => { :class => 'form-horizontal' } do |f| %>
Yet when i submit the form, I keep getting this error:
No route matches [PUT] "/payments/4/add_payment"
Why does it keep trying to use PUT instead of POST?
I have a form that will work sometimes and sometimes not and I can't work out what is changing between times, other than restarting the rails app. Currently it isn't working.
I have these entries in my routes file:
constraints :subdomain => 'my' do
namespace 'my', path: nil do
namespace 'author' do
resources :test_author do
resources :steps_author
[...]
end
end
end
end
The particular routes I'm interested in here produce this from rake routes
my_author_test_author_steps_author GET /author/test_author/:test_author_id/steps_author/:id(.:format) my/author/steps_author#show {:subdomain=>"my"}
PUT /author/test_author/:test_author_id/steps_author/:id(.:format) my/author/steps_author#update {:subdomain=>"my"}
DELETE /author/test_author/:test_author_id/steps_author/:id(.:format) my/author/steps_author#destroy {:subdomain=>"my"}
My form opening looks like this (using simple form and bootstrap):
<%= simple_form_for #step, :url => my_author_test_author_steps_author_path(#step), :html => { :class => 'form-horizontal' } do |f| %>
Can anyone shed some light on what is happening?
Update
Based on the help from juanpastas it seems that the form is rendering correctly however Rails is interpreting the request as a POST and not a PUT. Though for the life of me I can't work out why.
You need to pass complete params in route
<%= simple_form_for #step,
:url => my_author_test_author_steps_author_path(author, #step),
:html => { :class => 'form-horizontal' } do |f| %>
After reading your title I see you should be making POST to /author/test_author/24/steps_author/ without the last number. What about:
<% url = !#step.persisted? ? '/author/test_author/24/steps_author/' : my_author_test_author_steps_author_path(author, #step) %>
<%= simple_form_for #step,
:url => url,
:html => { :class => 'form-horizontal' } do |f| %>
I have harcoded route because I am not sure about your route helper name. You can run rake routes to find that helper.
I have the following form declaration :
<%= semantic_form_for #contrat_line,
:url => url_for(:controller =>"/backend/orders/#{#contrat.id}/contrat_lines",
:action =>"create") do |f| %>
I want to hit the following route :
POST /backend/orders/:order_id/contrat_lines(.:format) backend/contrat_lines#create
but i have the following error when i want to display the form (even before using it):
No route matches {:controller=>"backend/orders/23/contrat_lines", :action=>"create"}
I would say this route exist, why is it saying that it does not?
routes.rb code
match "/backend/orders/:order_id/contrat_lines" => "orders#contrat_lines", :as => "contrat_lines"
then, view code
<%= semantic_form_for #contrat_line,
:url => contrat_lines_url(:order_id => #contrat.id),
:action =>"create") do |f| %>
Thanks to salil i devised the following form :
backend_order_contrat_lines_url(:order_id => #contrat.id), :action =>"create" do |f| %>
And it works with thoses routes :
namespace :backend do
resources :orders do
resources :contrat_lines
end
end
Thx a lot!, if you want i can edit your answer and set it as accepted.
Very new to ruby on rails and trying to get my first ajax call working. It is making the ajax call, but it always calls the #index action and seems to ignore the URL parameter. Here's the code:
class UserController < ApplicationController
def flag
logger.debug "in flag user"
respond_to do |format|
format.js { render :layout=>false }
end
end
end
In my routes.db:
resources :users do
member do
post 'flag'
get 'flag'
end
end
And then in my view I create the link like this:
<%= link_to "Flag User", :url => flag_user_path(user.id), :method => :get, :remote => true %>
the HTML source is:
Flag User
rake routes produces this:
flag_user POST /users/:id/flag(.:format) users#flag
GET /users/:id/flag(.:format) users#flag
Whenever I click on the link, the user#index method always gets executed. How do I get the user#flag method to execute?
The issue was the :url symbol. I changed:
<%= link_to "Flag User", :url => flag_user_path(user.id), :method => :get, :remote => true %>
to:
<%= link_to "Flag User", flag_user_path(user.id), :method => :get, :remote => true %>
and everything works was expected. I was incorrectly using it like link_to_remote which requires the :url symbol. Thanks everyone for their input.
The problem is in how you are defining your routes. It needs to be like this,
resources :users do
post 'flag'
get 'flag', on: :member
end
Check the rails guides for routing to get more idea.
If you run rake routes, I think you'll see that the route created with name "flag_user_path" expects "post" not "get"
Been staring at this problem for a while now. Here's the error I'm getting when I try to view the page.
No route matches {:action=>"confirm", :controller=>"locations"}
This is what I have in the view.
<%= form_for(#location, :url => { :action => :confirm }) do |f| %>
<% end %>
And I think my routes file is set up correctly.
Finder::Application.routes.draw do
resources :locations do
member do
post :confirm
end
end
root :to => 'locations/index'
end
Any ideas?
Updated:
Ran rake routes and get what I think is correct.
confirm_location POST /locations/:id/confirm(.:format) {:action=>"confirm", :controller=>"locations"}
You can debug your routes easily in the future by running $ rake routes and looking at the output. ;)
I think what is happening is that your post :confirm isn't registering the route you're expecting. In the guides, match and it's brethren accept a string as a URL segment like so:
resources :locations do
member do
post 'confirm'
end
end
Note that "confirm" is now a string instead of a symbol.
If this doesn't help, run $ rake routes and tack the output onto your question.
Update
After seeing your rake output, I think that you just need to specify the POST method on your form_for:
<%= form_for(#location, :url => { :action => :confirm }, :method => :post) do |f| %>
<% end %>
You can also make this more readable using that helper method that Rails defines:
<%= form_for(#location, :url => confirm_location_path(#location), :method => :post) do |f| %>
<% end %>
Did you define the confirm action in your LocationsController?
Try adding a :method => :post to your form_for
<%= form_for(#location, :url => { :action => :confirm }, :method => :post) do |f| %>
<% end %>
Make sure that form_for doesn't sneak in a hidden field with _method=put if you have declared the route as accepting only post in your routes file.