【发布时间】: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