yet another Can't mass-assign protected attributes: address post - ruby-on-rails

Preface: I am trying to create a customer for that has a nested addresses form. upon clicking create customer i get this error.
ActiveModel::MassAssignmentSecurity::Error in Admin::CustomersController#create
Can't mass-assign protected attributes: address
customer model
class Customer < ActiveRecord::Base
attr_accessible :name, :email, :phone, :addresses_attributes
has_many :addresses
accepts_nested_attributes_for :addresses, :allow_destroy => true
end
address model
class Address < ActiveRecord::Base
attr_accessible :street, :city, :state, :zip, :customer_id
belongs_to :customer
has_one :customer_id
end
Customers controller
ActiveAdmin.register Customer, :sort_order => "name_asc" do
# Menu item
menu :label => "Customers", :parent => "Administration"
filter :name
filter :created_at
filter :updated_at
action_item :only => [:show] do
link_to "Contacts", client_contacts_path( resource )
end
index do |t|
selectable_column
column(:name, sortable: :name) { |customer| link_to truncate(customer.name, length: 35), customer, title: customer.name }
column "Created", :sortable => :created_at do |customer|
customer.created_at.humanize
end
column "Updated", :sortable => :updated_at do |customer|
customer.updated_at.humanize
end
column "" do |customer|
restricted_default_actions_for_resource(customer) + link_to( "Contacts", client_contacts_path(customer), :class => "member_link" )
end
end
form :partial => "form"
show :title => :name do
panel "Customer Details" do
attributes_table_for resource do
row :name
row :email
row :phone
row :created_at do
resource.created_at.humanize
end
row :updated_at do
resource.updated_at.humanize
end
end
text_node(render :partial => "admin/addresses/show", :locals => { :address => resource.address })
end
end
end
To say i have tried everything is a lie because it won't work, though i have tried to get this to work for a while.

You must add
accepts_nested_attributes_for :addresses
in your Customer model.
By the way, why the error is in singular (Address and not Addresses)?
You must add :addresses_attributes to the attr_accessible call too.

Related

has_one relation in form in active admin

I have two models/tabels: room and room_location, that have a one on one relation:
class Room < ApplicationRecord
has_one :room_location
end
class RoomLocation < ApplicationRecord
belongs_to :room
end
And this is what i want to do in my form in rooms.rb:
ActiveAdmin.register Room do
menu parent: 'Rooms'
permit_params :name, :description, room_location_attributes: [:address, :city]
form do |f|
f.inputs 'Roomsdata' do
f.input :name, as: :string
f.input :description
f.has_one :room_location do |t|
t.inputs do
t.address
t.city
end
end
f.actions
end
end
end
The has_one doesnt work and if i do has_many, it says relation "room_locations" does not exist
You should write in the params room_location_id instead of attributes
ActiveAdmin.register Room do
menu parent: 'Rooms'
permit_params :name, :description, room_location_id
form do |f|
address_id = ''
cs = params[:id].present? ? Case.find(params[:id]) : nil
unless cs.nil?
address_id = cs.address.id unless cs.address.nil?
end
f.inputs 'Roomsdata' do
f.input :name, as: :string
f.input :description
f.input :room_location_id , :as => :select, :collection => room_location.order(address: :desc).to_a.uniq(&:address).collect {|room_location| [room_location.address, room_location.id] }, selected: room_location_id
f.input :room_location_id , :as => :select, :collection => room_location.order(city: :desc).to_a.uniq(&:city).collect {|room_location| [room_location.address, room_location.id] }, selected: room_location_id
f.actions
end
end
end

Adding and listing has_many through associations in Active Admin

