ruby-on-rails Rails:如何在“create”动作中重定向到嵌套资源的“show”动作?

nbysray5  于 2023-01-03  发布在  Ruby
关注(0)|答案(3)|浏览(114)

您好我目前有3个模特:用户,相册,照片。我已经注册了,但还没有登录/会话,因为我想先完成相册/照片的创建。但是,当我创建相册时,URL从
http://localhost:3000/users/13/albums/new(没有问题)

http://localhost:3000/albums/76/photos/76(问题)
这里的问题是:1. user_id从url中消失了。2.它把我发送到了错误的url。正如你所看到的,我正在重定向到[@user,@album]。这不应该是相册控制器的show操作吗?我想要类似于/users/13/albums/1这样的url。相反,它把我发送到了photos#show。3.即使它是错误的链接,为什么相册ID和照片ID总是一样的?76/76 77/77等...我不知道这是从哪里来的...
这是我的档案:
相册控制器

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

相册/新的.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

如果你还需要档案就告诉我。

3zwtqj6y

3zwtqj6y1#

下面是您的用例所经历的一系列请求:
首先,表单向Album#create操作提交一些数据。
Album#create请求成功后,该请求被重定向到Albums#show操作。
Albums#show动作中,控制器使用当前User和新Album示例化@user@album,然后在Album的photos集合中构建新Photo对象。
这个动作 * then * 再次保存Album记录。因为没有任何改变,所以它实际上没有做任何事情。但是它没有成功地做任何事情,所以它继续将请求重定向到album_photo_path,并且这是事情变梨形的地方。正如定义的那样,这个路由导致Photo#show动作,在照片的父Album的上下文中。该路由需要 * 两个 * 参数,一个Album和一个Photo。在您的代码中调用时,它 * 不应该工作 *-而是抛出一个Routing Error异常。(相信我,我试过了。)
不过,我不会像那样被minutae挂掉,我会做一个盲目的推荐!
听起来你并不想让你的Album#show操作保存专辑并通过一个错误的路径重定向到Photo#show.我认为这是你所有三个问题的根源.最后,我认为你的AlbumsControllershow操作应该看起来像这样:

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

希望这有帮助!

针对Edmund的评论进行了编辑:

当然,你可以设置Album#show动作来允许用户添加照片,这将涉及到我上面建议的两个变化。
在控制器中,您将在@album的Photos集合中示例化一张新照片,就像之前所做的那样:

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,你可能会注意到user_album_photos_path还不存在,所以最后的修改是把Photos作为一个资源嵌套在routes.rb中:

resources :users do
  resources :albums do
    resources :photos
  end
end

这将为您提供使用表单所需的路线,现在您只需要添加一个Photo#create操作来处理表单提交,然后就可以开始比赛了!

b1zrtrql

b1zrtrql2#

我相信您的create操作应该如下所示:

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_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
wyyhbhjk

wyyhbhjk3#

如果嵌套路由如下所示

resources :albums do
   resources :photos
 end

然后,您可以重定向到show action,从创建一个成功的对象开始,如下所示:

album_photos_path(@album, @photo)

相关问题