2013-04-29 141 views
-1

我正在製作twitter副本,現在我正在嘗試顯示其他用戶所關注用戶的所有帖子。我在Ruby和Rails新的,所以我可能會做這樣一個非常奇怪的方式..爲什麼我得到這個NoMethodError

的就是這些文件我有:

會話#home.html.erb

<h2 class='User_Header'> Home <h2> 

<%= link_to "New Post", controller: "posts", action: "new" %> 
<%= link_to "Log Out", :controller => "sessions", :action => "logout" %> 
<%= show_tweets("following") %> 

sessions_helper

module SessionsHelper 

    def show_tweets(opt) 
     if opt == "following" 
      @sub = Subscription.where("userID = ?", @current_user.id) 
      @post = Post.where("user_id = ?", @sub.followingID) 

      render partial: 'shared/follower_tweets' 
     end 
    end 

    def show_tweet(s) 
     @post = Post.where("user_id = ?", s.id) 
     render partial: 'shared/tweet' 
    end 

    def tweet_username(p) 
     @username = User.where("id = ?", p.user_id) 
     Rails.logger.debug @username.inspect 
     render partial: 'shared/user' 
    end 
end 

_follower_tweets.html.erb

<h2>Tweets</h2> 

<table> 
    <tr> 
     <th>Username</th> 
     <th>Tweet</th> 
    </tr> 

    <% div_for(@post, class: 'post') do %> 
     <td><%= tweet_username(@post) %></td> 
     <td><%= @post.content %></td> 
    <% end %> 
</table> 

_user.html.erb

<%= @username %> 

session.rb

class Session < ActiveRecord::Base 
    attr_accessible :content, :user_id, :followingID, :userID 
end 

錯誤

應用程序/視圖/會話/ home.html.erb在行#9提出:

undefined method `followingID' for #<ActiveRecord::Relation:0x007fd74b66f8a8> 
+0

你的'訂閱'模型沒有一個叫'followingID'的方法。 – 2013-04-29 22:22:32

+0

我正在嘗試獲取訂閱的以下ID的值。我有一個表中的數據庫,我保存所有訂閱,以下ID是該表中的一列 – Alexander 2013-04-29 22:31:41

+0

您是否已將'attr_accessible:followingID'添加到'Subscription'模型? – 2013-04-29 22:36:24

回答

0

發生了什麼事是你有Session模型而不是Subscription模型。應該是這樣的:

class Subscription < ActiveRecord::Base 
    attr_accessible :followingID 
end 

但是,問題比這個大。你必須閱讀約Active Record Associations,那麼你就可以做類似

@subs = @current_user.subscriptions 
@posts = @current_user.posts 
0

檢查,如果你的模型的關聯是正確的。這些消息表明有關於此的錯誤。