Ruby on rails RubyonRails:将帖子推到散列中,然后显示它们

Ruby on rails RubyonRails:将帖子推到散列中,然后显示它们,ruby-on-rails,Ruby On Rails,我正在创建一个twitter副本,现在我正在尝试从你关注的所有用户那里获取所有帖子,然后将它们显示在主页上。我以前在PHP中做过这件事,但我是RoR的新手,所以我可能试图用错误的方式来做 一个用户有许多订阅 并且订阅属于用户 一个用户有很多帖子 一个帖子属于用户 到目前为止,我得到的是: 会话\u控制器.rb def get_posts @sub = @current_user.subscriptions.first Post.where("user_id = ?", @sub.foll

我正在创建一个twitter副本,现在我正在尝试从你关注的所有用户那里获取所有帖子,然后将它们显示在主页上。我以前在PHP中做过这件事,但我是RoR的新手,所以我可能试图用错误的方式来做

一个用户有许多订阅 并且订阅属于用户

一个用户有很多帖子 一个帖子属于用户

到目前为止,我得到的是:

会话\u控制器.rb

def get_posts
  @sub = @current_user.subscriptions.first
  Post.where("user_id = ?", @sub.following_id).find_each do |tweet|
    render partial: 'shared/tweet', locals: {tweet: tweet}
  end
end
<table>
    <tr>
        <th>Username</th>
        <th>Tweet</th>
    </tr>
    <%= yield %>
</table>
<div class="tweet">
    <td>Username here somehow</td>
    <td><%= tweet.content %></td>
</div>
def get_posts
  @sub = @current_user.subscriptions.first
  @tweets = Post.where("user_id = ?", @sub.following_id)
end
<table>
  <thead>
    <tr>
        <th>Username</th>
        <th>Tweet</th>
    </tr>
  </thead>
  <tbody>
   <% @tweets.each do |tweet| %>
     <%= render 'shared/tweet', tweet: tweet %>
   <% end %>
  </tbody>
</table>
<tr class="tweet">
    <td><%= tweet.user.name %></td> # Not sure
    <td><%= tweet.content %></td>
</tr>
我知道。first只收到第一份订阅,但我想尝试得到一些东西

home.html.erb

def get_posts
  @sub = @current_user.subscriptions.first
  Post.where("user_id = ?", @sub.following_id).find_each do |tweet|
    render partial: 'shared/tweet', locals: {tweet: tweet}
  end
end
<table>
    <tr>
        <th>Username</th>
        <th>Tweet</th>
    </tr>
    <%= yield %>
</table>
<div class="tweet">
    <td>Username here somehow</td>
    <td><%= tweet.content %></td>
</div>
def get_posts
  @sub = @current_user.subscriptions.first
  @tweets = Post.where("user_id = ?", @sub.following_id)
end
<table>
  <thead>
    <tr>
        <th>Username</th>
        <th>Tweet</th>
    </tr>
  </thead>
  <tbody>
   <% @tweets.each do |tweet| %>
     <%= render 'shared/tweet', tweet: tweet %>
   <% end %>
  </tbody>
</table>
<tr class="tweet">
    <td><%= tweet.user.name %></td> # Not sure
    <td><%= tweet.content %></td>
</tr>

用户名
推特
\u tweet.html.erb

def get_posts
  @sub = @current_user.subscriptions.first
  Post.where("user_id = ?", @sub.following_id).find_each do |tweet|
    render partial: 'shared/tweet', locals: {tweet: tweet}
  end
end
<table>
    <tr>
        <th>Username</th>
        <th>Tweet</th>
    </tr>
    <%= yield %>
</table>
<div class="tweet">
    <td>Username here somehow</td>
    <td><%= tweet.content %></td>
</div>
def get_posts
  @sub = @current_user.subscriptions.first
  @tweets = Post.where("user_id = ?", @sub.following_id)
end
<table>
  <thead>
    <tr>
        <th>Username</th>
        <th>Tweet</th>
    </tr>
  </thead>
  <tbody>
   <% @tweets.each do |tweet| %>
     <%= render 'shared/tweet', tweet: tweet %>
   <% end %>
  </tbody>
</table>
<tr class="tweet">
    <td><%= tweet.user.name %></td> # Not sure
    <td><%= tweet.content %></td>
</tr>

不知怎的,这里有用户名
但是现在桌子上什么都没有。。那么,我做错了什么?(我做得对吗?

试试这个:

会话\u控制器.rb

def get_posts
  @sub = @current_user.subscriptions.first
  Post.where("user_id = ?", @sub.following_id).find_each do |tweet|
    render partial: 'shared/tweet', locals: {tweet: tweet}
  end
end
<table>
    <tr>
        <th>Username</th>
        <th>Tweet</th>
    </tr>
    <%= yield %>
</table>
<div class="tweet">
    <td>Username here somehow</td>
    <td><%= tweet.content %></td>
</div>
def get_posts
  @sub = @current_user.subscriptions.first
  @tweets = Post.where("user_id = ?", @sub.following_id)
end
<table>
  <thead>
    <tr>
        <th>Username</th>
        <th>Tweet</th>
    </tr>
  </thead>
  <tbody>
   <% @tweets.each do |tweet| %>
     <%= render 'shared/tweet', tweet: tweet %>
   <% end %>
  </tbody>
</table>
<tr class="tweet">
    <td><%= tweet.user.name %></td> # Not sure
    <td><%= tweet.content %></td>
</tr>
home.html.erb

def get_posts
  @sub = @current_user.subscriptions.first
  Post.where("user_id = ?", @sub.following_id).find_each do |tweet|
    render partial: 'shared/tweet', locals: {tweet: tweet}
  end
end
<table>
    <tr>
        <th>Username</th>
        <th>Tweet</th>
    </tr>
    <%= yield %>
</table>
<div class="tweet">
    <td>Username here somehow</td>
    <td><%= tweet.content %></td>
</div>
def get_posts
  @sub = @current_user.subscriptions.first
  @tweets = Post.where("user_id = ?", @sub.following_id)
end
<table>
  <thead>
    <tr>
        <th>Username</th>
        <th>Tweet</th>
    </tr>
  </thead>
  <tbody>
   <% @tweets.each do |tweet| %>
     <%= render 'shared/tweet', tweet: tweet %>
   <% end %>
  </tbody>
</table>
<tr class="tweet">
    <td><%= tweet.user.name %></td> # Not sure
    <td><%= tweet.content %></td>
</tr>

为什么要在div中插入td?哦。。忘了换那个了。。从一开始就没有使用表。您在哪里使用
get\u posts
helper方法?sry。。get_posts位于会话_控制器中。。前后改变了这么多我现在都很困惑..我认为命名约定和路由存在一些问题。如果你愿意,我可以和你聊天。