Rails 3 / 嵌套路由:在用户显示页面中重定向到微帖子集合的正确 url 时出现问题
我遇到嵌套路由问题,无法显示正确的 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您还需要传入@micropost:
您也可以将其缩短为:
You need to pass in @micropost as well:
You could also shorten it to: