嗨,我目前有 3 个模型:用户、相册、照片。我已经进行了注册,但还没有登录/会话,因为我试图先完成相册/照片的创建。但是,当我创建相册时,URL 会从
http://localhost:3000/users/13/albums/new
(没问题)
至
http://localhost:3000/albums/76/photos/76
(问题)
这里的问题是: 1. user_id 从 url 中消失。2. 它把我送到了错误的网址。正如您在下面看到的,我正在重定向到 [@user, @album]。这不应该是专辑控制器的表演动作吗?我想要像 /users/13/albums/1 这样的网址。相反,它把我送到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
专辑/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
让我知道您是否需要更多文件。