How to embed additional code in link_to - html

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

Related

How do I get select_tag to display text in front of the parameter?

I want to display "Year: " inside the 'year' dropbox shown in the image below. How do I append text to the parameter field?
I'm using a select_tag as shown below:
<%= form_tag results_path, :method => 'get' do %>
<%= select_tag :season_year, options_for_select(#tournaments.order(season_year: :desc).collect{ |u| [u.season_year] }.uniq, session[:season_year]), class: "btn btn-outline-primary", :onchange => "this.form.submit();" %>
<%= select_tag :tournament, options_for_select(#tournaments_filtered.order(tournament_date: :desc).collect{ |u| [u.lake_name, u.id] }, session[:tournament]), class: "btn btn-outline-primary", :onchange => "this.form.submit();" %>
<%= select_tag :table_select, options_for_select(#result_options, session[:table_select]), class: "btn btn-outline-primary", :onchange => "this.form.submit();" %>
<% end %>
You can use the include_blank option to add a dud option.
ie add include_blank: "Year:"
See docs for more details

Do Devise forms require a label for each textfield?

Does Devise require a label for each textfield in its forms (ie: registration/new)?
When i delete the label in my code, ie:"<%= f.label :name %>", the textfield also disappears on the page. I want to show the textfield, but not the label.
devise/registrations/new.html.erb
<div class="row">
<div class="col-md-8">
<%= form_for(resource, as: resource_name, url: registration_path(resource_name)) do |f| %>
<%= devise_error_messages! %>
<div class="form-group>
<%= f.label :name %>
<%= f.text_field :name, autofocus: true, class: 'form-control', placeholder: 'Name' %>
</div>
<div class="form-group>
<%= f.label :email %>
<%= f.text_field :email, class: 'form-control', placeholder: 'Email' %>
</div>
<div class="form-group>
<%= f.label :password %>
<%= f.text_field :password, class: 'form-control', placeholder: 'Password' %>
</div>
<div class="form-group>
<%= f.label :password_confirmation %>
<%= f.text_field :password_confirmation, class: 'form-control', placeholder: 'Confirm password' %>
</div>
<div class="form-group">
<%= f.submit "Sign up", class: 'btn btn-success' %>
</div>
<div class="form-group">
<%= render "devise/shared/links" %>
</div>
<% end %>
</div>
</div>
After deleting label
Before deleting label
I've read its bad practice to exclude a label outside of a a textfield in a form, but Facebook does it ;)
I'm still new to RoR and any guidance is appreciated.
Does Devise require a label for each textfield
No.
Devise is a gem.
It's built using the same helpers and code as every other Ruby/Rails app. It does not have any special requirements to use labels.
In fact, the form helpers of Rails, and indeed every other helper, outputs vanilla HTML. If you can get away with not having labels in html, you can do it with your form helper.
Here's our code for a devise registration form (in HAML):
#app/views/devise/registrations/new.haml
.authenticate
= render "devise/shared/title"
= simple_form_for(User.new, as: resource_name, url: registration_path(resource_name)) do |f|
- if flash[:error]
.form-errors= flash[:error]
= f.error_notification
.form-inputs
= f.input :email, required: true, autofocus: true, placeholder: "Email"
%hr
= f.input :password, required: true, placeholder: "Password"
= f.input :password_confirmation, required: true, placeholder: "Confirm"
.form-actions
.bar
= link_to "", "#", rel: "submit", class: "ion-forward", title: "Register", data: { placement: "right" }
= render "devise/shared/links"
You should try the following:
<%= form_for(resource, as: resource_name, url: registration_path(resource_name)) do |f| %>
<%= devise_error_messages! %>
<%= f.text_field :name, autofocus: true, class: 'form-control', placeholder: 'Name' %>
<%= f.text_field :email, class: 'form-control', placeholder: 'Email' %>
<%= f.text_field :password, class: 'form-control', placeholder: 'Password' %>
<%= f.text_field :password_confirmation, class: 'form-control', placeholder: 'Confirm password' %>
<%= f.submit "Sign up", class: 'btn btn-success' %>
<%= render "devise/shared/links" %>
<% end %>
Remove any HTML elements to make sure they're not preventing Rails from showing the elements properly. If it works, you'll be able to rebuild the HTML and CSS to see what the problem is.

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

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>