0
votes

I upgraded to rails 4 and now I am no longer able to register users on my app. It seems like my gallery (carrierewave) has broken down. I have inspected the code and can't notice anything that would stop it from working now. I get a undefined method `galleries' and it points to def setup_gallery: self.galleries << Gallery.create and under def create: if @user.save

Fresh eyes on my code would be great.

Users controller:

 class UsersController < ApplicationController
  respond_to :html, :json

  def settings
    @user = User.find(id_params)
  end

  def new
    @user = User.new
  end

  def profile
    @profile = User.profile
  end

  def create
    @user = User.new(user_params)
    if @user.save
      UserMailer.registration_confirmation(@user).deliver
      session[:user_id] = @user.id
      redirect_to root_url, notice: "Thank you for signing up!"
    else
      render "new"
    end
  end

  def show
    @user = User.find(id_params)
  end

    def edit
      @user = User.find(id_params)
end

  def index
    @users = User.all
  end

  def destroy
     User.find(id_params).destroy
     flash[:success] = "User deleted."
     redirect_to users_url
   end

def update
    @user = if current_user.has_role?(:admin)
       User.find(id_params)
     else
       current_user
     end
    @user.update_attributes(user_params)
    respond_with @user
    end

    private


      def user_params
   params.require(:user).permit(:name, :email, :username, :password, :zip_code, :birthday, :role)
 end

     def id_params
       params.require(:id).permit(:name)
     end
end

User model:

# 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

photos controller:

class PhotosController < ApplicationController

  def new 
    @photo = Photo.new
  end

  def create
    @photo = Photo.new(photo_params)
    @photo.user = current_user
    if @photo.save
      flash[:notice] = "Successfully created photos."
      redirect_to :back
    else
      render :action => 'new'
    end
  end

  def edit
    @photo = Photo.find(id_params)
  end

  def update
    @photo = Photo.find(id_params)
    if @photo.update_attributes(photo_params)
      flash[:notice] = "Successfully updated photo."
      redirect_to @photo.gallery
    else
      render :action => 'edit'
    end
  end

  def destroy
    @photo = Photo.find(id_params)
    @photo.destroy
    flash

[:notice] = "Successfully destroyed photo." redirect_to @photo.gallery end

private

def user_params params.require(:user).permit(:name) end

def id_params params.require(:id).permit(:name) end end

1
Ok I updated users controller above with the strong parameters. Now when I register it redirects me to homepage with errors from form with password cannot be blank and email is invalid. That can't have something to do with strong parameters since the error messages are outside the controllers.pwz2000
If you're using devise for registrations check this github.com/plataformatec/devise#strong-parametersMartin
There is no devise. Perhaps I am doing the strong parameters wrong? I updated my codepwz2000
Look carefully at the logs. Is there a message along the lines of "Can't mass-assign protected attributes..." (the message is easily missed among all the other log entries). If there is such a message, then it is definitely a strong parameters problem, and the message should specify exactly what attributes are causing the problem.Dave Isaacs

1 Answers

1
votes

After some trial and error I found that I had to change the private method in the user model.

What works is,

 Gallery.create(user: self)

Thanks for those who responded to help!