Rails 5 link_to specific controller action - ruby-on-rails

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) %>

Related

getting error while edit_order_path

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| %>

Nested resources no route matches error

I am trying to get a basic nested resource path to work but am currently getting the following error:
No route matches {:action=>"show", :controller=>"stores"}
In my view I have the following links:
<% if current_user %> Hello <%= current_user.email %> /
<%= link_to 'Store', user_store_path %>
<%= link_to 'New Store', new_user_store_path %>
<%= link_to 'My Profile', home_path %>
<%= link_to 'Edit Profile', update_path %>
<%= link_to "Logout", logout_path %>
<% else %>
<%= link_to "Login", login_path %> / <%= link_to "Sign up", signup_path %>
<% end %>
Now when I rake my routes the paths I am being given match exactly those above - user_store_path etc..
My routes file looks like this:
resources :users do
resources :stores
end
match "signup" => "users#new"
match "home" => "users#show"
match "update" => "users#edit"
get "login" => "sessions#new"
post "login" => "sessions#create"
delete "logout" => "sessions#destroy"
get "logout" => "sessions#destroy"
resources :sessions
root :to => 'sessions#new'
This really is confusing me a lot because everything I have read on the RoR website suggests that this should work. Does anyone have any ideas where I am going wrong?
resources :users do
resources :stores
end
creates store routes which all require a given user since it is nested.
So e.g. <%= link_to 'Store', user_store_path %> is wrong because it doesn't provide any user. It should be <%= link_to 'Store', user_store_path(current_user, store) %>.
This also applies to your other links, e.g. <%= link_to 'New Store', new_user_store_path %> which should be <%= link_to 'New Store', new_user_store_path(current_user) %>
update based on your comment
No route matches {:action=>"show", :controller=>"stores" [...] occurs because you want to show a particular resource, in this example a store. Therefore, you need to pass in the store id or the store object to generate the path/url. E.g. <%= link_to 'Store', user_store_path(current_user, current_user.store.first %>. I missed that on my initial answer, sorry.
It is not enough to specify the path, you must also specify the objects or their id.
For example:
<%= link_to 'Store', [current_user, store] %>
<%= link_to 'Store', user_store_path(user_id: current_user.id, id: store.id) %>
<%= link_to 'New Store', new_user_store_path(user_id: current_user.id) %>
#and so on
Run rake routes and you will see that in some paths you want to specify id, for example: /users/:user_id/stores/:id
http://guides.rubyonrails.org/routing.html#creating-paths-and-urls-from-objects

No routes matches, for named route in rails 3

I have problem with rails routing.
Error is:
No route matches {:controller=>"orders", :action=>"sell_item"}
Route rule looks like:
match 'orders/sell/:id' => 'orders#sell_item', as: 'sell_item'
link generation in the view file:
<%= link_to 'sell', sell_item_url, id: line_item.id %>
function in the OrdersController
def sell_item(line_item_id)
line_item = LineItem.find(line_item_id)
line_item.status = 1
line_item.save
end
It still generates route if I remove '/:id' from route and ',id: line_item.id' from link_to.
Instead of
<%= link_to 'sell', sell_item_url, id: line_item.id %>
try
<%= link_to 'sell', sell_item_url(line_item) %>
Rails should build the route for you based on the object.
See here:
http://guides.rubyonrails.org/routing.html#creating-paths-and-urls-from-objects

Rails 3.0 form_tag Routing

I've used the following code in rails 2.3 without having to have a named route:
<% form_tag :controller => :session, :action => :login do %>
<ul>
<li><%= label_tag :email %><%= text_field_tag :email %></li>
<li><%= label_tag :password %><%= password_field_tag :password %></li>
<li><%= submit_tag 'Sign In', :id => 'login_submit' %></li>
</ul>
<% end %>
However, I'm converting my app to a 3.0 app and I get an error message saying "No route matches "/session/login"."
Do I need to create a named route in rails 3.0 or is there a better way to do this?
Creating a named route doesn't seem like the right approach because people would be able to call that through the url.
You can add resources route for sessions:
resoures :sessions
and fix form:
= form_tag sessions_path do
...
Remember rename you login action in sessions controller to new
UPD:
or you can add named route for 'session/login'
match 'session/login' => 'session#login', :as => :new_session
and use:
= form_tag new_session_path do
A named route is not necessary, but you should declare something like this in your routes file:
match "/sessions/login" => "sessions#login"
In RoR 3, "sessions#login" is a short hand for "the login action of the sessions controller".
The official RoR guide on routes is very well written and should help you a lot.

problems with form_tag for controller action with members-get route

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

Resources