Questions tagged [link-to]

link_to is the method in Ruby on Rails that is used to create link tags within views.

link_to is the method in Ruby on Rails that is used to create link tags within views.

e.g., With the appropriate route defined and the profile object with ID=1,

link_to "Profile", @profile

would return the HTML fragment

<a href="/profiles/1">Profile</a>

More details can be found in the Ruby on Rails API documentation.

863 questions
9
votes
4 answers

Why Rails "link_to" does not work for delete action?

In index.html.erb I display all products, and next to each product I have Edit and Delete actions: <% @products.each do |product| %> ... <%= link_to("Edit", edit_product_path(product.id), :class => 'action') %> <%= link_to("Delete", product,…
Misha Moroshko
  • 166,356
  • 226
  • 505
  • 746
9
votes
3 answers

Partial form in rails 3 needing different link depending on New or Edit

I have a basic setup of views that was generated by the Rails 3 scaffolding. It gives me a partial view _form.html.erb. Both my edit.html.erb and my new.html.erb render this partial view. In the partial view, I want to have a link_to that goes to…
Joel Friedlaender
  • 2,191
  • 1
  • 18
  • 24
9
votes
8 answers

rails link_to download file

I need to add a link to download the file from assets/docs/Физика.pdf I do not know how to do that. I am trying to do so here: in view - <%= link_to "download", '/Физика.pdf', :download => 'filename' %> I get an error message: No route matches…
Stiven Frams
  • 117
  • 1
  • 2
  • 7
9
votes
3 answers

Button_to uses POST Link_to uses GET, why? ROR

I've ran into a ror problem using the link_to. Why does my link to use the GET method and my button_to use the POST method, after I specified my "method"=>"post" within the link_to parameters? View: <%= button_to "pdf", :action => 'getquote' %> <%=…
JZ.
  • 21,147
  • 32
  • 115
  • 192
9
votes
2 answers

Using link_to in a class in a Rails helper

I have a rails helper using the structore below, but when I use it I get the message undefined method 'link_to' The helper is arranged as: module MyHelper class Facet def render_for_search link_to("Value", params) end end …
Mike Sutton
  • 4,191
  • 4
  • 29
  • 42
9
votes
2 answers

Rails link_to assigning class and id

<%= link_to event do %> #bunch of stuff making up the partial. <% end %> So I'm trying to assign an ID and a class to each item in a partial. I've seen where you have to call the full link_to function like <%= link_to event, { controller:…
Dudo
  • 4,002
  • 8
  • 32
  • 57
9
votes
2 answers

Add class inline if ... (in 1 line)

I want to add a class to a link_to only if a statement is true. <%= link_to product.name, product, :class => "last" if product == @products.last %> The problem that the IF statement affects the whole line and not just the :class part. I know i can…
Danpe
  • 18,668
  • 21
  • 96
  • 131
9
votes
1 answer

Rails link_to syntax in order to add some inner html

I have this code to generate a "unfollow" button via the 'link_to' function: <%= link_to "Non seguire più", user_user_relationship_path(id:@relationship.id), remote: true, id: "follow_#{@user.id}", class:"btn btn-small btn-danger", method: :delete…
Ciampo
  • 133
  • 1
  • 7
8
votes
1 answer

Rails 3 - How do you create a new record from link_to

I'm trying to create a 'tag' functionality which allows a user to "tag" items in which they are interested. Here is my model class tag belongs_to :user belongs_to :item end The corresponding DB table has the necessary :user_id and :item_id…
Don Leatham
  • 2,694
  • 4
  • 29
  • 41
8
votes
2 answers

How to add parameter to delete link?

I have a standard delete link, and want to add a parameter to it: <%= link_to "Delete", item, :confirm => 'Are you sure?', :method => :delete, :foo => 1 %> The parameter shows up in the html a tag, but does not make to the server. I get "undefined…
B Seven
  • 44,484
  • 66
  • 240
  • 385
8
votes
2 answers

Rails 2 to Rails 3 : using link_to instead of link_to_remote (including remote and update)

A quick and easy answer I'm sure. I'm upgrading a Rails project from version 2 to version 3 and replacing a load of the link_to_remote with link_to's as per the Rails 3 update. Even something as simple as : <%= link_to "Check Time", …
detheridge02
  • 640
  • 1
  • 6
  • 18
8
votes
4 answers

link_to with jquery params in rails

I want to make an in-place search in my rails app. I used button_to_remote with prototype, but now I'm using JQuery, so I changed to link_to. This is my code: <%= link_to "Search", {:action => "geocode", :with =>…
pasine
  • 11,311
  • 10
  • 49
  • 81
8
votes
2 answers

How to add a link_to in a select_tag with Rails/ERB?

I have the following code in my index.html.erb <%= select_tag 'team_id', options_for_select(@teams.map{|team| ["#{team.name} # {team.nick}", team.id] }) %> Where would I add a link_to helper within that block? Can I even add a link_to for…
asing
  • 385
  • 2
  • 7
  • 16
8
votes
2 answers

Rails: How can I show a block with or without a link based on a condition (link_to_if)

I have a complex block of tags (

,

, ...) that I want to render with a link or without a link around it based on a condition. I know about link_to_if that works like that: <% link_to_if condition, name, path %> if the condition is false only…

DiegoFrings
  • 3,043
  • 3
  • 26
  • 30
8
votes
3 answers

Call jQuery function from Rails link_to

I have a ruby loop that creates a list of comments.. I wonder if I can attach jQuery function to Rails link_to helper in this case? <% @video.phrases.each do |phrase| %>
<%= phrase.content %>
Stpn
  • 6,202
  • 7
  • 47
  • 94