Ruby on Rails link_to - html

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

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

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

Rails ERB form fields rendered hidden

I have this form, and all of its element are rendered with the type="hidden", I don't know why this happens.
<%= form_for(:session, url: sessions_path) do |f| %>
<%f.text_field :username, :value => "Enter your user name", :class => "username-label" %>
<span class="email-icon"></span>
<% f.text_field :password, :value => "Enter your password", :class => "password-label" %>
<% f.submit "LOG IN", :class => "login-button" %>
<% end %>
With <% you are just executing the helpers. You need to use <%= ... %> to output the result of those helper calls:
<%= form_for(:session, url: sessions_path) do |f| %>
<%= f.text_field :username, :value => "Enter your user name", :class => "username-label" %>
<span class="email-icon"></span>
<%= f.text_field :password, :value => "Enter your password", :class => "password-label" %>
<%= f.submit "LOG IN", :class => "login-button" %>
<% 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 %>

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>