ruby on rails wrap block of code in link_to - html

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%>

Related

How to embed additional code in link_to

I'm trying to embed additional code in a link_to but not sure how to get it to work. How do I go about doing that? I'm trying to get this to work:
<%= link_to image_tag("Favorite 3.png", class: "act_actions", title: "Unfavorite", alt: "Unfavorite") + <%= activity.votes.size %>, favorite_activity_path(activity), method: :put, :remote => true, :class => "btn favorite" %>
I would try to use linkt_to as block to gain readability
<%= link_to favorite_activity_path(activity), method: :put, :remote => true, :class => "btn favorite" do %>
<%= image_tag("Favorite 3.png", class: "act_actions", title: "Unfavorite", alt: "Unfavorite") %>
<%= activity.votes.size %>,
<% end %>
I hope that the above code works for you.
you can do it this way also.
<%= link_to "#{image_tag('Favorite 3.png', class: 'act_actions', title: 'Unfavorite', alt: 'Unfavorite')} #{activity.votes.size}".html_safe, favorite_activity_path(activity), method: :put, :remote => true, :class => "btn favorite" %>

Ruby on Rails link_to

I am using link_to with some HTML elements and this is what I have :
<% link_to "", { :controller => "posts" }, :id => "posts", :class => "read-more" %>
But I want it that it will link to the posts with the id that each post has, any help would be most appreciated.
Thank You
config/routes.rb
resources :posts
<% #posts.each do |post| %>
<%= link_to "View post", post_path(post), :id => "posts", :class => "read-more" %>
<% end %>
Your hash is missing a few params...
<% link_to "", { :controller => "posts", :action => "show", :id => post.id}, :id => "posts", :class => "read-more" %>
But I recommend
<% link_to "", post_path(post), :id => "posts", :class => "read-more" %>
I think what you are asking is this. You have an array or active record relation #posts which you want to show in a webpage with links to each post. If I'm right you can do
<% #posts.each do |post| %>
<%= link_to "", { :controller => "posts" }, :id => post.id, :class => "read-more" %>br>
<% end %>
But you have to specify the action which will be triggered when the link is clicked.
I think this should work
<% #posts.each do |post| %>
<%= link_to "", { :controller => "posts" , :action => :show}, :id => post.id, :class => "read-more" %>br>
<% end %>
How about this?
<% #posts.each do |post| %>
<%= link_to "POST", post_path(post), :id => post.id %>
<% end %>

Simple_form how to make accept terms checkbox inline

<p><%= f.input :terms, :as => :boolean, :label => false, :boolean_style => :inline %>
Accept <%= link_to "Terms of use", terms_path,:remote => true %>
and <%=link_to "privacy Policy", privacy_path, :remote => true%></p>
It ends up looking like this
What is the best way to line them up on the same line.
Here's a rather simple way:
<%= content_for(:the_links) do %>
Accept <%= link_to "Terms of use", terms_path,:remote => true %>
and <%=link_to "privacy Policy", privacy_path, :remote => true%>
<% end %>
<%= simple_form_for #user do |f| %>
<%= f.input :email %>
<%= f.input :password %>
<%= f.input :terms, :as => :boolean, :label => content_for(:the_links)%>
<% end%>
Ensure the checkbox and text are small enough to fit in one row inside the container, then set display: inline; or float:left;
Try using wrapper_html like this:
<p>
<%= f.input :terms,
:as => :boolean,
:label => false,
:boolean_style => :inline,
:wrapper_html => { :style => 'display: inline' }
%>
Accept <%= link_to "Terms of use", terms_path,:remote => true %>
and <%=link_to "privacy Policy", privacy_path, :remote => true%>
</p>

Adding data- attribute to embedded Ruby

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.

Combining link_to with render :partial

I would like to combine 2 commands into 1, if its possible.
<%= render :partial => 'shared/logo' %>
<%= link_to 'Dashboard', root_url %>
I would like to call the logo in the shared directory and have it be a link at the same time.
How would I write this?
This should do it:
<%= link_to render(:partial => 'shared/logo'), root_url %>