Ruby on rails 未定义的方法'each';在控制器中创建
我有一个未定义的方法“each”,它指向photos控制器中的create方法。看起来@photo.user=当前用户是罪魁祸首,但我不知道如何修复它。尝试了几种不同的方法,但没有成功。有没有办法解决这个问题 照片控制器:Ruby on rails 未定义的方法'each';在控制器中创建,ruby-on-rails,Ruby On Rails,我有一个未定义的方法“each”,它指向photos控制器中的create方法。看起来@photo.user=当前用户是罪魁祸首,但我不知道如何修复它。尝试了几种不同的方法,但没有成功。有没有办法解决这个问题 照片控制器: def new @photo = Photo.new end def create @photo = Photo.new(params[:photo]) @photo.user = current_user if @photo.s
def new
@photo = Photo.new
end
def create
@photo = Photo.new(params[:photo])
@photo.user = current_user
if @photo.save
flash[:notice] = "Successfully created photos."
redirect_to user_photos_path(current_user)
else
render :action => 'new'
end
end
def edit
@photo = Photo.find(params[:id])
end
def update
@photo = Photo.find(params[:id])
if @photo.update_attributes(paramas[:photo])
flash[:notice] = "Successfully updated photo."
redirect_to @photo.gallery
else
render :action => 'edit'
end
end
def destroy
@photo = Photo.find(params[:id])
@photo.destroy
flash[:notice] = "Successfully destroyed photo."
redirect_to @photo.gallery
end
end
def index
@galleries = Gallery.all
end
def show
@gallery = Gallery.find(params[:id])
end
def new
@gallery = Gallery.new
end
def create
@gallery = Gallery.new(params[:gallery])
if @gallery.save
flash[:notice] = "Successfully created gallery."
redirect_to @gallery
else
render :action => 'new'
end
end
def edit
@gallery = Gallery.find(params[:id])
end
def update
@gallery = Gallery.find(params[:id])
if @gallery.update_attributes(params[:gallery])
flash[:notice] = "Successfully updated gallery."
redirect_to gallery_url
else
render :action => 'edit'
end
end
def destroy
@gallery = Gallery.find(params[:id])
@gallery.destroy
flash[:notice] = "Successfully destroy gallery."
redirect_to galleries_url
end
end
照片模型:
attr_accessible :title, :body, :gallery_id, :name, :image, :remote_image_url
belongs_to :gallery
has_many :gallery_users, :through => :gallery, :source => :user
belongs_to :user
mount_uploader :image, ImageUploader
validate :quota
private
def quota
return unless user
if gallery.photos.count > 4
errors[:base] << "Maximum photos uploaded, delete to upload more"
end
end
end
照片上载视图:
<%= form_for @photo, :html => {:multipart => true} do |f| %>
<%= f.hidden_field :gallery_id %>
<p>
<%= f.label :name %><br/>
<%= f.text_field :name %>
</p>
<p>
<%= f.file_field :image %>
</p>
<%= f.label :remote_image_url, "or image URL" %><br/>
<%= f.text_field :remote_image_url %>
</p>
<p> <%= f.submit %></p>
<% end %>
从错误中请求参数:
{"utf8"=>"✓",
"authenticity_token"=>"Pher65dG6gRU9NGgv2q1ot0cfjq+MELgXE6dOtvcrY0=",
"photo"=>{"gallery_id"=>"",
"name"=>"dssdsdds",
"image"=>#<ActionDispatch::Http::UploadedFile:0x007f82c171cb18 @original_filename="X-box-720-8.jpg",
@content_type="image/jpeg",
@headers="Content-Disposition: form-data; name=\"photo[image]\"; filename=\"X-box-720-8.jpg\"\r\nContent-Type: image/jpeg\r\n",
@tempfile=#<Tempfile:/var/folders/2_/p3vcr8d93ydbsrcv63k2n56r0000gn/T/RackMultipart20130502-20210-1498gwp>>,
"remote_image_url"=>""},
"commit"=>"Create Photo"}
{“utf8”=>“✓",
“真实性令牌”=>“Pher65dG6gRU9NGgv2q1ot0cfjq+MELgXE6dOtvcrY0=”,
“照片”=>{“画廊id”=>”,
“名称”=>“DSDDS”,
“图像”=>#,
“远程\u图像\u url”=>“”},
“提交”=>“创建照片”}
我认为这就是问题所在
has_many :user, :through => :gallery
belongs_to :user
关联逻辑是错误的
恐怕您必须从模型级别开始检查。我无法提供有关当前信息的更多帮助。您不能有两个名为
:user
的:Photo
模型关联。您还缺少中引用的:gallery
关联。首先,添加:gallery
a在宣布其他关联之前的关联:
class Photo < ActiveRecord::Base
attr_accessible ... # your list of attributes
belongs_to :gallery
...
致:
一个实际的错误消息和堆栈跟踪,以及对“几个不同方法”的描述“如果你尝试不成功,这将是一个更好的问题。事实上,任何人都无法提供帮助。我将添加更多的文件,但我希望有人查看create方法,看看在指向它时,那里的一切是否都很好。与此修改后的编码一样,它无法保存/创建照片,这导致它无法工作。如果我恢复到我的旧代码,它允许我很好地上传。您是否在照片模型中设置了
属于:user
?您的照片表是否有“用户id”列?请发布完整的错误消息和相关的视图/控制器代码,以便我们提供帮助。@Yoshiji先生,它没有列用户id,只有gallery id。当我使用您修改的表时,它提供了未定义的局部变量或方法“gallery”(我保留了我的归属)。您一定缺少了属于:gallery
语句,在使用该语句之前,应该先编写该语句,然后再将其用于已更新的多个关联。不,我在提交评论之前已这样做。得到了一个未定义的方法“user=”错误,所以我刚刚返回。你所做的看起来是对的。它必须在“我的照片”控制器中才能执行创建操作。您可以重新发布照片、图库和用户模型吗?我认为有些东西仍然被错误地配置了…在你的照片模型中,原始的属于:user
发生了什么?那应该是留下来的…谢谢你指出这一点。我会尽量找一个工作的。
has_many :user, :through => :gallery
belongs_to :user
class Photo < ActiveRecord::Base
attr_accessible ... # your list of attributes
belongs_to :gallery
...
has_many :user, :through => :gallery
has_many :gallery_users, :through => :gallery, :source => :user