I have a comment model that posts under a micropost both are paginated and both show on the same page.
I have done everything from http://railscasts.com/episodes/174-pagination-with-ajax?view=asciicast and everything should work but the problem is that both micropost and commet are paginated and both are on the same page.
The links for both pagination turns into this href="/users/2?page=2" rather than href="/users/2/micropost?page=2" or href="/users/2/comment?page=2". I am unsure how to go about solving this problem. Here are some of my code. All suggestions are much appreciated!
Micropost Render HTML
<table class="microposts">
<% if microposts.any? %>
<%= render microposts %>
<%= will_paginate microposts, :page_links => false %>
<% else %>
<div class="EmptyContainer"><span class='Empty'>Add a thread!</span></div>
<% end %>
</table>
Comment Section HTML
<div id='CommentContainer-<%= micropost.id%>' class='CommentContainer Condensed2'>
<div class='Comment'>
<%= render :partial => "comments/form", :locals => { :micropost => micropost } %>
</div>
<div id='comments'>
<% comments = micropost.comments.paginate(:per_page => 5, :page => params[:page]) %>
<%= render comments %>
<%= will_paginate comments, :class =>"pagination" %>
</div>
</div>
User Controller for the Show Page
def show
#user = User.find(params[:id])
#comment = Comment.find(params[:id])
#micropost = Micropost.new
#comment = Comment.new
#comment = #micropost.comments.build(params[:comment])
#comments = #micropost.comments.paginate(:page => params[:page], :per_page => 5)
#microposts = #user.microposts.order('created_at DESC').paginate(:per_page => 10, :page => params[:page])
respond_to do |format|
format.html
format.js
end
end
to separate the two, you should go here
http://blog.devinterface.com/2011/08/tips-multiple-pagination-with-will_paginate/
you can pass in separate params, so that only one will paginate even if both are on the same page
Related
I am able to update the like and unlike button via ajax, but I am not able to update the like count.
Post has_many likes
likes_controller.rb
def create
#like = Like.new(:post_id => params[:post_to_be_liked])
if #like.save
respond_to do |format|
format.html {redirect_to :back, notice: "Liked!"}
format.js
end
end
end
view.html.erb
<% #post = user.posts.first %>
<div id="send_like_for_<%= #post.id %>">
<% if #post.is_liked?(#post, current_user)%>
<%= render 'unlike'%>
<%else%>
<%= render 'like'%>
<%end%>
</div>
<div id="liked_<%= #post.id %>" %>
<%= render 'liked_users'%>
</div>
create.js.erb
$("#send_like_for_<%= params[:post_to_be_liked]%>").html("<%= escape_javascript(render('users/unlike')) %>")
$("#liked_<%= params[:post_to_be_liked]%>").html("<%= escape_javascript(render('users/liked_users')) %>")
_like.html.erb
<%= link_to "LIKE", likes_path(:post_to_be_liked => #post.id), remote: true, :method=>:post %>
_unlike.html.erb
<span> UNLIKE </span>
_liked_users.html.erb
<%= "#{#post.likes.length} like this" %>
The like/unlike functionality works just fine and when someone likes the post the button changes to Unlike. But the like count doesn't increase.
But why are you doing this in your view?
<% #post = user.posts.first %>
It will update only your first post always. I can't understand.
Try with this:
_liked_users.html.erb
<%= #updated_post = Post.where(id: #post.id) %>
<%= "#{#updated_post.likes.length} like this" %>
or
<%= #post.reload %>
<%= "#{#post.likes.length} like this" %>
Edit (Try to build your like this):
def create
#post = Post.where(id: params[:post_to_be_liked])
#like = #post.likes.build(:post_id => #post.id)
if #like.save
respond_to do |format|
format.html {redirect_to :back, notice: "Liked!"}
format.js
end
end
end
I think you should try to do this
#post = user.posts.first
after save in your controller and remove <% #post = user.posts.first %> from erb file and if it is not possible then initialize #post in your controller#create
#post = Post.find(params[:post_to_be_liked])
Controller:
class PostsController < ApplicationController
def index
#posts = Post.published
respond_to do |format|
format.html # index.html.erb
format.json { render json: #posts }
end
end
def show
.
.
end
def month
#posts_by_month = Post.find(:all, :order => "created_at DESC").group_by { |post| post.created_at.strftime("%B %Y") }
respond_to do |format|
format.html # index.html.erb
format.json { render json: #posts }
end
end
end
posts#month View:
<% #posts_by_month.each do |monthname, posts| %>
<p><%= monthname %></p>
<div>
<ul>
<% posts.each do |post| %>
<li><p><%= post.title %></p></li>
<% end %>
</ul>
</div>
<% end %>
posts#index view:
<h1>Listing posts</h1>
<%= render :partial => #posts %>
<h2>Blog archive</h2>
<%= ?I want link to single months archive here? %>
I'm creating a blog in rails and I thought I would add an archive section that you commonly see in the sidebar of many blogs. When I navigate to the posts#month view it displays the month as a heading and lists all the posts made during that month.
What I want to do now is have a list of months that posts where made on the posts#index view with each month linked to the posts#month view described above.
I'm not sure what to put on the posts#index view to accomplish this. Any ideas on what to put there or a better way to implement this would be great.
Any help appreciated!
I am always do it by this way for grouping:
#posts_by_month = Post.find(:all, :order => "created_at DESC").group_by { |post| post.created_at.beginning_of_month }
Then create for example posts/_post_archive.html.erb:
<div id="post-archive">
<% #posts_by_month.each do |month, posts| %>
<h4><%= "#{month.strftime('%B %Y')} (#{posts.count}):" %></h4>
<ul>
<% for post in posts %>
<li><%= link_to post.title, post %></li>
<% end %>
</ul>
<% end %>
</div>
And where it will be needed write this <%= render :partial => "posts/post_archive" %>
UPDATE:
In your controller create action:
def by_year_and_month
#posts = Post.where("YEAR(created_at) = ? AND MONTH(created_at) = ? ", params[:year], params[:month]).order("created_at DESC")
end
In your routes.rb:
match 'posts/by_year_and_month/:year/:month' => 'posts#by_year_and_month', :as=> :posts_by_year_and_month
And modifying our posts/_posts_archive.html.erb:
<h4><%= link_to "#{month.strftime('%B %Y')} (#{posts.count}):", posts_by_year_and_month_path(:year=> month.year, :month => month.month) %></h4>
In case someone try this for Postgres, the above method will not work.
def by_year_and_month
#posts = Post.where("YEAR(created_at) = ? AND MONTH(created_at) = ? ", params[:year], params[:month]).order("created_at DESC")
end
so you can do it like this,
def by_year_and_month
#posts = Post.where('extract(year from created_at) = ?', params[:year]).where('extract(month from created_at) = ?', params[:month]).order("created_at DESC")
end
Hope this will help someone.
In PostsController
def index
#posts = Post.published
#grouped_posts = Post.select("COUNT( * ) AS posts, MONTHNAME( created_at ) AS MONTH , YEAR( created_at ) AS YEAR").group("MONTH, YEAR").order("YEAR, MONTH( created_at )")
respond_to do |format|
format.html # index.html.erb
format.json { render json: #posts }
end
end
and then in view index.html.erb
<h1>Listing posts</h1>
<%grouped_posts.each do |rec|%>
<%=link_to "#{rec.month} #{rec.year} (#{rec.posts})", {:controller => "posts", :action => "month", :month => rec.month, :year => rec.year} %>
<%end%>
<%= render :partial => #posts %>
<h2>Blog archive</h2>
<%= ?I want link to single months archive here? %>
And then change the month action in posts_controller to accept two parameters params[:month] & params[:year]
Hope this will help
When I add a model in my index action, the create action is invoked that adds an instance of the model to the database. This is the following create action:
tracks_controller.rb
def create
#track = Track.new(params[:track])
if #track.save
redirect_to(root_url) //Want to change this!
else
#tracks = Track.all
render :action=>"index"
end
end
Where you can see that I am redirected to my root url (where I want to be) everytime create is invoked. However, how can I carry this out without refreshing the page? Since tracks are being played, I do not want the page to be refreshed whenever something is added to the database.
If I change this line to render :action=>"index", then I receive the following error in my index.html.erb file
undefined methodeach' for nil:NilClass`
15: <p>Database is empty!</p>
16: <%else%>
17: <br>
18: <% #tracks.each do |track| %>
19: <div id="list_container">
20: <ul>
21: <li class="list_container">
How do I go about achieving this?
form
<%= form_for #track, remote: true %>
form fields
<%= f.submit %>
<% end %>
app/views/tracks/create.js.erb
<% if #track.valid? %>
$(".tracks").prepend('<%= j(render(#track)) %>'); // make sure you have _track.html.erb
$("ID OR CLASS OF YOUR FORM")[0].reset(); // this will clear your form inputs
<% else %>
alert('Something Went Wrong');
<% end %>
app/views/tracks/index.html.erb
<div class="tracks">
<% #tracks.each do |track| %>
<%= render :partial => 'track' %>
<% end %>
</div>
app/views/tracks/_track.html.erb
some code here to show track:
<div>track.id</div>
<div>track.name</div>
index.html.erb needs the #tracks variable. Therefore, you have to set it before rendering the page.
For example:
def create
#track = Track.create(params[:track])
#tracks = Track.all
render :action => "index"
end
The simplest, quickest way to get it is to add :remote => true to you form_for!
If you are posting track from a form then make the remote to true
like
<%=form_for #track, :remote => true do |f| %>
your input fields
<%end %>
index.html.erb
<div id="track_list">
<%= render :partial => 'tracks_record' %>
</div>
_tracks_record.html.erb
<% #tracks.each do |track|%>
your code
<% end %>
in controller
def create
#track = Track.create(params[:track])
#tracks = Track.all
respond_to do |format|
format.js
end
end
Create one js.erb file for create.js.erb
<% if #track.valid? %>
('#track_list').html('<%= escape_javascript( render :partial => "tracks_record" ) %>');
<% else %>
alert('Could not save');
<% end %>
This will refresh the data and populate the new entries without refreshing the page.
Unfortunately I can't comment on Debadatt's suggestion but I found it much better than the top post. My only notes are to change this to
def create
#track = Track.create(params[:track])
#tracks = Track.all
respond_to do |format|
format.js
end
end
to this:
def create
#track = Track.create(params[:track])
#tracks = Track.all
respond_to do |format|
format.js { render layout: false, content_type: 'text/javascript' }
end
end
And this:
('#track_list').html('<%= escape_javascript( render :partial => "tracks_record" ) %>');
to this:
$('#track_list').html('<%= escape_javascript( render :partial => "tracks_record" ) %>');
Great suggestion thanks Debadatt
I have a comment model that posts under a micropost like facebook. The problem is that I don't think I am writing the right code under the user show in the user controller and that is what is making this error pop up. Any suggestions? All help is much appreciated!
Error
NoMethodError in Users#show
Showing /Users/Brian/rails_projects/stateschool/app/views/microposts/_micropost.html.erb where line #70 raised:
undefined method `total_pages' for #<ActiveRecord::Relation:0x007fc8c2f83468>
Extracted source (around line #70):
67: <%= render :partial => "comments/form", :locals => { :micropost => micropost } %>
68: </div>
69: <div id='comments'>
70: <%= will_paginate micropost.comments, :class =>"pagination" %>
71: </div>
72: </div>
73:
This is my current user show page:
User Controller
class UsersController < ApplicationController
def show
#user = User.find(params[:id])
#school = School.find(params[:id])
#micropost = Micropost.new
#comment = Comment.new
#comment = #micropost.comments.build(params[:comment])
#comments = #micropost.comments.paginate(:page => params[:page], :per_page => 10)
#microposts = #user.microposts.paginate(:per_page => 10, :page => params[:page])
end
end
Thank you any suggestions are welcomed!
EDIT
<div id='comments'>
<%=render micropost.comments %>
<%= will_paginate #comments, :class =>"pagination" %>
</div>
try doing
<%= will_paginate #comments, :class =>"pagination" %>
I have a table of venues and offers. Each venue can have have many offers.
I would like to be able to add the offers to the venues from the venues edit page. So far I have this (code below) but its giving a "NoMethodError in Venues#edit, undefined method `model_name' for NilClass:Class" error.
venues edit page
(the div id="tabs-3" is a container in an accordion)
<div id="tabs-3">
<%= form_for [#venue, #offer] do |f| %>
<h2 class="venue_show_orange">Offers</h2>
<%= f.fields_for :offers do |offer| %>
<div class="add_offer">
<%= offer.text_field :title %><br>
</div>
<div class="button"><%= submit_tag %></div>
<% end %>
<% end %>
</div>
offers controller
class OffersController < ApplicationController
def new
#offer = Offer.new
end
def create
#offer = #venue.offers.create!(params[:offer])
#offer.venue = #venue
if #offer.save
flash[:notice] = 'Offer added'
redirect_to offers_path
else
render :action => :new
end
end
def edit
#offer = Offer.find(params[:id])
end
def update
#offer = Offer.find(params[:id])
#offer.attributes = params[:offer]
if #offer.save!
flash[:notice] = 'Offer updated successfully'
redirect_to offers_path(#offer)
end
end
end
venues controller
(nothing offer related in here - is this where I'm going wrong?)
class VenuesController < ApplicationController
protect_from_forgery :only => [:update, :delete, :create]
load_and_authorize_resource
def new
#venue = Venue.new
5.times { #venue.venuephotos.build }
end
def create
#venue = Venue.new params[:venue]
if #venue.save
flash[:notice] = 'Venue added'
redirect_to venues_path
else
render :action => :new
end
end
def edit
#venue = Venue.find(params[:id])
5.times { #venue.venuephotos.build }
end
def update
#venue = Venue.find(params[:id])
#venue.attributes = params[:venue]
if #venue.save!
flash[:notice] = 'Venue updated successfully'
redirect_to :back
end
end
end
Any help is much appreciated thanks very much!
edit
venues edit page
<div id="tabs-3">
<%= form_for #venue do |f| %>
<div class="edit_venue_details">
<h2 class="venue_show_orange">Offers</h2>
<%= render :partial => 'offers/offer', :collection => #venue.offers %>
<div class="clearall"></div>
<h2 class="edit_venue_sub_header">Add a new offer</h2>
<%= f.fields_for :offers do |offer| %>
<% if offer.object.new_record? %>
<p class="edit_venue">title: <br>
<%= offer.text_field :title, :class => "edit_venue_input" %></p>
<% end %>
<% end %>
</div>
<button class="submit_button" type="submit"> Save changes</button>
<% end %>
</div>
whats being displayed
however if I add a new offer, that will display correctly:
Some remarks:
1) Replace:
<%= form_for [#venue, #offer] do |f| %>
with:
<%= form_for #venue do |f| %>
Because offers data will be updated through the related venue, only one controller action will handle the form.
2) If you want to add some unexisting offers in this form, you shoud instantiate them the way you did with venuephotos
3) Show your Venue model. You should have at least:
accepts_nested_attributes_for :offers
Fixed with:
<%= f.fields_for :offers, #venue.offers.build do |offer| %>