- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
嗨,我目前有 3 个模型:用户、相册、照片。我已经注册,但还没有登录/ session ,因为我想先完成相册/照片的创建。但是,当我创建相册时,URL 会从http://localhost:3000/users/13/albums/new
(没问题)
到http://localhost:3000/albums/76/photos/76
(问题)
这里的问题是:
1. user_id 从 url 中消失。
2.它把我发送到错误的网址。正如你在下面看到的,我正在重定向到 [@user, @album]。那不应该是相册 Controller 的显示 Action 吗?我想要像/users/13/albums/1 这样的 URL。相反,它把我发送到 photos#show
.
3.即使是错误的链接,为什么相册ID和照片ID总是相同的? 76/76 77/77 等等……我不知道那是从哪里来的……
这是我的文件:
专辑 Controller
def show
@user = User.find(params[:user_id])
@album = @user.albums.find(params[:id])
@photo = @album.photos.build(params[:photo])
respond_to do |format|
if @album.save
format.html { redirect_to album_photo_path(@album), notice: 'Album was successfully created.' }
format.json { render json: @album, status: :created, location: @album}
else
format.html { render action: "new" }
format.json { render json: @album.errors, status: :unprocessable_entity }
end
end
end
def update
end
def edit
end
def create
@user = User.find(params[:user_id])
@album = @user.albums.build(params[:album])
respond_to do |format|
if @user.save
format.html { redirect_to [@user, @album], notice: 'Album was successfully created.' }
format.json { render json: @album, status: :created, location: @album}
else
format.html { render action: "new" }
format.json { render json: @album.errors, status: :unprocessable_entity }
end
end
end
<%= form_for (@album), url: user_albums_path, :html => { :id => "uploadform", :multipart => true } do |f| %>
<div>
<%= f.label :name %>
<%= f.text_field :name %>
<%= f.label :description %>
<%= f.text_area :description %>
<br>
<%=f.submit %>
</div>
<% end %>
Pholder::Application.routes.draw do
resources :users do
resources :albums
end
resources :albums do
resources :photos
end
最佳答案
以下是您的用例运行的一系列请求:
首先,表单向您的 Album#create
提交一些数据行动。
后 Album#create
请求成功,请求被重定向到 Albums#show
行动。
在 Albums#show
Action , Controller 实例化 @user
和 @album
使用您当前的用户和新相册,然后在相册的 photos
中构建一个新的 Photo 对象。收藏。
然后该操作会保存 Album
再次记录。由于什么都没有改变,这实际上并没有做任何事情。但是它什么也没做,所以它继续将请求重定向到 album_photo_path
, 和 这就是梨形的地方 .根据定义,这条路线通向 Photo#show
Action ,在照片的父相册的上下文中。该路由需要两个参数,一个相册和一个照片。正如在您的代码中调用的那样,它不应该工作——而是抛出一个路由错误异常。 (相信我,我试过了。)
不过,我不会像那样卡在细节上,而是要提出一个盲目的建议!
听起来您不是有意为您的 Album#show
保存相册并重定向到 Photo#show
的操作通过畸形的路线。我认为这是您所有三个问题的根源。最后,我认为您的 AlbumsController
的 show
操作需要如下所示:
def show
@user = User.find(params[:user_id])
@album = @user.albums.find(params[:id])
end
Album#show
允许用户添加照片的操作。这将涉及对我上面建议的两个更改。
@album
上实例化一张新照片。的照片集,就像您之前拥有的一样:
def show
@user = User.find(params[:user_id])
@album = @user.albums.find(params[:id])
@photo = @album.photos.build
end
Album#show
模板(例如
app/views/albums/show.html.erb
),您将包含一个用于提交新照片的表单:
<%= form_for [@user, @album, @photo] do |f| %>
<%= f.text_field :caption %>
<%= f.file_field :image %>
<%- end %>
user_album_photos_path
,您可能会注意到尚不存在。所以最后的变化是将照片作为资源嵌套在
routes.rb
中。 :
resources :users do
resources :albums do
resources :photos
end
end
Photo#create
处理表单提交的操作,您就可以参加比赛了!
关于ruby-on-rails - rails : how to redirect to "show" action of nested resource in a "create" action?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12697553/
我是一名优秀的程序员,十分优秀!