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" %>
Related
I am following the Railcast tutorial on endless page. I am receiving a Argument Error The #users variable appears to be empty. Did you forget to pass the collection object for will_paginate?
The javascript has been modified so you have to click a read more link to get more results. So the pagination will work by click instead of scrolling.
The error points to the line <%= will_paginate #questions %>
Questions Controller:
def index
#Before gem added it was `#questions = Question.all`
#questions = Question.order("question").page(params[:users]).per_page(2)
respond_with(#questions)
end
Users Controller:
def profile
#profile = User.profile
#user = User.find(params[:id])
#question = Question.where(recipient_id: params[:id]).first
end
def show
#user = User.find(params[:id])
#question = Question.where(recipient_id: params[:id])
end
View:
<% #question.select(&:answer?).each do |question| %>
Question: <%= question.question %>
<br>
Answer: <%= question.answer %><br><br>
<%= will_paginate #questions %>
<% end %>
Questions.js.coffee:
jQuery ->
if $('.pagination').length
$('#append_and_paginate').prepend('<a id="append_more_results" href="javascript:void(0);">Show more questions</a>');
$('#append_more_results').click ->
url = $('.pagination .next_page').attr('href')
if url
$('.pagination').text('Fetching more questions...')
$.getScript(url)
Index.js.erb:
$('#questions').append('<%= j render(#questions) %>');
<% if #questions.next_page %>
$('.pagination').replaceWith('<%= j will_paginate(#questions) %>');
<% else %>
$('#append_and_paginate').remove();
<% end %>
<% sleep 1 %>
Replace
#questions = Question.order("question").page(params[:users]).per_page(2)
With
#questions = Question.order("question").paginate(:page => params[:page], :per_page => 2)
EDIT
Update the User model as below:
class User < ActiveRecord::Base
has_many :sender_questions, :class_name => 'Question', :foreign_key => 'sender_id'
has_many :recipient_questions, :class_name => 'Question', :foreign_key => 'recipient_id'
end
Update the UsersController#show action as below:
def show
#user = User.find(params[:id])
#question = #user. recipient_questions.paginate(page: params[:page])
end
Ok so i am tring to list my genres in a f.select form and I am getting an error. I have look everywhere and i am just not understanding what i am doing differently. when I enter rails c and type g = Genre.all it lists all genres then g.map out puts => #<Enumerator: ...>
Error:
undefined method `map' for nil:NilClass
View Page:
<%= f.fields_for :genres do |g| %>
<div class="field">
<%= g.label :genre %>
<%= g.select :genres, #genres.map {|g| g.name} %>
</div>
<% end %>
Controller:
def create
#song = Song.new(params[:song])
#genres = Genre.all
if #song.save
redirect_to player_path
else
render :new
end
end
You need to assign #genres variable in new action too:
def new
#genres = Genre.all
end
I'm trying to create a partial template using <%= render "/shopping/coupons/cou" %> . Not really sure where went wrong. Thanks!
This is the error message.
undefined method `model_name' for NilClass:Class
Extracted source (around line #3):
1: <h4> Coupon </h4>
2:
3: <%= form_for(#coupon, :url => shopping_coupon_path(#coupon)) do |f| %>
4: <div class="field">
5: <%= f.label :code %>
6: <%= f.text_field :code %>
this is my coupons controller
class Shopping::CouponsController < Shopping::BaseController
def cou
form_info
end
def create
#coupon = Coupon.find_by_code(params[:coupon][:code])
if #coupon && #coupon.eligible?(session_order) && update_order_coupon_id(#coupon.id)
flash[:notice] = "Successfully added coupon code #{#coupon.code}."
redirect_to shopping_orders_url
else
form_info
flash[:notice] = "Sorry coupon code: #{params[:coupon][:code]} is not valid."
render :action => 'show'
end
end
private
def form_info
#coupon = Coupon.new
end
def update_order_coupon_id(id)
session_order.update_attributes( :coupon_id => id )
end
end
#coupon is nil when the view is being rendered.
The problem might be that <%= render "/shopping/coupons/cou" %> does not go through the cou action in the controller thus form_info method does not execute and #coupon does not get assigned a value.
You have to set #coupon in the action which renders the main view (the one which has the <%= render "/shopping/coupons/cou" %> in it).
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
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| %>