I have the following setup:
class User < ActiveRecord::Base
has_many :device_ownerships, :dependent => :destroy
has_many :devices, :through => :device_ownerships
end
class device < ActiveRecord::Base
has_one :device_ownership, :dependent => :destroy
has_one :user, :through => :device_ownership
end
class deviceOwnership < ActiveRecord::Base
belongs_to :user
belongs_to :device
validates_uniqueness_of :device_id, :scope => :user_id
validates_uniqueness_of :user_id, :scope => :device_id
end
I am trying to achieve the following in Active Admin:
In Edit screen
1) List all devices that belong to user with an option to delete the device or destroy the deviceOwnership that connects device to user
2) have an option to create a new pairing user-device from existing devices (by creating new DeviceOwnership).
3) have an option to create new device and add it to user via new DeviceOwnership.
I listed my problems with what I have now in comments below:
ActiveAdmin.register User do
permit_params :email, :password, :password_confirmation, :role,
device_ownerships_attributes: [:device_id, :user_id],
devices_attributes: [:device_identifier]
index do |user|
user.column :email
user.column :current_sign_in_at
user.column :last_sign_in_at
user.column :sign_in_count
user.column :role
actions
end
filter :email
form do |f|
f.inputs "User Details" do
f.input :email
f.input :password
f.input :password_confirmation
f.input :role, as: :radio, collection: {Editor: "editor", Moderator: "moderator", Administrator: "administrator"}
#This one allows to create new devices but also lists all existing devices with option to modify their device_identifier column which I don't want
f.has_many :devices, :allow_destroy => true, :heading => 'Themes', :new_record => true do |cf|
cf.input :device_identifier
end
#This one lists all the devices but no option to remove any of them.
f.input :devices
#This one shows dropdownw with existing devices but allows to swap them
f.has_many :devices, :allow_destroy => true do |device_f|
device_f.input :device_identifier, :as => :select, :collection => device.all.map{ |device| [device.device_identifier] }, include_blank: false,
end
f.actions
end
end
end
The line
f.has_many :devices, :allow_destroy => true, :heading => 'Themes', :new_record => true do |cf|
cf.input :device_identifier
end
looks like it might do the job. You could check whether the cf.object is a new record and only let the user change the device_identifier in that case.
cf.input :device_identifier if cf.object.new_record?
or something like cf.input :device_identifier, input_html: { readonly: !cf.object.new_record? }
What do you think?

How to update attributes in many to many relation in rails?

I am new in rails. So i am not comprehending much of it. I have a database with three table. students table, courses table and registrations table.
My Railde models are as follows :
class Student < ActiveRecord::Base
attr_accessible :name
has_many :registrations
has_many :courses, :through => :registrations
validates :name, :presence => true
end
class Course < ActiveRecord::Base
attr_accessible :name
has_many :registrations, :dependent => :destroy
has_many :students, :through => :registrations
validates :name , :presence => true
validates :name, :uniqueness => true
end
class Registration < ActiveRecord::Base
attr_accessible :course_id, :student_id
belongs_to :course
belongs_to :student
validates :course_id, :student_id, :presence => true
validates :course_id, :uniqueness => {:scope => :student_id}
validates :student_id, :uniqueness => {:scope => :course_id}
end
......................
Controller action for updating student :
def update
#student = Student.find(params[:id])
if #student.update_attributes(params[:student])
redirect_to students_path, :notice => "Registration completed"
else
render 'edit'
end
end
................
View :
<%=form_for #student do |f| %>
<p>
<%= f.label :name, "Name" %>
<%= f.text_field :name %>
</p>
<p>
<%= render('course_list') %>
</p>
<p>
<%= f.submit %>
</p>
<% end %>
...............
_course_list partial :
Select Courses :<br/>
<p>
<% Course.all.each do |course| %>
<%=check_box_tag "student[course_ids][]", course.id, `enter code here`#student.course_ids.include?(course.id) %>
<%= course.name %> <br/>
<% end %>
</p>
.............................
when i submit the update button, i got an error
Can't mass-assign protected attributes: course_ids
.......
parameters :
{"utf8"=>"✓",
"_method"=>"put",
"authenticity_token"=>"j/lDE5bv1gWkfadQ6Cag6hGjg5nD2Ikad9vHOJTE7Pc=",
"student"=>{"name"=>"Galib",
"course_ids"=>["2",
"3"]},
"commit"=>"Update Student",
"id"=>"6"}
.........................
What i want to do is, if update button is clicked, both students table and registrations table need to be updated. Please help.
In models define the associations
class Student < ActiveRecord::Base
attr_accessible :name
has_many :registrations
has_many :courses, :through => :registrations
validates :name, :presence => true
accepts_nested_attributes_for :courses
attr_accessible :course_ids
end
class Course < ActiveRecord::Base
attr_accessible :name
has_many :registrations, :dependent => :destroy
has_many :students, :through => :registrations
validates :name , :presence => true
validates :name, :uniqueness => true
accepts_nested_attributes_for :students
end
class Registration < ActiveRecord::Base
attr_accessible :course_id, :student_id
belongs_to :course
belongs_to :student
validates :course_id, :student_id, :presence => true
validates :course_id, :uniqueness => {:scope => :student_id}
validates :student_id, :uniqueness => {:scope => :course_id}
accepts_nested_attributes_for :course
end
In Controller
def update
#student = Student.find(params[:id])
if #student.update_attributes(params[:student])
#student.courses.build
redirect_to students_path, :notice => "Registration completed"
else
render 'edit'
end
end
It may help you
The error is telling you that you have a permission error - course_ids can't be posted to from a form. More specifically, you have attr_accessible :name in the Student model which means that is the only attribute that can be saved when you save a Student record using a form (attr_accessible dictates what can be mass assigned).
Try changing your Student model to:
attr_accessible :name, :registrations_attributes
accepts_nested_attributes_for :registrations
You are trying to created a nested model form, so accepts_nested_attributes_for is used for this.
If you want to have the Registrations table updated, as well, you're going to have to tell the partial so that it knows that the partial is updating a different model than Student.
<%= f.fields_for :registrations do |registration| %>
<%= render('course_list') %>
<% end %>
In case of MongoDB, For implementing many to many in rails model you have to use has_and_belongs_to_many instead of simple has_many

Rails: undefined method 'MODELNAME' ? Am I saving the wrong thing on create?

Currently I'm making an app for users to create picture albums. The catch is that there can be multiple owners for each album, so in the form to create an album there is a check box portion where you highlight your friends names and "invite them" to be an owner. However, I am having a hard time getting it to work since it's giving me an error in my AlbumsController#create. The error is :
undefined method 'user' for #<Album:0x007fcd9021dd00>
app/controllers/albums_controller.rb:43:in 'create'
Here's my form
<%= form_for ([#user, #album]), :html => { :id => "uploadform" } do |f| %>
<div class="formholder">
<%= f.label :name %>
<%= f.text_field :name %>
<br>
<label>Hosts</label>
<% #user.friends.each do |friend| %>
<%= friend.name %>
<%= check_box_tag 'album[user_ids][]', friend.id, #album.users.include?(friend) %>
<% end %>
<%= f.label :description %>
<%= f.text_area :description %>
<br>
<%=f.submit %>
</div>
<% end %>
The checkboxes return an array of friend.id values that I want to invite to be owners for the album. The tricky part here is that I have a imaginary pending_album column in my join album_user model. Here's what is sending me errors that I can't figure out how to fix:
albums controller
def create
#user = User.find(params[:user_id])
#album = #user.albums.build(params[:album], :status => 'accepted')
#friends = #user.friends.find(params[:album][:user_ids])
for friend in #friends
params[:album1] = {:user_id => friend.id, :album_id => #album.id, :status => 'pending'}
AlbumUser.create(params[:album1])
end
#the next line is where the error occurs. why???
if #user.save
redirect_to user_album_path(#user, #album), notice: 'Album was successfully created.'
else
render action: "new"
end
end
album model
class Album < ActiveRecord::Base
attr_accessible :name, :description, :user_ids
validates_presence_of :name
validates :user, :uniqueness => true
has_many :album_users
has_many :users, :through => :album_users
has_many :photos
end
user model
class User < ActiveRecord::Base
has_secure_password
attr_accessible :email, :name, :password, :password_confirmation, :profilepic
validates_presence_of :password, :on => :create
validates_format_of :name, :with => /[A-Za-z]+/, :on => :create
validates_format_of :email, :with => /\A([^#\s]+)#((?:[-a-z0-9]+\.)+[a-z]{2,})\Z/i, :on => :create
validates_length_of :password, :minimum => 5, :on => :create
# validates :album, :uniqueness => true
has_many :album_users
has_many :albums, :through => :album_users, :conditions => "status = 'accepted'"
has_many :pending_albums, :through => :album_users, :source => :album, :conditions => "status = 'pending'"
accepts_nested_attributes_for :albums
has_many :friendships, :dependent => :destroy
has_many :friends, :through => :friendships, :conditions => "status = 'accepted'"
has_many :requested_friends, :through => :friendships, :source => :friend, :conditions => "status = 'requested'", :order => :created_at
has_many :pending_friends, :through => :friendships, :source => :friend, :conditions => "status = 'pending'", :order => :created_at
has_attached_file :profilepic
before_save { |user| user.email = email.downcase }
def name_with_initial
"#{name}"
end
private
def create_remember_token
self.remember_token = SecureRandom.urlsafe_base64
end
end
album_user join table model
class AlbumUser < ActiveRecord::Base
attr_accessible :user_ids, :album_id, :status, :user_id
belongs_to :album
belongs_to :user
end
parameters (looks a little fishy... especially since album_id is nil):
{"utf8"=>"✓",
"authenticity_token"=>"xkIi6+1vjEk4yQcFs9vI1uvI29+Gyuenyp71vhpX9Hw=",
"album"=>{"name"=>"123123",
"user_ids"=>["27",
"28"],
"description"=>"123123"},
"commit"=>"Create Album",
"user_id"=>"29",
"album1"=>{"user_id"=>28,
"album_id"=>nil,
"status"=>"pending"}}
I'm not too sure what I'm doing wrong. Can someone help please!!
The error message is:
undefined method 'user' for #<Album:0x007fcd9021dd00>
All right, so something's calling Album#user. Line 46 in the controller is #user.save, so how is user getting called?
class Album < ActiveRecord::Base
attr_accessible :name, :description, :user_ids
validates_presence_of :name
validates :user, :uniqueness => true # <-- bam!
has_many :album_users
has_many :users, :through => :album_users
has_many :photos
end
save triggers validations, including that line right there, which validates that user is unique. Since this is exploding, I'm guessing that user is a column that doesn't exist, either because a) it's not defined in a migration or b) you didn't run the migration.
(As an aside, I'm rather confused as to why you would have a unique user attribute on Album, an AlbumUser model with both user_id and user_ids, and a User model. Maybe it makes sense, but I think it's more likely that it all needs a cleanup.)
Your album really has many several users? Then that should return method "user"? If want that one user owned album you must use belongs_to in your model. Then method "user" will return correct user to album

Rails nested form with has_many :through, not saving the data to joining table

I am kinda new to Rails and this is my first post to StackOverflow.
Say I have 3 models:
class Product < ActiveRecord::Base
default_scope :order => :title
has_many :line_items
has_many :promo_products
has_many :promotions, :through => :promo_products, :foreign_key => :promotion_id
before_destroy :ensure_not_referenced_by_any_line_item
before_destroy :ensure_not_referenced_by_any_promo_product
validates :title, :presence => true, :uniqueness => true
validates :description, :presence => true
validates :price, :numericality => {:greater_than_or_equal_to => 0.01}
private
def ensure_not_referenced_by_any_line_item
if line_items.empty?
return true
else
errors.add(:base, 'Line Items present')
return false
end
end
def ensure_not_referenced_by_any_promo_product
if promo_products.empty?
return true
else
errors.add(:base, 'Some promotions are still in effect')
return false
end
end
end
class Promotion < ActiveRecord::Base
CART_OR_PRODUCT = ['Cart', 'Product']
PROMOTION_TYPE = ['Percentage based', 'Value based']
has_many :promo_products
accepts_nested_attributes_for :promo_products
has_many :products, :through => :promo_products, :foreign_key => :product_id
accepts_nested_attributes_for :products
#attr_accessible :promo_products_attributes, :title, :description, :cart_or_product, :promotion_type, :discount, :minimum_price, :minimum_quantity
validates :title, :description, :presence => true
validates :cart_or_product, :inclusion => {:in => CART_OR_PRODUCT, :message =>
"is invlaid. Please select a valid option"}
validates :promotion_type, :inclusion => {:in => PROMOTION_TYPE, :message =>
"is invalid. Please select a valid option"}
validates :discount, :minimum_price, :numericality => {:greater_than_or_equal_to => 0.00}
validates :minimum_quantity, :numericality => {:greater_than_or_equal_to => 0}
end
class PromoProduct < ActiveRecord::Base
belongs_to :promotion
belongs_to :product
accepts_nested_attributes_for :products
end
In the promotions new page, I would like to show list of products that could be part of a promotion. A user may select 0, 1 or more products, depending on the type of promotion.
In the action new of promotions_controller, I built like this:
#promotion.promo_products.build.build_product
In the _form of promotions, I needed to show the list of products for user to select. I made a nested form like:
<%= form_for(#promotion) do |f| %>
<!-- other promotion fields -->
<%= f.fields_for :promo_products do |pp| %>
<%= pp.fields_for :products do |p| %>
<div class="field">
<%= f.label "Products" %><br />
<%= collection_select :promo_product, :product_id, Product.all, :id, :title {:selected => #promotion.product_ids}, {:multiple => true} %>
</div>
<% end %>
<% end %>
<% end %>
I have 2 issues.
First my code throws an error:
ArgumentError in PromotionsController#new
No association found for name `products'. Has it been defined yet?
If I change the line in PromoProduct model:
accepts_nested_attributes_for :products
to
accepts_nested_attributes_for :product
Then there are no errors, and everything works fine.
The data doesn't get saved to promo_product table. I have the create action in promo_product controller as:
def create
#promotion = current_promotion
products = Product.select(:id => params[:product_id])
products.each do |p|
promo_product = #promotion.promo_products.build(p)
promo_product.save
end
##promo_product = PromoProduct.new(params[:promo_product])
redirect_to promotions_path
end
How can I go about it?
Thank you.
You shouldn't put the "accept_nested_attribute_for" in the association table PromoProducts. It should exist in the model that you want to use for creating association to another model. "accept_nested_attribute_for" IIRC simply inserts an "[association]_attributes=" method for your model. For instance, if you add this method to your Product class for Promotion, you will get "promotion_attributes=" method inserted in the Product class. Then a nested form can use this function to create new objects with a hash that represents the model and association.
Base on the above, the create action shouldn't be in PromoProduct controller, instead it should be in Promotion controller.
<%= form_for(#promotion) do |f| %>
<!-- other promotion fields -->
<%= f.fields_for :products do |pp| %>
<div class="field">
<%= f.label "Products" %><br />
<%= collection_select :promo_product, :product_id, Product.all, :id, :title {:selected => #promotion.product_ids}, {:multiple => true} %>
</div>
<% end %>
<% end %>
I don't know without trying if the above collection_select line is correct. But you can debug this by checking the parameter returned by the form to the controller in the server console log. Basically you should see a nested hash of
{:promotion => {:products => ...}}
Let me know if you need more help on this. In my solution I used a combination of select_tag and options_from_collection_for_select. (But I don't recall the behavior of all these offhand without looking at the API doc.)
Lastly, do you need the :through model? I think since you created the through model you need to handle saving that in your create action. But since you don't have other attributes on the PromoProducts table I wonder if you want to simply leave it as a HABTM association and let rails deal with the rest?

Resources