2013-04-29 107 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

检查,如果你的模型的关联是正确的。这些消息表明有关于此的错误。