Rails 3 link_to partial structure - ruby-on-rails-3

I want to create a link_to render :partial. I have two partials stored already in the view folder in which I call the link_to (which is the profiles views). Getting the JavaScript to load the data is the next step but I am having trouble structuring the link_to properly.
Here's what I had:
<ul id="infoContainer">
<li><%= link_to render(:partial => "about") do %>About<% end %></li>
<li><%= link_to render(:partial => "other") do %>Other<% end %></li>
</ul>
Using these, both partials rendered in my show.html.erb and the links disappeared.
So I tried the following:
<ul id="infoContainer">
<li><%= link_to render(:partial => 'about'), :class => "a", :remote => true do %>About<% end %></li>
<li><%= link_to render(:partial => 'other'), :class => "o", :remote => true do %>Other<% end %></li>
</ul>
Both partials still show and the "About"/"Other" text links still don't show.
UPDATE: So I may have the link_to working. It's just not rendering anything. Gotta fix that with JavaScript I assume.
<li><%= link_to "About", (:partial => 'about'}, {:class => "a"}, :remote => true %></li>
<li><%= link_to "Other", (:partial => 'other'}, {:class => "o"}, :remote => true %></li>
Using the link_to above makes the URL: /profiles/1?partial=about

you're using link_to incorrectly -- please check the API / docs
it's called either:
link_to(body, url, html_options = {})
# url is a String; you can use URL helpers like
# posts_path
link_to(body, url_options = {}, html_options = {})
# url_options, except :confirm or :method,
# is passed to url_for
link_to(options = {}, html_options = {}) do
# name
end
link_to(url, html_options = {}) do
# name
end
See:
http://api.rubyonrails.org/classes/ActionView/Helpers/UrlHelper.html#method-i-link_to
http://apidock.com/rails/ActionView/Helpers/UrlHelper/link_to
and perhaps:
link_to syntax with rails3 (link_to_remote) and basic javascript not working in a rails3 app?

<ul id="infoContainer">
<li><%= link_to render(:partial => 'about'), urlyouwant %></li>
<li><%= link_to render(:partial => 'other'), urlyouwant %></li>
</ul>
If you want to run javascript when user click on, you can read more at http://api.rubyonrails.org/classes/ActionView/Helpers/JavaScriptHelper.html#method-i-link_to_function
<ul id="infoContainer">
<li><%= link_to_function render(:partial => 'about'), "alert('About!')" %></li>
<li><%= link_to_function render(:partial => 'other'), "alert('Other!')" %></li>
</ul>

Related

Rails - How do you make a nav link remain highlighted while on a specific page while still using application.html.erb to hold your navigation links?

In Ruby on Rails(5) how do you make a nav link remain highlighted while on a specific page while still using application.html.erb to hold your navigation links?
For example:
application.html.erb
<nav class="nav">
<li><%= link_to "Home", root_path, :class => 'na' %></li>
<li><%= link_to "Units", units_path, :class => 'ACTIVE' %></li>
</nav>
Maybe something simple like:
<%= link_to 'Units', units_path, class: "#{'active' if controller_name == 'units' && action_name == 'index'}" %>

html inside link_to rails is not working

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

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.