こんにちは私は現在、ユーザー、アルバム、写真の3つのモデルを持っています。アルバム/写真の作成を最初に終了させようとしているため、登録はしましたが、ログイン/セッションはまだ行っていません。ただし、アルバムを作成すると、URLが次のように変更されます。
http://localhost:3000/users/13/albums/new
(問題なし)
に
http://localhost:3000/albums/76/photos/76
(問題)
ここでの問題は次のとおりです。1。user_idがURLから消えます。2.間違ったURLに送信されます。以下に示すように、私は[@ user、@album]にリダイレクトしています。それがアルバムコントローラーのショーアクションではないですか?URLとして/users/ 13 / albums/1のようなものが欲しかった。代わりに、それは私をに送っていphotos#show
ます。3.リンクが間違っているのに、アルバムIDと写真IDが常に同じであるのはなぜですか。76/7677/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
albums / 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 %>
config /routes
Pholder::Application.routes.draw do
resources :users do
resources :albums
end
resources :albums do
resources :photos
end
もうファイルが必要な場合はお知らせください。