pwz2000
pwz2000

Reputation: 1395

undefined method `each' in controller for create

I have a undefined method `each' that points to the create method in photos controller. It looks like @photo.user = current_user is the culprit but I don't understand how to fix it. Tried a few different methods but was unsuccessful. Any ideas on how can I fix this?

photo controller:

  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

photo model:

  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

User model:

 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

Gallery model:

   attr_accessible :title, :body, :name
   belongs_to :user
   has_many :photos

end

photo upload view:

<%= 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 %>

gallery controller:

 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

Request parameters from error:

{"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"}

Upvotes: 0

Views: 164

Answers (2)

PinnyM
PinnyM

Reputation: 35531

You can't have 2 associations for the Photo model that are both called :user. You are also missing the :gallery association being referenced in has-many-through. Firstly, add the:gallery association before the other associations are declared:

class Photo < ActiveRecord::Base
  attr_accessible ... # your list of attributes
  belongs_to :gallery
  ...

Then change:

has_many :user, :through => :gallery

to:

has_many :gallery_users, :through => :gallery, :source => :user

Upvotes: 1

Billy Chan
Billy Chan

Reputation: 24815

I think this is the problem

  has_many :user, :through => :gallery
  belongs_to :user

The association logic is wrong.

I'm afraid you must start checking from model level. I can't provide more help with current info.

Upvotes: 1

Related Questions