gpt4 book ai didi

ruby-on-rails - rails : how to redirect to "show" action of nested resource in a "create" action?

转载 作者:行者123 更新时间:2023-12-03 15:57:06 26 4
gpt4 key购买 nike

嗨,我目前有 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

专辑/new.html.erb
<%= 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 的操作通过畸形的路线。我认为这是您所有三个问题的根源。最后,我认为您的 AlbumsControllershow操作需要如下所示:

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

希望这可以帮助!

编辑以解决埃德蒙的评论:

当然,您可以设置 Album#show允许用户添加照片的操作。这将涉及对我上面建议的两个更改。

在您的 Controller 中,您将在 @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/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com