【问题标题】:rails post path issuerails post路径问题
【发布时间】:2014-01-20 09:31:42
【问题描述】:

我对视图中的路径有疑问,我不知道如何解决。 我的“类别”有_许多“帖子”和“帖子”属于_“类别”。

1.- 我想在主页上显示特定类别的截断最后一篇文章(ID 号“1”)。然后我希望该帖子链接到显示帖子路径,但我收到此错误:

“未知动作 找不到 PostsController 的操作“索引””

我认为我的路径错了,因为我不需要索引视图,因为我只会显示那个特定的帖子。所以,我认为 category_posts_path(@last_post) 不是正确的路径(我不知道在哪里可以找到有关在视图中制作路线路径的更多信息......)。实际上,当它是“1”类别的帖子时,浏览器向我显示正在寻找“2”类别......?我究竟做错了什么? 这是浏览器路由:

http://localhost:3000/en/categories/2/posts

这是我的意见/类别/home.html.erb 文件:

<div class="post_details">
 <h2><%= @last_post.title %></h2>
 <%= image_tag @last_post.image(:header), class: "post_image" %>
 <p><%= truncate @last_post.body, length: 100 %></p>
 <p class="button"><%= link_to "READ MORE", category_posts_path(@last_post) %></p>
</div>

2.- 我在views/categories/show.html.erb 文件中有另一个路径问题。我有一个循环来显示一个特定类别的所有帖子,但是当我链接到某个帖子(以显示它)时,再次出现“索引”错误:

“未知动作 找不到 PostsController 的操作“索引””

这是浏览器路由:

http://localhost:3000/en/categories/1/posts

这是views/categories/show.html.erb文件:

<div class="post_details">
    <h2><%= link_to post.title, category_posts_path(post) %></h2>
    <%= image_tag post.image(:header), class: "post_image" %>
    <p><%= post.body %></p>
</div>

这是 categories_controller.rb 文件:

class CategoriesController < ApplicationController

  before_action :get_categories

  def index

  end

  def show

    @category = Category.find(params[:id])

  end

  def home
    if params[:set_locale]
        redirect_to root_url(locale: params[:set_locale])
    else

      @category = Category.find_by_id(1)

      @last_post = @category.posts.order("created_at desc").first

    end
  end

  def get_categories
    @categories = Category.all.order("rank asc, name asc")    
  end

end

这是我的 posts_controller.rb 文件:

class PostsController < ApplicationController


    def show
        @category = Category.find(params[:category_id])
        @post = @category.posts.find(params[:id])
    end


end

这是我的 route.rb 文件:

  scope '(:locale)' do

    resources :categories do
      resources :posts
    end

    resources :contacts

    root 'categories#home'

    get "/contact" => "contacts#new"

    # static pages
    get "/investment" => "contents#investment"
    get "/partner-with-us" => "contents#partner", as: "partner"
    get "/our-companies" => "contents#companies", as: "companies"
    get "/site-map" => "contents#sitemap", as: "sitemap"
    get "/terms-and-conditions" => "contents#terms", as: "terms"
    get "/privacy" => "contents#privacy"

  end

【问题讨论】:

  • 你能发布“rake routes”的输出吗

标签: ruby-on-rails path


【解决方案1】:

当您嵌套路由时,您应该始终考虑给定路由中的父级和子级。由于您的路径对您的关联一无所知,因此您必须明确定义嵌套中的每个对象。

即因为您在链接到给定类别中的最后一篇文章的类别中嵌套了帖子,所以看起来像这样: category_post_path(@category, @last_post)

(我认为您那里还有一个错字 - category_posts_paths - 链接到帖子索引索引 - 因此出现错误。请改用 category_post_path.,并同时给它父类别和帖子。

您可以运行rake routes 以查看有关路径的确切信息(或转到http://localhost:3000/rails/info/routes

【讨论】:

  • 感谢 madsheep,这就是问题所在,根据您的解释,我刚刚想出了如何更改 views/categories/show.html.erb 文件,如下所示: category_post_path(@category, post)
  • 没问题 ;) 乐于助人。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2016-03-24
  • 1970-01-01
  • 2012-06-02
  • 2014-03-28
  • 1970-01-01
  • 1970-01-01
  • 2012-10-16
相关资源
最近更新 更多