我目前正在上传图片的项目。这些相册中有用户,相册和图片。我已经达到了以用户身份创建专辑的程度(尚未完成会话,身份验证或登录,但已完成注册)。我收到一个错误我的表单提交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