Creating a link_to action for will_paginate + order_by parameters (Rails) - ruby-on-rails-3

I have a parameter in my comments called total_votes (composed of integers).
This is how I order comments by the most voted and apply will paginate at the same time:
show.rb:
def show
#post = Post.find(params[:id])
#comments = #post.comments.paginate(:page => params[:page],
:per_page => 5).order('total_votes DESC,
created_at DESC')
end
show.html.erb:
<% #comments.map do |comment| %>
<div class="comment-<%= comment.id %>">
<p>
<b>Comment:</b>
<%= comment.content %>
</p>
<p>
<b>Vote:</b>
<span class="vote-count"><%= comment.total_votes %></span>
<div class='voted-user'>
<% comment.votes.each do |vote| %>
<%= link_to vote.user.username, vote.user %>
<% end %>
</div>
</p>
<p>
<b>Commenter</b>
<%= link_to comment.user.username, comment.user %>
</p>
<p>
<b>Link</b>
<%= link_to "Show Post Comment", [#post, comment] %>
</p>
<p>
<b>Vote</b>
<%= link_to "Vote Up", vote_up_path(comment, :votable_type => "Comment"), :remote => true, :class => "vote-up" %><br />
</p>
</div>
<% end %>
<%= will_paginate #comments %>
I would like to create a link_to action so that I can manipulate this part:
#comments = #post.comments.paginate(:page => params[:page],
:per_page => 5).order('THIS_PART')
So that I can archive something like this:
mysite/posts/93?order_by=votes
and or this (order_by + will_paginate):
mysite/posts/93?order_by=votes&page=2
or something like that.
Any suggestions about how to accomplish code this in the controller and view (Please let me know if this is not the best way of doing it)?

EDIT:
This Railscasts is what you want!

Related

Bootstrap (2.3.1) + SimpleForm (2.1) inline label for radio button group

I'm trying to create inline radio buttons which also has its label inline (at the very left).
As I state in the title, using Bootstrap and SimpleForm.
My code looks like this:
<%= f.input :breakdown_or_size,
:collection => ["Breakdown", "Manual"],
:as => :radio_buttons,
:item_wrapper_class => 'inline',
:label => 'Select One: ' %>
This is what I get:
(source: webpagescreenshot.info)
I looked high and low for simple_form_for help. In the end I couldn't find anything, but at least with form_for you can do this:
<%= form_for(#book) do |f| %>
<% if #book.errors.any? %>
<div id="error_explanation">
<h2><%= pluralize(#book.errors.count, "error") %> prohibited this book from being saved:</h2>
<ul>
<% #book.errors.full_messages.each do |msg| %>
<li><%= msg %></li>
<% end %>
</ul>
</div>
<% end %>
<div class="actions", style='inline'>
<%= f.label 'Select One: ' %>
<br/>
<%= f.radio_button :collection, "Breakdown" %> Breakdown
<%= f.radio_button :collection, "Manual" %> Manual
<br/>
<%= f.submit %>
</div>
Which at least solves the inline rendering.

link_to current page rails

I was wondering what the syntax is to link to an attribute on the same page
I have a list of dates
<ul>
<% #response.each_pair do |date, movie| %>
<li><%= link_to date_format(date), date, :class => 'scroll_to' %></li>
<% end %>
</ul>
These then have movies underneath each date like so
<% #response.each_pair do |date, movie| %>
<h3 class="resultTitle fontSize13">Available on <%= date_format(date) %></h3>
<% movie.each do |m| %>
<div class="thumbnail clearfix">
<img class="pull-left" src=<% if m.image_link %> <%= m.image_link %> <% else %> "/assets/noimage.jpg" <% end %>>
<div class="caption pull-right">
<%= link_to m.name, m.title_id, :class => 'resultTitle fontSize11' %>
<p class="bio"><%= m.bio %></p>
<p class="resultTitle">Cast</p>
<p class="bio"><%= m.cast.join(", ") unless m.cast.empty? %></p>
<%= link_to "Remind me", reminders_path(:title_id => m.title_id), :method => :post, :class => 'links button' %>
</div>
</div>
<% end %>
<% end %>
What i would like to achieve is that when a user clicks on a date in the list then it will take them to that date with movies on the same page
My attribute for each date is
"release_date"
Do i need to link to that, maybe a piece of Jquery aswell to scroll down to that date? or would it jump to the date in one step?
Any advice appreciated, I have linked to other pages before but not the same page like this
EDIT
I have tried this but the page just re renders
<li><%= link_to date_format(date), params.merge(:release_date => 'release_date'), :class => 'scroll_to' %></li>
Am i on the right track?
Thanks
All you need is to give your date a unique identifier and link to that in you list. For instance below we give the h3 for each date an id relative to the date object. The browser knows how to handle internal links and will simply jump to the corresponding identifier. Notice how the id of the field you're linking to gets appended to the end of the url when you click on the link.
<ul>
<%- index = 0 %>
<% #response.each_pair do |date, movie| %>
<%- index += 1 %>
<li><%= link_to date_format(date), "##{index}", :class => 'scroll_to' %></li>
<% end %>
</ul>
<%- index = 0 %>
<% #response.each_pair do |date, movie| %>
<%- index += 1 %>
<h3 class="resultTitle fontSize13" id="<%= index %>">Available on <%= date_format(date) %></h3>
<% movie.each do |m| %>
<div class="thumbnail clearfix">
<img class="pull-left" src=<% if m.image_link %> <%= m.image_link %> <% else %> "/assets/noimage.jpg" <% end %>>
<div class="caption pull-right">
<%= link_to m.name, m.title_id, :class => 'resultTitle fontSize11' %>
<p class="bio"><%= m.bio %></p>
<p class="resultTitle">Cast</p>
<p class="bio"><%= m.cast.join(", ") unless m.cast.empty? %></p>
<%= link_to "Remind me", reminders_path(:title_id => m.title_id), :method => :post, :class => 'links button' %>
</div>
</div>
<% end %>
<% end %>
For the added JQuery transition you can replace
<li><%= link_to date_format(date), "##{index}", :class => 'scroll_to' %></li>
with
<li><%= link_to_function date_format(date), "$('html, body').animate({scrollTop:$('##{index}').offset().top }, 'slow');", :class => 'scroll_to'%></li>
The above isn't exactly a DRY approach, but the important thing to abstract out of that is that you're linking to a unique ID elsewhere on the page and not the code itself.

RoR: why aren't my microposts showing up?

Here is the users show view where they are supposed to show up. ..
edit (I have updated this post slightly you can see it at RoR: How can I get my microposts to show up?)
<section>
<div id= "purchases">
<%= render 'shared/micropost_form_purchase' %>
</div>
<div id="sales">
<%= render 'shared/micropost_form_sale' %>
</div>
</section>
<div id="purchases list">
<ol class="microposts">
<%= render #purchases unless #purchases.nil? %>
</ol>
</div>
<div id="sales list">
<ol class="microposts">
<%= render #sales unless #sales.nil? %>
</ol>
</div>
so the forms (partials) are loading fine, but then when I make a post, in either one, neither the purchases list nor the sales list shows up. I checked the database and they are being created along with an entry in the column indicating kind (either sale or purchase)
Here are the forms:
<%= form_for (#micropost) do |f| %>
<div class="field no-indent">
<%= f.text_area :content, placeholder: "What's something else you want to buy?" %>
<%= hidden_field_tag 'micropost[kind]', "purchase" %>
</div>
<%= f.submit "Post", class: "btn btn-large btn-primary" %>
<% end %>
and
<%= form_for (#micropost) do |f| %>
<div class="field no-indent">
<%= f.text_area :content, placeholder: "What's something else you want to buy?" %>
<%= hidden_field_tag 'micropost[kind]', "sale" %>
</div>
<%= f.submit "Post", class: "btn btn-large btn-primary" %>
<% end %>
also, here is the show part of the users_controller.rb
def show
#user = User.find(params[:id])
#micropost=Micropost.new
#microposts = #user.microposts.paginate(page: params[:page])
end
and here is the show part of the microposts_controller.rb
def show
#micropost = Micropost.find(params[:id])
#microposts = Micropost.where(:user_id => #user.id)
#purchases = #microposts.where(:kind => "purchase")
#sales = #microposts.where(:kind => "sale")
end
can anyone help me out? anything else i need? hmmm
First, just to be sure you are getting the results you want, you should try something like this in your view
<%= #sales %>
This should be a hash of the results you want. Then, if that looks good, you want to do something like this
<div id="sales_list">
<ol class="microposts">
<% if #sales.any? %>
<% #sales.each do |sale| %>
<li><%= sale %></li>
<% end %>
<% end %>
</ol>
</div>
And repeat for purchases

RoR: why am I getting undefined method 'hidden_field_tag'?

right now I have two forms in a row
<section>
<%= render 'shared/micropost_form_purchase' %>
<%= render 'shared/micropost_form_sale' %>
</section>
then for _micropost_form_purchase.html.erb
<%= form_for(#micropost) do |f| %>
<%= render 'shared/error_messages', object: f.object %>
<div class="field no-indent">
<%= f.text_area :content, placeholder: "What's something else you want to buy?" %>
<%= f.hidden_field_tag :type, :value => "purchase" %>
</div>
<%= f.submit "Post", class: "btn btn-large btn-primary" %>
<% end %>
and for _micropost_form_sale.html.erb I have
<%= form_for(#micropost, :html => { :id => "sale" }) do |f| %>
<%= render 'shared/error_messages', object: f.object %>
<div class="field no-indent">
<%= f.text_area :content, placeholder: "What's something else you want to buy?" %>
<%= f.hidden_field_tag :type, :value => "sale" %>
</div>
<%= f.submit "Post", class: "btn btn-large btn-primary" %>
<% end %>
so I want the first micro post to automatically become a purchase micropost (I have a column in the micropost database called type that is a string that I want to depict either sale or purchase) and for the second one I want it to become a sale micropost. I was using hidden_field_tag because I thought you didn't have to define it in the controller, but am I wrong? Is hidden_field more appropriate? how can I use hidden_field_tag?
You can use:
<%= f.hidden_field :type, :value => "sale" %>
or:
<%= hidden_field_tag 'micropost[type]', "sale" %>
but not:
<%= f.hidden_field_tag :type, :value => "sale" %>
Using f.hidden_field will use the value from the variable #micropost, whereas hidden_field_tag will not use that.
It should be f.hidden_field not f.hidden_field_tag as you're using the model's form helpers :)

Can't trigger this controller action in a view (it isn't a default Rails RESTful action)

I have a controller called votes_controller.rb. In that file there is the following action:
class VotesController < ApplicationController
def vote_up
#post = Post.find(params[:post_id])
vote_attr = params[:vote].merge :user_id => current_user.id, :polarity => 1
#vote = #post.votes.create(vote_attr)
end
(etc...)
I want to trigger the vote_up action in a view:
views/posts/show.html.erb::
<%= link_to "Vote Up", ??? %>
Here is the whole file just in case:
<h2>posts show</h2>
<span>Title: <%= #post.title %></span><br />
<span>Content: <%= #post.content %></span><br />
<span>User: <%= #post.user.username %></span><br />
<%= link_to "Vote Up", ??? %>
<h2>Comments</h2>
<% #post.comments.each do |comment| %>
<p>
<b>Comment:</b>
<%= comment.content %>
</p>
<p>
<b>Commenter</b>
<%= link_to comment.user.username, comment.user %>
</p>
<% end %>
<h2>Add a comment:</h2>
<%= form_for([#post, #post.comments.build]) do |f| %>
<div class="field">
<%= f.label :content %><br />
<%= f.text_area :content %>
</div>
<div class="actions">
<%= f.submit %>
</div>
<% end %>
<% if current_user.id == #post.user_id %>
<%= link_to 'Edit', edit_post_path(#post) %> |
<% end %>
<%= link_to 'Back', posts_path %>
I have no idea what to type in the ??? part (I would also like to make it work as :remote. My intention is to trigger the action without refreshing the page).
Do I have to add something in the routes.rb?
Any suggestions?
You have to define a route in routes.rb. Use a named route to be easy to use in the view. Something like:
get 'votes/:id/vote_up' => 'votes#vote_up', as: 'vote_up'
And so can now use in the view
<%= link_to "Vote Up", vote_up_path(#post) %>
and in the controller
def vote_up
#post = Post.find(params[:id])
...
end
See Rails routing