html inside link_to rails is not working - ruby-on-rails-3

I´m new with ruby and I´m trying to insert an awesome-font icon inside a link using rails 3.2.15
This is my code
<%= link_to tweet_path(tweet), method => :delete, :confirm => 'Are you sure?' do %>
<i class="icon-trash"></i>
<% end %>
<%= link_to edit_tweet_path(tweet) do %>
<i class="icon-pencil"></i>
<% end %>
I don´t know what I'm doing wrong.
Thank you in advance.

You can do this:
<%= link_to raw('<i class="icon-trash"></i>'), tweet_path(tweet), method => :delete, :confirm => 'Are you sure?' %>

you can do this:
<%= link_to content_tag(:i,nil, :class => "icon-trash"), tweet_path(tweet), method => :delete, :confirm => 'Are you sure?' %>
<%= link_to content_tag(:i,nil, :class => "icon-pencil"), edit_tweet_path(tweet) %>
hope this helps you

Related

Rails link_to in templates

When I add a link_to to a pre-made bootstrap template it ends up putting the link below the nav-icon. I'm not sure if this is a CSS problem or if my code is wrong.
<li><i class="icon-off icon-white"></i><%= link_to('Logout', destroy_user_session_path, :method => :delete) %></li>
Try this
<%= link_to("<i class='icon-off icon-white'></i> Logout".html_safe, destroy_user_session_path, :method => :delete) %>

Routing error - passing a parameter to update action from link_to

I have a booking model, I want the user to confirm/reject a booking by clicking on one link or the other. I'm trying to avoid having to create a form.
here is how I tried implementing it:
<% #bookings.each do |booking| %>
<%= link_to "Confirm", appointment_booking_path(#appointment, booking, :status => "confirmed"), :method => :put, confirm: 'Are you sure?', :class => 'btn btn-success' %>
<%= link_to "Reject", appointment_booking_path(#appointment, booking, :status => "rejected"), :method => :put, confirm: 'Are you sure?',:class => 'btn btn-danger' %>
<%end%>
I am getting this error upon clicking on one of the links:
NoMethodError in BookingsController#update
undefined method `[]' for nil:NilClass
app/controllers/bookings_controller.rb:64:in `update'
Bookings is nested within appointments. Here is my rake Routes:
PUT /appointments/:appointment_id/bookings/:id(.:format)
appointment_booking GET /appointments/:appointment_id/bookings/:id(.:format)
edit_appointment_booking GET /appointments/:appointment_id/bookings/:id/edit(.:format)
BookingController Update action:
line 64* #appointment = Appointment.find(params[:booking][:appointment_id])
#booking = Booking.find(params[:id])
Let me know if you need any more information. Thank you in advance.
Look carefully at your rake routes, and you'll see it's appointment_booking and not appointment_bookings (i.e. no s at the end of bookings).
You'll also need to pass in the appointment instance as the first argument. Assuming your appointment is called #appointment, try this:
<% #bookings.each do |booking| %>
<%= link_to "Confirm", appointment_booking_path(#appointment, booking, :status => "confirmed"), :method => :put, confirm: 'Are you sure?', :class => 'btn btn-success' %>
<%= link_to "Reject", appointment_booking_path(#appointment, booking, :status => "rejected"), :method => :put, confirm: 'Are you sure?',:class => 'btn btn-danger' %>
<% end %>

Moving this action that works in the show view into the index view (Rails)?

This is how the user votes the current post that it is showing:
controllers/votes_controller.rb:
class VotesController < ApplicationController
def vote_up
#post = Post.find(params[:id])
if #post.votes.exists?(:user_id => current_user.id)
#notice = 'You already voted'
else
#vote = #post.votes.create(:user_id => current_user.id, :polarity => 1)
end
respond_to do |format|
format.js
end
end
This updates the total number of votes with Ajax:
vote_up.js.erb:
<% unless #notice.blank? %>
alert("<%= #notice %>");
<% end %>
<% unless #vote.blank? %>
$('.post-<%=#post.id%> span.vote-count').html('<%= #post.votes.count %>');
$('.post-<%=#post.id%> div.voted-user').html('<% #post.votes.each do |vote| %><%= link_to vote.user.username, vote.user %><% end %>');
<% end %>
This is the voting link in the show view:
views/posts/show.html.erb:
<%= link_to "Vote Up", vote_up_path(#post), :remote => true, :class => "vote-up" %><br />
and the routing for the action:
routes.rb:
get 'votes/:id/vote_up' => 'votes#vote_up', as: 'vote_up'
This is how all the posts are displayed in the index view:
views/posts/index.html.erb:
<% #posts.each do |post| %>
<h2>Title: <%= post.title %></h2>
<p>Author: <%= post.user.username %></p>
<p>Created At: <%= post.created_at %></p>
<p>Content: <%= post.content %></p>
<p>Votes: <%= post.total_votes %></p>
<p>Comments: <%= post.comments_count %></p>
<ul>
<li><%= link_to 'Show', post %></li>
<li><%= link_to 'Edit', edit_post_path(post) %></li>
<li><%= link_to 'Destroy', post, confirm: 'Are you sure?', method: :delete %></li>
</ul>
<br />
<% end %>
I would like to add the voting link in the index.html.erb view for each post or find a way of triggering the vote_up action for each post in the index view. Any suggestions to accomplish that?
I think I must be totally missing the point here but why can't you just do the following within the block displaying each post?
<%= link_to "Vote Up", vote_up_path(post), :remote => true, :class => "vote-up" %>
Note that i'm using post not #post.

Rails is not creating hidden field for put method on an update form

I have a REST resource called Greetings.
Here is my routes file:
resources :greetings
I have a form to create or update a greeting as follows:
<%= form_tag(#greeting, :remote => true, :id => 'greeting_form') do %>
<%= text_area :greeting, :content, :rows => 3, :placeholder => "Type your message..." %>
<% end %>
note: I am using form_tag because this form also collects user data.
This is supposed to create a hidden field with method=> put but its not so it can't find the route.
Any ideas how I can get this to submit to the update action?
Just write
<%= form_tag(#greeting, :remote => true, :id => 'greeting_form', :method => :put) do %>
and everything should be working.
You can use form_for tag and still collect user data like this:
<%= form_for #greeting, :validate => true do |f| %>
<%= f.text_area :content, :rows => 3, :placeholder => "Type your message..." %>
<%= f.fields_for #user do |u| %>
<%= u.text_field :name %>
<% end %>
<% end

in rails 3.0 I want to use a partial view in a show and in an edit, for edit I want to show the link to delete

<%= question.name %>
Created <%= time_ago_in_words(question.created_at) %> ago.
<% if :action == "edit" then %>
<%= link_to "delete", question, :method => :delete,
:confirm => "You sure?",
:title => question.name %>
<% end %>
Use action_name like so:
<% if action_name == 'edit' %>
<%= link_to 'delete', question, :method => :delete, :confirm => 'You sure?', :title => question.name %>
<% end %>