我有一个未定义的方法`each'指向照片控制器中的create方法。看起来像@ photo.user = current_user是罪魁祸首,但我不明白如何解决它。尝试了一些不同的方法,但没有成功。关于如何解决这个问题的任何想法?
照片控制器:
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
照片模特:
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
用户模型:
has_secure_password
attr_accessible :role, :age, :age_end, :password_confirmation, :about_me, :feet, :inches, :password, :birthday, :career, :children, :education, :email, :ethnicity, :gender, :height, :name, :password_digest, :politics, :religion, :sexuality, :user_drink, :user_smoke, :username, :zip_code
validates_uniqueness_of :email
validates_format_of :email, with: /^[-a-z0-9_+\.]+\@([-a-z0-9]+\.)+[a-z0-9]{2,4}$/i
validates_presence_of :password, :on => :create
has_many :photos
before_create { generate_token(:auth_token) }
ROLES = %w[admin user guest banned]
# models/user.rb
after_create :setup_gallery
def received_messages
Message.received_by(self)
end
def unread_messages?
unread_message_count > 0 ? true : false
end
def unread_messages
received_messages.where('read_at IS NULL')
end
def sent_messages
Message.sent_by(self)
end
# Returns the number of unread messages for this user
def unread_message_count
eval 'messages.count(:conditions => ["recipient_id = ? AND read_at IS NULL", self.user_id])'
end
def to_s; username
end
def has_role?(role_name)
role.present? && role.to_sym == role_name.to_sym
end
def send_password_reset
generate_token(:password_reset_token)
self.password_reset_sent_at = Time.zone.now
save!
UserMailer.password_reset(self).deliver
end
def generate_token(column)
begin
self[column] = SecureRandom.urlsafe_base64
end while User.exists?(column => self[column])
end
private
def setup_gallery
self.galleries << Gallery.create
end
end
图库模型:
attr_accessible :title, :body, :name
belongs_to :user
has_many :photos
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 %>
画廊控制器:
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
从错误请求参数:
{"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"}
答案 0 :(得分:1)
我认为这是问题
has_many :user, :through => :gallery
belongs_to :user
关联逻辑错误。
我担心你必须从模型级开始检查。我无法提供有关当前信息的更多帮助。
答案 1 :(得分:1)
Photo
模型中不能有两个名为:user
的关联。您还缺少has-many-through中引用的:gallery
关联。首先,在声明其他关联之前添加:gallery
关联:
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