link_to with embedded ruby in the data-toggle - ruby-on-rails

I'm following this SE question to put a form in a bootstrap modal with rails.
The person who answered the question states: "Make sure that you have a unique id or class for every modal body.". So I am trying to put a unique id number in my link_to:
<%= link_to "Edit", edit_post_path(post.id), :class => "btn", :remote => true, "data-toggle" => "modal", "data-target" => "<%= post.id %>-my-modal" %>
But this is causing an error. If I take out <%= post.id %> I do not have an error, but the modal behavior does not work.
How can I add the post.id with embedded ruby to the link to?

You have to write it like this:
<%= link_to "Edit", edit_post_path(post.id), :class => "btn", :remote => true, "data-toggle" => "modal", "data-target" => "#{post.id}-my-modal" %>

Once you open a <% ... %> tag, you are writing ruby code. What this means is that you can't nest <% ... %> tags inside another <% ... %> tag because these tags aren't ruby syntax.
Inside the tag, to do string interpolation, use normal ruby methods:
<%= link_to "Edit", edit_post_path(post.id), :class => "btn", :remote => true, "data-toggle" => "modal", "data-target" => "#{post.id}-my-modal" %>

Only mistake is putting another (ruby) tag <% %> inside the same tag.
In erb (embedded ruby) one <%= %> tag cannot have another tag inside.
This code result a syntax error.
<%= link_to "Edit", .... "data-target" => "<%= post.id %>-my-modal" %>
If you plan to output/render data inside the tag and quoted as string,
used #{put_data_hare}
Output like:
<%= link_to "Edit", .... "data-target" => "#{post.id}-my-modal" %>
In other case, you can do like:
post.id.to_s + "-my-modal"

Related

Rails: link_to not rendering dynamic text

In my rails 4 erb view i have this link,
<%= link_to "Download #served_file.title", "/public/uploads/#served_file.file", :class => "btn" %>
However, it won't replace #served_object.title and #served_object.file with the text but just placing those lines inside a <%= %> works as expected.
You need to interpolate the values into the string.
<%= link_to "Download #{#served_file.title}", "/uploads/#{#served_file.file}", :class => "btn" %>

How to use bootstrap Modal with link_to in rails?

I had asked this earlier but unfortunately I am still stuck. I have a link_to like this
<%= link_to "Click Here", page_path,, :id => 'login', "data-toggle" => "modal" %>
in page.html.erb (the page which I want to load in modal when the link gets clicked),I have
<div class="modal" id="loginModal">
Test Content
</div>
in assets/page.js.coffee
$('#loginModal').modal(options)
but still, the link is not opening in a modal Any help ?
Add data-target
<%= link_to "Click Here", page_path, :id => 'login', "data-toggle" => "modal", 'data-target' => '#loginModal' %>
There is a prettier way to add data attributes in Rails. You can do something like this to get the same results.
<%= link_to 'Click Here', "#", data: {toggle: "modal", target: "#modal"} %>
Using Rails 7 with Bootstrap 5
<%= link_to 'Add user', '#', {:remote => true, 'data-bs-toggle' => "modal", 'data-bs-target' => '#exampleModal', class: 'nav-link'} %>

Rails : Linking an anchor from a different controller/view

<%= link_to (:controller => "company_stuff", :action => "index", :anchor => :menu), :class => 'links' do %>
<li>Terms of Use</li>
<% end %>
I am having difficulty linking a page which is on a different controller and also the link is an anchor. Basically the controller is called company_stuff the action is index and the anchor is called #terms
The problem was that the :controller :action :anchor was not being passed through as a hash, separate from the CSS class
Below is the solution
<%= link_to "Terms Of Use", {:controller => "company_stuff", :anchor => "terms"}, :class => "links" %>
I believe you can try something like this
<%= link_to index_company_stuff_path + "#terms", :class => 'links' do %>
<li>Terms of Use</li>
<% end %>
Or
<%= link_to index_company_stuffs_path + "#terms", :class => 'links' do %>
<li>Terms of Use</li>
<% end %>
Depending on your controller name and route.
You can find more information on this question How to create an anchor and redirect to this specific anchor in Ruby on Rails

Rails, how do you submit a form with a text link?

I am trying to get this form to submit correctly. Here's what I have so far:
<% form_for(:user, :url => update_user_setting_path, :remote => true, :html => {:method => :post, :class => "search_form general_form"}) do |f| %>
and the button renders with this code:
<li><%= link_to raw("<span class='button approve'><span><span>SAVE</span></span></span>"), :action => 'create' %></li>
I am using action create, is this correct?
Here is the rendered form tag:
<form method="post" data-remote="true" class="search_form general_form" action="/settings/2/update_user" accept-charset="UTF-8">
What am I missing? Thanks for your help!
No, you are not using link_to properly. You need to use a submit tag to submit your form, not a link_to tag, for example:
<% form_for(:user, :url => update_user_setting_path, :remote => true, :html => {:method => :post, :class => "search_form general_form"}) do |f| %>
...
<li><%= f.submit "Save" %></li>
If you want to use a text link you'll have to have javascript submit the form. For example, if you are using jQuery you could do the following:
<%= link_to 'Save', "#", :onclick=>"$('.search_form').submit()" %>
I like Pan's solution but I prefer to use the ID of the form directly which you can get from the dom_id(obj). The form_for helper also uses dom_id(obj) to assign the form's ID. This way you aren't dependent on setting classes by hand or subject to accidentally submitting more than one form that share the same CSS class. It looks a little stranger but I usually have a custom FormBuilder anyway so I just add a generic link_to_submit method to encapsulate this:
<%= link_to 'Save', "#", :onclick => "$('##{dom_id(#user)}').submit()" %>
You don't need to use an id or a selector if you have jquery, you can simply do :
= link_to 'Save', "#", onclick: "$(this).closest('form').submit()"
Thanks for the answers... I ended up using this and it works great:
<li><%= link_to raw("<span class='button approve'><span><span>SAVE</span></span></span>"), "index_users", :onclick=>"document.forms['form1'].submit();"%></li>

