我是Web开发和Rails的新手,已经完成了Hartl的教程。我正在尝试使用添加到home.html.erb的链接来扩展应用程序,以允许用户在主页上选择不同的微博提要并在不重新加载页面的情况下显示新的订阅源(使用ajax和jquery)。三个提要是:
社区 - 所有微博
我的朋友 - 为用户和他/她的粉丝提供微博(与最终教程提要相同)
我的帖子 - 只是用户的帖子 **修正了这个帖子
唯一正常工作的Feed是My Friends Feed(教程中的那个),另外两个,@ feed_items为nil,导致* .erb.js中出现以下错误
我正在撕扯我的头发 - 感谢您的帮助!我从未使用过这个网站,所以请告诉我是否应该添加任何信息或者我是否错误地提出了这个问题。
在2013-01-12 06:10:12 -0500开始获取127.0.0.1的“/ mycontributions” 由StaticPagesController处理#userfeed为JS 用户负载(0.2ms)SELECT“users”。* FROM“users”WHERE“users”。“remember_token”='fHe72EwO6387WdP26K07Rw'LIMIT 1 SQL(112.2ms)UPDATE“users”SET“feed_selection”='我的贡献'WHERE“users”。“id”= 1 Micropost Load(1.9ms)SELECT“microposts”。* FROM“microposts”WHERE(user_id IN(SELECT followed_id FROM relationships) 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 followed_id FROM relationships) WHERE follower_id = 1)OR user_id = 1) 呈现static_pages / userfeed.js.erb(5.8ms) 在143毫秒内完成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
user.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');