Adding data- attribute to embedded Ruby - html

I want to add this attribute:
data-show-url="<%= video_path(#video) %>"
to the HTML that this embedded Ruby produces:
<%= link_to 'video', video.video_url, :class => 'oembed' %>
What do I do in the embedded ruby?

<%= link_to 'video', video.video_url, :class => 'oembed', :'data-show-url' => video_path(#video) %>

If it is a parameter you want to add, you can simply pass it like this:
<%= link_to 'video', video.video_url, :class => 'oembed', :data-show-url => video_path(#video) %>
You will get it as params[:data-show-url] and the link will be appended with ?data-show-url=value.

Related

ruby on rails wrap block of code in link_to

Hello I have this link_to and I can't figure out how to wrap a block of html code in it such that the div inside the link_to becomes a link. I have this code:
<%= link_to #favorites[0].name, {:controller => "events", :action => "search", :category => #favorites[0].name} %>
I have tried using "do" then <%end%> but I can't make it work.
<%= link_to #favorites[0].name, {:controller => "events", :action => "search", :category => #favorites[0].name} do %>
<div> CODE HERE</div>
<%end%>
Any ideas on how to do this? I do not understand the syntax.
You need to give only path with link_to when using the block.
<%= link_to({:controller => "events", :action => "search", :category => #favorites[0].name}) do %>
<div>
<%= #favorites[0].name %>
</div>
<%end%>

Rails collection_select custom name attribute

I have the following collection select which acts as a filter in a Rails app.
<%= form_tag( "/appointments", :method => "get", :id => "filter_form") do %>
<%= collection_select :doctor, :id, #doctors, :id, :full_name, {:include_blank => 'All'} %>
<% end %>
This always generates a name attribute of the select element like name="doctor[id]" which results in the browser to ?utf8=✓&doctor%5Bid%5D=1, which is not quite readable.
How can I change the name attribute to just name = "doctor" or basically just remove the brackets from it?
http://api.rubyonrails.org/classes/ActionView/Helpers/FormOptionsHelper.html#method-i-collection_select
The collection_select method contains the parameters "options" and "html_options". "options" allow you to add specific information, like {:include_blank => 'All'}, but does not replace html attributes.
You have to add the name to the next hash, like this:
<%= form_tag( "/appointments", :method => "get", :id => "filter_form") do %>
<%= collection_select :doctor, :id, #doctors, :id, :full_name, {:include_blank => 'All'}, {:name => 'doctor'} %>
<% end %>
Have you tried:
<%= form_tag( "/appointments", :method => "get", :id => "filter_form") do %>
<%= collection_select :doctor, :id, #doctors, :id, :full_name, {:include_blank => 'All', :name => 'doctor'} %>
<% end %>

link_to_unless_current assign class to disabled (current) link

Anyone know if you can assign a tag and class to the disabled or current link? The example below only displays as plain text in the browser for the current link.
I have a bit of rails code displaying a list of buttons for each design in the database.
<% #id_cards.each do |id| %>
<%= link_to_unless_current id.design_type, id_card_design_path(id.id), :class => 'btn' %>
<% end %>
The active links are assigned the correct class and display as buttons.
link_to_unless_current accepts a block which can be used to override the default behavior.
http://api.rubyonrails.org/classes/ActionView/Helpers/UrlHelper.html#method-i-link_to_unless_current
<%=
link_to_unless_current("Comment", { :controller => "comments", :action => "new" }) do
link_to("Go back", { :controller => "posts", :action => "index" })
end
%>
In the example above it would yield the 'Go back' link if the current page was the 'new comment' page.
#James gave proper answer its just you are too young to take it right :)
<% #id_cards.each do |id| %>
<%=
link_to_unless_current(id.design_type, id_card_design_path(id.id), :class => 'btn') do
content_tag(:p, id.design_type, :class => :some_class
end
%>
<% end %>

Rails 3. how to use disable_with in button tag?

I recently started testing web_app_theme plugin. In Create button I have something like this...
<button class="button" type="submit">
<%= image_tag("web-app-theme/icons/tick.png", :alt => "#{t("web-app-theme.save", :default => "Save")}") %> <%= t("web-app-theme.save", :default => "Save") %>
</button>
How can I add :disable_with => "Processing" to this button to prevent duplicate records?
I tried t("web-app-theme.save", :default => "Save"), :disable_with => "Processing", but of course, that didn't work.
You need to use form tag helper methods - something like this should do it:
<%= button_tag :class => "button", :type => "submit", :data => { :disable_with => "Processing" } do %>
<%= image_tag("web-app-theme/icons/tick.png", :alt => "#{t("web-app-theme.save", :default => "Save")}") %>
<%= t("web-app-theme.save", :default => "Save") %>
<% end %>
Here's a link to the API docs for the button_to method: http://api.rubyonrails.org/classes/ActionView/Helpers/UrlHelper.html#method-i-button_to
Updated Dec 14, 2013 for Rails 3.2 & 4 syntax.
button_tag is entirely optional. You could just as easily edit the tag in html as follows:
<button class="button" type="submit" data-disable-with="Processing">
<%= image_tag("web-app-theme/icons/tick.png", :alt => "#{t("web-app-theme.save", :default => "Save")}") %> <%= t("web-app-theme.save", :default => "Save") %>
</button>
All that :disable_with does is add a data-disable-with attribute to the element. Then the jquery-rails gem's javascript (jquery_ujs.js) does the rest of the disabling work for you.
This is assuming, of course, that you're on rails 3.0 or higher.

Link with Name Attibute in Rails

I was wondering about how i can do this in rails
because i want something like
<a href="requests/13#new">Comment!<a>
anyone knows it?
greetings
<%= link_to "Comment!", url_for(:controller => "requests", :action => "show", :id => 13, :anchor => "new") %>
If you are working with a Request object and a restful route.
<%= link_to "Comment!", request_path(#request, :anchor => "new") %>
More details are available in the link_to helper documentation.