1

私は Web 開発と Rails の初心者で、Hartl のチュートリアルを完了しました。home.html.erb へのリンクを追加してアプリを拡張して、ユーザーがホームページでさまざまなマイクロポスト フィードを選択し、ページをリロードせずに新しいフィードを表示できるようにしようとしています (ajax と jquery を使用)。3 つのフィードは次のとおりです。

コミュニティ - すべてのマイクロポスト
マイ フレンド - ユーザーとそのフォロワー向けのマイクロポスト (最終的なチュートリアル フィードと同じ)
マイ ポスト - ユーザーの投稿のみ** これを修正しました

正しく動作する唯一のフィードは My Friends フィード (チュートリアルの 1 つ) です。他の 2 つのフィードでは、@feed_items が nil であり、*.erb.js で次のエラーが発生します。

私は髪を引き裂いています-助けてくれてありがとう! このサイトを使用したことがないので、情報を追加する必要があるかどうか、またはこれを間違って尋ねた場合はお知らせください。

127.0.0.1 の GET "/mycontributions" を 2013-01-12 06:10:12 -0500 で開始 JS ユーザー ロード (0.2ms) として StaticPagesController#userfeed で処理 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 followed_id FROM Relationship 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 Relationship 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

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');
4

0 に答える 0