我目前正在上傳圖片的項目。這些相冊中有用戶,相冊和圖片。我已經達到了以用戶身份創建專輯的程度(尚未完成會話,身份驗證或登錄,但已完成註冊)。我收到一個錯誤我的表單提交Rails所後Rails:ActiveRecord :: RecordNotFound - 無法找到沒有ID的用戶
Couldn't find User without an ID
我發現我的new.html.erb的URL是確定:
http://localhost:3000/users/13/albums/new
(沒問題)
但之後我提交它,它給了我一個錯誤,頁面是:
http://localhost:3000/albums/58
(問題在URL沒有USER_ID)
沒有人知道爲什麼我的路線突然改變,突然之間以及如何解決它?該錯誤是在我的應用程序/控制器/ albums_controller.rb:14:在'顯示'@user = User.find(params [:user_id])行。
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 %>
albums_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 @user.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 @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
路線
Pholder::Application.routes.draw do
resources :users do
resources :albums
end
resources :albums do
resources :photos
end
你見過這個答案嗎? http://stackoverflow.com/questions/1303980/activerecordrecordnotfound-couldnt-find-user-without-an-id?rq=1 –
是的我已經看到了,但它沒有爲我做任何事情。同樣的錯誤 – Edmund
我認爲問題是我的專輯#重定向創建... – Edmund