我是 Web 开发和 Rails 的新手,并且已经完成了 Hartl 的教程。我正在尝试通过添加指向 home.html.erb 的链接来扩展应用程序,以允许用户在主页上选择不同的微帖子提要并在不重新加载页面的情况下显示新提要(使用 ajax 和 jquery)。这三个提要是:
社区 - 所有微博
我的朋友 - 用户和他/她的追随者的微博(与最终教程提要相同)
我的帖子 - 只是用户的帖子** 修复了这个
唯一正常工作的提要是“我的朋友”提要(教程中的提要),对于其他两个提要,@feed_items 为零,导致 *.erb.js 中出现以下错误
我把头发扯掉了——谢谢你的帮助!我从来没有使用过这个网站,所以如果我应该添加任何信息或者我问错了,请告诉我。
在 2013 年 1 月 12 日 06:10:12 -0500 开始 GET "/mycontributions" for 127.0.0.1 由 StaticPagesController#userfeed 作为 JS 用户负载进行处理(0.2 毫秒) SELECT "users".* FROM "users" WHERE "users" ."remember_token" = 'fHe72EwO6387WdP26K07Rw' LIMIT 1 SQL (112.2ms) UPDATE "users" SET "feed_selection" = 'My Contributions' WHERE "users"."id" = 1 Micropost Load (1.9ms) SELECT "microposts".* FROM "microposts" WHERE (user_id IN (SELECT follower_id FROM Relations WHERE follower_id = 1) OR user_id = 1) ORDER BY microposts.created_at DESC LIMIT 30 OFFSET 0 (0.4ms) SELECT COUNT(*) FROM "microposts"WHERE (user_id IN (SELECT follower_id FROM 关系 WHERE follower_id = 1) OR user_id = 1) 渲染 static_pages/userfeed.js.erb (5.8ms) 在 143ms 内完成 500 内部服务器错误
ActionView::Template::Error(未定义方法paginate' for nil:NilClass):
1: alert ('start of userfeed.js.erb');
2: $('#MyContributions').parent().addClass('active').siblings().removeClass('active');
3: <% @micropost = current_user.microposts.build %>
4: <% @feed_items = current_user.userfeed.paginate(page: params[:page]) %>
5: $('.microposts').remove();
6: $('.pagination').remove();
7: $("#buzzFeed").append("<%= escape_javascript(render 'shared/feed') %>");
app/views/static_pages/userfeed.js.erb:4:in
_app_views_static_pages_userfeed_js_erb_774602371 _641866188 '
home.html.erb 中的链接:
<li class="<%= 'active' if current_user.feed_selection == 'Community Buzz' %>">
<%= link_to 'Community Buzz',
{ :controller => :static_pages, :action => :communityfeed },
remote: true, id: 'CommunityBuzz', class: "buzz-cat" %>
</li>
<li class="<%= 'active' if current_user.feed_selection == 'My Friends' %>">
<%= link_to "My Friends",
{ :controller => :static_pages, :action => :friendfeed },
remote: true, id: "MyFriends", class: "buzz-cat" %>
</li>
<li class="<%= 'active' if current_user.feed_selection == 'My Contributions' %>">
<%= link_to "My Contributions",
{ :controller => :static_pages, :action => :userfeed },
remote: true, id: "MyContributions", class: "buzz-cat" %>
</li>
static_pages_controller.rb
class StaticPagesController < ApplicationController
respond_to :html, :js
def home
puts "StaticPagesContoller: action: home: start method"
if signed_in?
puts "home signed-in - feed_selection is: " + current_user.feed_selection
if current_user.feed_selection.nil?
puts "home signed-in - feed_selection is nil, so default it to 'Community Buzz' in the database"
current_user.update_column(:feed_selection, "Community Buzz")
toggle_feed(current_user.feed_selection, "Community Buzz") if !current_user.feed_selection.nil?
end
puts "home, signed-in - before calling toggle_feed"
toggle_feed(current_user.feed_selection, current_user.feed_selection) if !current_user.feed_selection.nil?
puts "home, signed-in - after calling toggle_feed"
end
end
def communityfeed
puts "StaticPagesContoller: action: communityfeed: start method"
if signed_in?
puts "communityfeed, signed-in - feed_selection is: " + current_user.feed_selection
puts "communityfeed, signed-in - before calling toggle_feed"
toggle_feed(current_user.feed_selection, "Community Buzz") if !current_user.feed_selection.nil?
puts "communityfeed, signed-in - after calling toggle_feed"
end
end
def friendfeed
puts "StaticPagesContoller: action: friendfeed: start method"
if signed_in?
puts "friendfeed, signed-in - feed_selection is: " + current_user.feed_selection
puts "friendfeed, signed-in - before calling toggle_feed"
toggle_feed(current_user.feed_selection, "My Friends") if !current_user.feed_selection.nil?
puts "friendfeed, signed-in - after calling toggle_feed"
end
end
def userfeed
puts "StaticPagesContoller: action: userfeed: start method"
if signed_in?
puts "userfeed, signed-in - feed_selection is: " + current_user.feed_selection
toggle_feed(current_user.feed_selection, "My Contributions") if !current_user.feed_selection.nil?
end
end
end
toggle_feed 在 application_controller.rb
class ApplicationController < ActionController::Base
protect_from_forgery
include SessionsHelper
def toggle_feed(saved_feed_selection, picked_feed)
if saved_feed_selection != picked_feed
puts "ApplicationController: toggle_feed: saved_feed_selection != picked_feed, saving picked_feed to database"
current_user.update_column(:feed_selection, picked_feed)
puts "ApplicationController: toggle_feed: after database save."
end
puts "ApplicationController: toggle_feed: before assigning @micropost"
@micropost = current_user.microposts.build
puts "ApplicationController: toggle_feed: after assigning @micropost = " + @micropost.to_s
case picked_feed
when "Community Buzz" then
@feed_items = current_user.communityfeed.paginate(page: params[:page])
when "My Friends" then
@feed_items = current_user.feed.paginate(page: params[:page])
when "My Contributions" then
@feed_items = Micropost.where("user_id = ?", current_user.id).paginate(page: params[:page])
else
puts "FAILED case!"
end
puts "ApplicationController: toggle_feed: End"
end
end
用户.rb
class User < ActiveRecord::Base
attr_accessible :email, :name, :password, :password_confirmation
has_secure_password #call this method to populate :password_digest field
has_many :microposts, dependent: :destroy
has_many :relationships, foreign_key: "follower_id", dependent: :destroy
has_many :followed_users, through: :relationships, source: :followed
has_many :reverse_relationships, foreign_key: "followed_id",
class_name: "Relationship",
dependent: :destroy
has_many :followers, through: :reverse_relationships, source: :follower
# before_save { |user| user.email = email.downcase }
before_save { self.email.downcase! }
before_save :create_remember_token # calls the private method (see below)
validates :name, presence: true, length: { maximum: 50 }
VALID_EMAIL_REGEX = /\A[\w+\-.]+@[a-z\d\-.]+\.[a-z]+\z/i
validates :email, presence: true, format: { with: VALID_EMAIL_REGEX },
uniqueness: { case_sensitive: false }
validates :password, length: { minimum: 6 }
validates :password_confirmation, presence: true
def communityfeed
puts "User.rb: communityfeed method: start"
Micropost.all
puts "User.rb: communityfeed method: end"
end
def feed # this is My Friends + the user
Micropost.from_users_followed_by(self)
end
def userfeed # this is My Contributions
puts "User.rb: userfeed method: start"
Micropost.where("user_id = ?", id)
puts "User.rb: userfeed method: end"
end
def following?(other_user)
relationships.find_by_followed_id(other_user.id)
end
def follow!(other_user)
relationships.create!(followed_id: other_user.id)
end
def unfollow!(other_user)
relationships.find_by_followed_id(other_user.id).destroy
end
private
def create_remember_token
puts "executing 'create remember token'"
self.remember_token = SecureRandom.urlsafe_base64
end
end
这个工作friendfeed.js.erb
alert ('start of friendfeed.js.erb');
$('#MyFriends').parent().addClass('active').siblings().removeClass('active');
<% @micropost = current_user.microposts.build %>
<% @feed_items = current_user.feed.paginate(page: params[:page]) %>
$('.microposts').remove();
$('.pagination').remove();
$("#buzzFeed").append("<%= escape_javascript(render 'shared/feed') %>");
alert ('end of friendfeed.js.erb');
这个不起作用 userfeed.js.erb
alert ('start of userfeed.js.erb');
$('#MyContributions').parent().addClass('active').siblings().removeClass('active');
<% @micropost = current_user.microposts.build %>
<% @feed_items = Micropost.where("user_id = ?", current_user.id).paginate(page: params[:page]) %>
$('.microposts').remove();
$('.pagination').remove();
$("#buzzFeed").append("<%= escape_javascript(render 'shared/feed') %>");
alert ('end of userfeed.js.erb');