In my index.html.erb I have
<%= link_to 'Edit', edit_order_path(order) %>
Whenever I try to edit the order contents i got the following error:
NoMethodError in Order#index
undefined method `edit_order_path' for #<ActionView::Base:0x13f4aeb>
How can I overcome it?
Add following line of code in your routes.rb resources :orders
or add following line of code in routes.rb
match 'orders/:id/edit' => 'orders#edit', :as => :edit_order
I assume, your controller name as orders and action edit.
use following code for form
<% form_for(#order, :url => {:action => :update}) do |f| %>
Related
I have following routing problem in Rails 5:
<%= link_to product.id, product %>
generates a link like this
localhost:3000/products/12345
What I want is a link to the "ext" action in the products controller:
localhost:3000/products/ext/12345
If I try to build a link like this
<%= link_to 'To the product', :controller => :products, :action => :ext %>
it gives back following error:
No route matches {:action=>"ext", :controller=>"products"}
In the routes.rb I have
get "products/ext/:id", to: "products#ext"
Thanks for help!
Modify your routes to
get "products/ext/:id", to: "products#ext", as: :products_ext
and change your view to
<%= link_to products_ext_path(product) %>
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.
I'm making a form_tag panel that contains information (checkboxes) specific to a controller action. This action is set up in "routes.rb" as follows:
resources :students do
collection do
get :send_student_report_pdf
end
end
This setup works perfectly when I call the action from a link_to:
<%= link_to "Download PDF Report", :action => 'send_student_report_pdf', :controller => 'students'%>
However when I used it in a form_tag, it keeps giving me this error:
Routing Error
No route matches "/students/send_student_report_pdf"
The form_tag code I have is here:
<%= form_tag :controller => 'students', :action => 'send_student_report_pdf', :method => 'get' do %>
<%= label_tag "Include columns" %> <br>
<%= check_box_tag "first_name", params[:first_name], checked = true %> <%= label_tag "First Name" %><br>
<%= submit_tag "Download PDF Report", :action => 'send_student_report_pdf', :controller => 'students'%>
<% end %>
I have tried giving it the url, path like:
<%= form_tag send_student_report_pdf_students_path, :method => 'get' do %>
But it has been consistently giving me the same Route error (as if the action doesn't exist at all in routes.rb, even though it works perfectly using link_to instead of form_tag submit
Here is the code for the action in the controller, it basically sends back a file.
def send_student_report_pdf
#students = search_sort_and_paginate
puts "params[:first_name] = ", params[:first_namea]
send_data(generate_pdf_report(#students), :filename => "report.pdf", :type => 'application/pdf')
end
If you see that I'm missing something here, please help me.
Thank you very much,
Regards,
The :method => 'get' part in your form_for is in the url_for_options hash, not the options hash, so Rails will be putting it onto the url as cgi params instead. Try changing it to this:
form_tag url_for(:controller => 'students', :action => 'send_student_report_pdf'), :method => 'get' do ...
The reason you can't use the named route is because you didn't name it in your routes. If you name it in your routes and use the named route in your form_tag, you won't need to use url_for...
resources :students do
collection do
get :send_student_report_pdf, :as => :send_student_report_pdf
end
end
You can check whether your routes are as you expect by running rake routes
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.
I can run this command on my console:
#user.relationships.find_by_followed_id(9)
But I can't use this on my view:
<%= form_for current_user.relationships.find_by_followed_id(9), :html => { :method => :delete } do |f| %>
The error is:
undefined method `relationship_path' for #<#<Class:0x00000100bdc008>:0x00000100bc1f50>
I can't figure it out why.
you do not have a route for your relationship resource/model defined ...