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
Related
I get an error when I run this code. I want to make a button that redirects to an action from pages_controller.
Submultimi.html.erb
<%= form_tag({:controller => '/pages_controller', :action => 'calculeaza'}, :method => "post") do %>
<%= text_field_tag :field1 %>
<%= submit_tag "Button" %>
<% end %>
pages_controller.rb
def Submultimi
end
def Combinari
end
def Permutari
end
def calculeaza
puts "YAY"
redirect_to '/combinari'
end
Error message: No route matches {:action=>"calculeaza", :controller=>"pages_controller"}
routes.rb
Rails.application.routes.draw do
get '/submultimi' => 'pages#Submultimi'
get '/combinari' => 'pages#Combinari'
get '/permutari' => 'pages#Permutari'
end
If you get an error you must include the error message in the question. Anyway the controller name is obviously wrong so this must be the problem. The controller should not include the "/" nor the "_controller".
<%= form_tag( { :controller => 'pages', :action => 'calculeaza' }, :method => "post") do %>
<%= text_field_tag :field1 %>
<%= submit_tag "Button" %>
<% end %>
Your routes are also wrong:
there's no calculeaza method in routes
methods should be lowercase in routes and controller
Thanks to Phlip for the correction about the controller name :)
As Pablo said, remove the / from your controller name.
Your error message says there is no route defined. That means you haven't correctly told rails what to do with your form's post request; it's trying a route that doesn't exist.
You've got a few things going wrong. Your action names are capitalized in routes.rb, but your method names (at least the one you've linked) is not. They're case sensitive, convention is all lowercase. Also, you don't have a route defined for calculeaza. You need one, in routes.rb add (something like, I haven't tested any of this):
post '/calculeaza/' to 'pages#calculeaza'
If you want to see your currently defined routes, run rails routes in a terminal, and to use it in code append _path to the prefix verb. You end up with something like:
form_tag calculeaza_path do
You may want to read the rails routing guide, especially the parts about resourceful routes.
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'm following this answer on how to clone a record.
I can't though workout how to phrase the link and route it.
It is in my #miniature show view so I thought it should be something like
<%= link_to 'clone', :controller => :miniatures_controller, :action => :clone %>
and the route
match 'clone', to: 'miniatures#clone', via: 'get'
but this is clearly wrong. I am using #miniature in place of the above answer's #prescription.
What if you just use clone_path:
<%= link_to 'clone', clone_path %>
Cause rake routes shows just clone route. It works with the same routes.
If you are not satisfied with route and you should pass parameters (like miniature_id), add member to your resource (probably nested), like:
resources :miniatures do
member do
get 'clone'
end
end
This will be clone_miniature_path where you should pass #miniature:
<%= link_to 'clone', clone_miniature_path(#miniature) %>
I created an form_tag form:
<%= form_tag(set_image_dokumente_path) do %>
<%= text_field_tag :shit,'', data: {autocomplete_source: search2_patients_path}, :class => "shit" %>
<% end %>
I try to route to set_image action of dokumente controller, but i get the error:
undefined local variable or method `set_image_dokumente_path' for #<#<Class:0x711ff60>:0x762d578>
By default my form_tag goes to dokumente controller index action!
My routes:
resources :images
get "dokumente/index"
post "dokumente/index"
match 'patients/list' => 'patients#list'
resources :patients do
collection do
get :search2
end
end
How do i have to change it?
You can add the as: parameter to you route in order to create a named path.
For example:
post "dokumente/index", as: 'set_image_dokumente'
or similar, I'm not sure what you are trying to achieve, but I hope you get the idea :)
More info:
http://guides.rubyonrails.org/routing.html#generating-paths-and-urls-from-code
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.