【问题标题】:How to link post to topic?如何将帖子链接到主题?
【发布时间】:2016-07-05 06:08:07
【问题描述】:

我在我的应用程序的根目录中创建了一个最新的帖子框。在这个框中,我有属于主题的帖子。也许这是一个愚蠢的问题,但我怎样才能将点击帖子重定向到它所属的主题?

这会将我重定向到 localhost:3000/topics,如何将 topic_id 添加到此路径?

这是我获取最新帖子的方式:

控制器:

class ApplicationController < ActionController::Base
    # Prevent CSRF attacks by raising an exception.
    # For APIs, you may want to use :null_session instead.
    protect_from_forgery with: :exception
    before_filter :configure_permitted_parameters, if: :devise_controller?
    helper_method :latest_posts
def latest_posts
      @posts ||= Post.all.order("created_at desc").limit(3)
end
end

索引:

<% latest_posts.each do |posts| %>
  <div class="bs-callout bs-callout-warning">
    <p><%= link_to post.content.html_safe, topic_path(post.topic) %></p>
  </div>
<% end %>

routes.rb:

  devise_for :users
  get 'categories' => 'categories#index'
  resources :topics
  resources :posts
  resources :users

路线:

new_user_session GET    /users/sign_in(.:format)       devise/sessions#new
            user_session POST   /users/sign_in(.:format)       devise/sessions#create
    destroy_user_session DELETE /users/sign_out(.:format)      devise/sessions#destroy
           user_password POST   /users/password(.:format)      devise/passwords#create
       new_user_password GET    /users/password/new(.:format)  devise/passwords#new
      edit_user_password GET    /users/password/edit(.:format) devise/passwords#edit
                         PATCH  /users/password(.:format)      devise/passwords#update
                         PUT    /users/password(.:format)      devise/passwords#update
cancel_user_registration GET    /users/cancel(.:format)        devise/registrations#cancel
       user_registration POST   /users(.:format)               devise/registrations#create
   new_user_registration GET    /users/sign_up(.:format)       devise/registrations#new
  edit_user_registration GET    /users/edit(.:format)          devise/registrations#edit
                         PATCH  /users(.:format)               devise/registrations#update
                         PUT    /users(.:format)               devise/registrations#update
                         DELETE /users(.:format)               devise/registrations#destroy
              categories GET    /categories(.:format)          categories#index
                  topics GET    /topics(.:format)              topics#index
                         POST   /topics(.:format)              topics#create
               new_topic GET    /topics/new(.:format)          topics#new
              edit_topic GET    /topics/:id/edit(.:format)     topics#edit
                   topic GET    /topics/:id(.:format)          topics#show
                         PATCH  /topics/:id(.:format)          topics#update
                         PUT    /topics/:id(.:format)          topics#update
                         DELETE /topics/:id(.:format)          topics#destroy
                   posts GET    /posts(.:format)               posts#index
                         POST   /posts(.:format)               posts#create
                new_post GET    /posts/new(.:format)           posts#new
               edit_post GET    /posts/:id/edit(.:format)      posts#edit
                    post GET    /posts/:id(.:format)           posts#show
                         PATCH  /posts/:id(.:format)           posts#update
                         PUT    /posts/:id(.:format)           posts#update
                         DELETE /posts/:id(.:format)           posts#destroy
                   users GET    /users(.:format)               users#index
                         POST   /users(.:format)               users#create
                new_user GET    /users/new(.:format)           users#new
               edit_user GET    /users/:id/edit(.:format)      users#edit
                    user GET    /users/:id(.:format)           users#show
                         PATCH  /users/:id(.:format)           users#update
                         PUT    /users/:id(.:format)           users#update
                         DELETE /users/:id(.:format)           users#destroy
                    root GET    /                              categories#index

【问题讨论】:

  • topic_path(post.topic)?您能否发布您的路线以使我们更加清楚。
  • 我已经尝试过这种方式@Justin,但出现错误:undefined local variable or method post'` 我更新了路线。
  • 您不能遍历posts。请参考this Rails Guides post
  • 我添加了一个答案以反映上面评论中提到的更改。它应该让你找到你的解决方案。干杯。

标签: ruby-on-rails ruby-on-rails-4


【解决方案1】:

您需要先遍历posts,然后基于post 的单个实例链接到topic

<h5>Here are all of my posts</h5>
<% @posts.each do |post| %>
  <%= link_to post.content.html_safe, topic_path(post.topic) %>
<% end %>

这是关于 ActionView 和渲染的可靠 Rails guides 帖子。绝对值得一读。

【讨论】:

  • 我仍然收到undefined local variable or method post'` 我应该在控制器中做什么?我已经尝试从 3 小时开始解决这个问题,但一无所获...我更新了一个问题,您可以查看我如何获得最新帖子。
  • &lt;% latest_posts.each do |posts| %&gt;更改为&lt;% latest_posts.each do |post| %&gt;
  • 是的。当您遍历数组时,您用作 块参数 的变量是块中允许的变量。即:如果块参数为post,则当变量posts 在块内为零时,反之亦然。这就是你的未定义方法的来源。
【解决方案2】:

假设您在config/routes.rb 中定义了show 路由,则可以使用topic_path 帮助器。

<%= link_to posts.content.html_safe, topic_path(topic) %>

topic 是您要重定向到的实例。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-24
    • 2019-12-22
    • 2015-04-19
    • 1970-01-01
    • 1970-01-01
    • 2016-10-14
    相关资源
    最近更新 更多