Ruby on rails 3 link_to controller and action
Asked Answered
P

4

39

I know this is probably a pretty simple concept. I am trying to create a link to a controller and action. For example I have a link in my layout file to update a record when a link is clicked, so I need to be able to link to the controller and action. How would I accomplish this?

Photocurrent answered 9/4, 2011 at 19:2 Comment(1)
You use link_to to link to an action on a controller. You need to provide more information to get the full answer. Could you provide the name of your controller and the action you require?Crenelation
K
58
link_to "Label", :controller => :my_controller, :action => :index

See url_for.

Kynan answered 9/4, 2011 at 19:3 Comment(1)
also often it is not bad idea to set :method if it is different from :getSheatfish
O
11

Also with CSS:

<%= link_to "Purchase", { :controller => :transactions, :action => :purchase }, { class: "btn btn-primary btn-lg", style: "width: 100%;" } %>
Orford answered 11/11, 2015 at 18:6 Comment(0)
D
0

If you want to pass params too then do

<%= link_to student.name,  controller: "users", action: "show", id: student.id, partial: "profile"  %> 
Devolution answered 30/9, 2019 at 18:13 Comment(0)
C
0

For Rails 6:

 <%= button_to "SIGN IN", { controller: 'o_auth', action: :login }, { class: "btn btn-primary btn-lg", style: "width: 100%;", method: :get} %>
Callimachus answered 13/3, 2023 at 9:52 Comment(0)

© 2022 - 2025 — McMap. All rights reserved.