Bastien
Bastien

Reputation: 606

ActiveModel::ForbiddenAttributesError - / attr_access

I have cloned some source code to study this railscast (https://github.com/railscasts/253-carrierwave-file-uploads). It was probably done with Rails 3 at the time using deprecated attr_accessible method.

I am using Rails 4 so I have commented out the attr_accessible method in the model

class Gallery < ActiveRecord::Base
  #attr_accessible :name
  has_many :paintings
end

and I have added a private section in my controller with the appropriate method

private

def gallery_params
  params.require(:gallery).permit(:name)
end

It should be straithgforward but I still get this error.

ActiveModel::ForbiddenAttributesError

Any fresh eyes to help me out? Thanks.

EDIT Here is the full source code of the controller

class GalleriesController < ApplicationController

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 destroyed gallery."
  redirect_to galleries_url
end

private

def gallery_params
  params.require(:gallery).permit(:name)
end
end

and the source code of the Gemfile

source 'https://rubygems.org'
gem 'rails', '4.2.5'
gem 'sqlite3'
gem 'sass-rails', '~> 5.0'
gem 'uglifier', '>= 1.3.0'
gem 'coffee-rails', '~> 4.1.0'
gem 'jquery-rails'
gem 'turbolinks'
gem 'jbuilder', '~> 2.0'
gem 'sdoc', '~> 0.4.0', group: :doc
gem 'protected_attributes'
gem 'bcrypt', '~> 3.1.7'

group :development, :test do
  console
    gem 'byebug'
end

group :development do
  # Access an IRB console on exception pages or by using <%= console %> in views
gem 'web-console', '~> 2.0'
gem 'spring'
end

Upvotes: 0

Views: 225

Answers (2)

Elvn
Elvn

Reputation: 3057

Looking at your code, I would suggest the following in your controller

New/Create

def new
  @gallery = Gallery.new
end

def create
  @gallery = Gallery.new(gallery_params)  # <= Changed line
  if @gallery.save
    flash[:notice] = "Successfully created gallery."
    redirect_to @gallery
  else
    render :action => 'new'
  end
end

Edit/Update

def edit
  @gallery = Gallery.find(params[:id])
end

def update
  @gallery = Gallery.find(params[:id])
  if @gallery.update_attributes(gallery_params) # <= Changed line
    flash[:notice] = "Successfully updated gallery."
    redirect_to gallery_url
  else
    render :action => 'edit'
  end
end

I hope this helps. Let me know how it goes.

Upvotes: 1

dimuch
dimuch

Reputation: 12818

Try to use gallery_params instead of params[:gallery] in create and updated actions.

And you probably need to remove protected_attributes gem from the gemfile.

Upvotes: 0

Related Questions