Skip to content

Instantly share code, notes, and snippets.

@morganhein
Created September 12, 2012 01:07
Show Gist options
  • Save morganhein/3703424 to your computer and use it in GitHub Desktop.
Save morganhein/3703424 to your computer and use it in GitHub Desktop.
Can't mass-assign protected attributes: job
# == Schema Information
#
# Table name: jobs
#
# id :integer not null, primary key
# restaurant_id :integer
# shortname :string(255)
# user_id :integer
# created_at :datetime not null
# updated_at :datetime not null
#
class Job < ActiveRecord::Base
attr_accessible :restaurant_id, :shortname, :user_id
belongs_to :user
belongs_to :restaurant
has_many :shifts
validates :shortname, presence: false
end
<% provide(:title, 'Restaurant') %>
<%= form_for @restaurant do |f| %>
<%= render 'shared/error_messages' %>
<%= f.label "Restaurant Name" %>
<%= f.text_field :name %>
<%= f.fields_for :job do |child_f| %>
<%= child_f.label "Nickname" %>
<%= child_f.text_field :shortname %>
<% end %>
<%= f.submit "Done", class: "btn btn-large btn-primary" %>
<% end %>
# == Schema Information
#
# Table name: restaurants
#
# id :integer not null, primary key
# name :string(255)
# created_at :datetime not null
# updated_at :datetime not null
#
class Restaurant < ActiveRecord::Base
attr_accessible :name, :job_attributes
has_many :jobs
has_many :users, :through => :jobs
has_many :positions
accepts_nested_attributes_for :jobs, :allow_destroy => true
validates :name, presence: true
end
class RestaurantsController < ApplicationController
before_filter :logged_in, only: [:new_restaurant]
def new
@restaurant = Restaurant.new
@user = current_user
@restaurant.jobs.build
end
def create
@restaurant = Restaurant.new(params[:restaurant])
if @restaurant.save
flash[:success] = "Restaurant created."
redirect_to welcome_path
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment