0

I'm stumped on my Rails app. I've created 3 models: user, event, category that have the following associations:

class User
  has_many :events, :dependent => :destroy
  attr_accessible :name, :email, :password, :password_confirmation, :remember_me

class Category
  attr_accessible :name
  has_many :events

class Event
  attr_accessible :address, :cost, :date, :details, :end_time, :fav, :start_time, :title, :venue
  belongs_to :user
  belongs_to :category, :foreign_key => :name #not sure if this foreign key is working

The idea is that Users create events that are filed under a single category. The categories are pre-populated so users don't get to make them, only choose which to file under.

My user and events association has been working for a while, but since I've added the category model I get a "Can't mass-assign protected attributes: category" error when I try to create a new event.

I've been browsing pages all day and can't seem to track the error down. I tried adding a foreign key using belongs_to :category, :foreign_key => :name in the Event class but that didn't seem to help.

Would graciously appreciate any help, solutions, and/or pointers in the right direction!

Edit 2: I'm pretty new at Rails, but I think I've tracked down where the problem is from the error screen. Says "ActiveModel::MassAssignmentSecurity::Error in EventsController#create" and further down says "app/controllers/events_controller.rb:58:in create" which equates to this line of code: @event = current_user.events.new(params[:event]).

If I'm reading it correctly, that would mean the error occurs because I'm trying to create a new event with a category param passed in the hash and it doesn't know what to do with it. Unfortunately, I don't know what to do either...

Edit 3: As requested, here's the Event controller's create action:

def create
@event = current_user.events.new(params[:event])

respond_to do |format|
    if @event.save
      format.html { redirect_to @event, notice: 'Event was successfully created.' }
      format.json { render json: @event, status: :created, location: @event }
    else
      format.html { render action: "new" }
      format.json { render json: @event.errors, status: :unprocessable_entity }
    end
  end
end
bholtbholt
  • 11,281
  • 6
  • 22
  • 32
  • You should probably take a look at this similar issue: http://stackoverflow.com/questions/3944288/warning-cant-mass-assign-protected-attributes – Hrishi Aug 17 '13 at 05:32
  • Can you tell exactly, while doing what you are getting this "Can't mass-assign protected attributes: category" error. – Charizard_ Aug 17 '13 at 05:39
  • I'm trying to add an event by "localhost:3000/events/new" which is the form I've been able to create events prior to adding the categories. Since I've added the category f.collection_select (which displays correctly) I get the error after I click the submit button. I'll edit in the form HTML – bholtbholt Aug 17 '13 at 05:45

2 Answers2

0

Place the :foreign_key => :name in class User OR provide attr_accessible to class User

Sami
  • 1,041
  • 3
  • 16
  • 32
0

Add :category to attr_accessible in Event model. And using name as foreign key is a bad idea, becouse it isn't unique

For create event with relation with user and category you can do:

user.events.build do |event|
    event.category = category
end

or

category.events.build do |event|
    event.user =user
end

Also, you can use method create

MaxKonin
  • 468
  • 3
  • 16
  • I get the following error when I add :category: `Category(#70276053732040) expected, got String(#70276029549620)` and then it references my 'create' action in the event controller. – bholtbholt Aug 17 '13 at 05:50
  • You should create event like `category.events.build` or `category.events.create` – MaxKonin Aug 17 '13 at 07:33