Rails 3 / 嵌套路由:在用户显示页面中重定向到微帖子集合的正确 url 时出现问题

发布于 2024-10-24 21:19:03 字数 1700 浏览 2 评论 0原文

我遇到嵌套路由问题,无法显示正确的 url。

我使用下面的代码,但它呈现类似:

.../users/[:user_id]/microposts/[:user_id] 

而不是:

.../users/[:user_id]/microposts/[:micropost_id] 

对于用户显示页面中显示的每个微帖子。

关联

has_many :microposts #user model
belongs_to :users #micropost model

路由

resources :microposts, :only => [:create, :destroy]
resources :users, :only => [:show] do
  resources :microposts, :only => [:show]  
end

Microposts_controller

def show
  @user = User.find(params[:user_id])
  @micropost = @user.microposts.find(params[:id])
end

microposts/_micropost.html.erb

<%= link_to "show this micropost" user_micropost_url(@user) %> #link for every microposts displayed in the user show pages

user/show.html.erb

<%= render @microposts %>

我看不出我错在哪里。 非常感谢您的帮助!

编辑

if I use [@user, @micropost ] the displayed url is .../users/[:user_id]
if I use user_micropost_url(@user, @micropost) it renders an error:

No route matches {:action=>"show", :controller=>"microposts", :user_id=>#<User id: 1

路线:

user_micropost GET /users/:user_id/microposts/:id(.:format) {:action=>"show", :controller=>"microposts"}
microposts POST   /microposts(.:format) {:action=>"create", :controller=>"microposts"}
micropost DELETE /microposts/:id(.:format) {:action=>"destroy", :controller=>"microposts"}
user GET    /users/:id(.:format) {:action=>"show", :controller=>"users"}

I have a problem with nested routes which don't display correct url.

I use the below code, but it renders something like:

.../users/[:user_id]/microposts/[:user_id] 

instead of:

.../users/[:user_id]/microposts/[:micropost_id] 

for every microposts displayed in the user show pages.

Associations

has_many :microposts #user model
belongs_to :users #micropost model

routes

resources :microposts, :only => [:create, :destroy]
resources :users, :only => [:show] do
  resources :microposts, :only => [:show]  
end

Microposts_controller

def show
  @user = User.find(params[:user_id])
  @micropost = @user.microposts.find(params[:id])
end

microposts/_micropost.html.erb

<%= link_to "show this micropost" user_micropost_url(@user) %> #link for every microposts displayed in the user show pages

user/show.html.erb

<%= render @microposts %>

I can't see where I am wrong.
Thank a lot for your help!

EDIT

if I use [@user, @micropost ] the displayed url is .../users/[:user_id]
if I use user_micropost_url(@user, @micropost) it renders an error:

No route matches {:action=>"show", :controller=>"microposts", :user_id=>#<User id: 1

Routes:

user_micropost GET /users/:user_id/microposts/:id(.:format) {:action=>"show", :controller=>"microposts"}
microposts POST   /microposts(.:format) {:action=>"create", :controller=>"microposts"}
micropost DELETE /microposts/:id(.:format) {:action=>"destroy", :controller=>"microposts"}
user GET    /users/:id(.:format) {:action=>"show", :controller=>"users"}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

如何视而不见 2024-10-31 21:19:03

您还需要传入@micropost:

<%= link_to "show this micropost", user_micropost_url(@user, @micropost) %> #link for every microposts displayed

您也可以将其缩短为:

<%= link_to "show this micropost", [@user, @micropost] %> #link for every microposts displayed

You need to pass in @micropost as well:

<%= link_to "show this micropost", user_micropost_url(@user, @micropost) %> #link for every microposts displayed

You could also shorten it to:

<%= link_to "show this micropost", [@user, @micropost] %> #link for every microposts displayed
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文