Search code examples
ruby-on-railsruby-on-rails-4mass-assignmentmonologue

Mass assignment error when creating first user for monologue rails blog engine


I am using the monologue blog engine for an existing rails 4 app. I'm following their instructions via their github page and have encountered a mass assignments error when creating the first user in the rails console. I would typically go into the model and allow mass assignments, however I can't seem to find any model files for monologue in my rails application. When I navigate to /blog on my site, I see the template, and when I goto /blog/login, I see the login page. I should also note, I've copied the contents of the monologue views, models, controllers, helpers etc to my monologue files. The users_controller.rb file includes a protected method that should allow mass assignment.

The error is:

WARNING: Can't mass-assign protected attributes for Monologue::User: name, email, password, password_confirmation

I've included monologue in my gemfile:

source 'https://rubygems.org'
ruby '1.9.3'

gem 'rails', '4.1.0'
gem 'devise', '3.0'
gem 'google-analytics-rails'
gem 'meta-tags'
gem 'databasedotcom'
gem 'databasedotcom-rails'
gem 'protected_attributes'
gem 'thin'

group :development do
  gem 'pg'
end

group :production do
  gem 'newrelic_rpm'
  gem 'rails_12factor'
end 

gem 'sass-rails',   '~> 4.0.2'
gem 'bootstrap-sass'
gem 'sprockets', '2.11.0'
gem 'sprockets-rails'
gem 'coffee-rails', '~> 4.0.0'
gem 'uglifier', '>= 1.3.0'
gem 'jquery-rails', "~> 2.1.0"
gem 'font-awesome-rails'
gem 'monologue', github: 'jipiboily/monologue'

users_controller.rb in app/controllers/monologue/admin/users_controller.rb

class Monologue::Admin::UsersController < Monologue::Admin::BaseController

  before_filter :load_user, except: [:index, :new, :create]

  def edit

  end

  def new
    @user = Monologue::User.new
  end

  def update
    if @user.update user_params
      flash.notice = "User modified"
      redirect_to admin_users_path
    else
      render :edit
    end
  end

  def destroy
    if @user.destroy
      redirect_to admin_users_path, notice:  I18n.t("monologue.admin.users.delete.removed", user: @user.name)
    else
      redirect_to admin_users_path, alert: I18n.t("monologue.admin.users.delete.failed", user: @user.name)
   end
  end

  def create
    @user = Monologue::User.new user_params
    if @user.save
      flash.notice = I18n.t("monologue.admin.users.create.success")
      redirect_to admin_users_path
    else
      render :new
    end
  end

  def index
    @users = Monologue::User.all
  end

  private
    def load_user
      @user = Monologue::User.find(params[:id])
    end

    def user_params
      params.require(:user).permit(:name, :email, :password, :password_confirmation)
    end
end

My Routes.rb file:

# Blog URL
  mount Monologue::Engine, at: '/blog' # or whatever path, be it "/blog" or "/monologue"

Solution

  • Open you application.rb file and change---

    config.active_record.whitelist_attributes = true

    to

    config.active_record.whitelist_attributes = false

    and restart server.