Rails: link_to image tag. how to add class to a tag

I am using link_to img tag like following
<%= link_to image_tag("Search.png", :border=>0, :class => 'dock-item'),
:action => 'search', :controller => 'pages'%><span>Search</span></a>
Which results in following html
<a href="/pages/search"><img alt="Search" border="0" class="dock-item"
src="/images/Search.png?1264132800" /></a><span>Search</span></a>
I want the class="dock-item" to go to the <a> tag instead of the img tag.
How can i change this?
Update:
<%= link_to image_tag("Search.png", :border=>0), :action => 'search',
:controller => 'pages', :class => 'dock-item' %>
results in
<a href="/pages/search?class=dock-item"><img alt="Search" border="0"
src="/images/Search.png?1264132800" /></a>
hi you can try doing this
link_to image_tag("Search.png", border: 0), {action: 'search', controller: 'pages'}, {class: 'dock-item'}
or even
link_to image_tag("Search.png", border: 0), {action: 'search', controller: 'pages'}, class: 'dock-item'
note that the position of the curly braces is very important, because if you miss them out, rails will assume they form a single hash parameters (read more about this here)
and according to the api for link_to:
link_to(name, options = {}, html_options = nil)
the first parameter is the string to be shown (or it can be an image_tag as well)
the second is the parameter for the url of the link
the last item is the optional parameter for declaring the html tag, e.g. class, onchange, etc.
hope it helps! =)
Just adding that you can pass the link_to method a block:
<%= link_to href: 'http://www.example.com/' do %>
<%= image_tag 'happyface.png', width: 136, height: 67, alt: 'a face that is unnervingly happy'%>
<% end %>
results in:
<a href="/?href=http%3A%2F%2Fhttp://www.example.com/k%2F">
<img alt="a face that is unnervingly happy" height="67" src="/assets/happyface.png" width="136">
</a>
This has been a life saver when the designer has given me complex links with fancy css3 roll-over effects.
Best will be:
link_to image_tag("Search.png", :border => 0, :alt => '', :title => ''), pages_search_path, :class => 'dock-item'
this is my solution:
<%= link_to root_path do %>
<%= image_tag "image.jpg", class: "some class here" %>
<% end %>
Easy:
<%= link_to image_tag("Search.png", :border=>0), :action => 'search', :controller => 'pages', :class => 'dock-item' %>
The first param of link_to is the text/html to link (inside the a tag). The next set of parameters is the url properties and the link attributes themselves.
I tried this too, and works very well:
<%= link_to home_index_path do %>
<div class='logo-container'>
<div class='logo'>
<%= image_tag('bar.ico') %>
</div>
<div class='brand' style='font-size: large;'>
.BAR
</div>
</div>
<% end %>
To respond to your updated question, according to http://api.rubyonrails.org/classes/ActionView/Helpers/UrlHelper.html...
Be careful when using the older argument style, as an extra literal hash is needed:
link_to "Articles", { :controller => "articles" }, :id => "news", :class => "article"
# => Articles
Leaving the hash off gives the wrong link:
link_to "WRONG!", :controller => "articles", :id => "news", :class => "article"
# => WRONG!
The whole :action =>, :controller => bit that I've seen around a lot didn't work for me.
Spent hours digging and this method definitely worked for me in a loop.
<%=link_to( image_tag(participant.user.profile_pic.url(:small)), user_path(participant.user), :class=>"work") %>
Ruby on Rails using link_to with image_tag
Also, I'm using Rails 4.
Hey guys this is a good way of link w/ image and has lot of props in case you want to css attribute for example replace "alt" or "title" etc.....also including a logical restriction (?)
<%= link_to image_tag("#{request.ssl? ? #image_domain_secure : #image_domain}/images/linkImage.png", {:alt=>"Alt title", :title=>"Link title"}) , "http://www.site.com"%>
Hope this helps!
<%= link_to root_path do %><%= image_tag("Search.png",:alt=>'Vivek',:title=>'Vivek',:class=>'dock-item')%><%= content_tag(:span, "Search").html_safe%><% end %>
You can also try this
<li><%= link_to "", application_welcome_path, class: "navbar-brand metas-logo" %></li>
Where "metas-logo" is a css class with a background image

Resources