ActiveAdmin: Issue with permitted params - ruby-on-rails

When trying to update a user in ActiveAdmin, I get the following error:
Unpermitted parameter: :roles. Context: { controller: Admin::UsersController, action: update, request: #<ActionDispatch::Request:0x0000000110f2ff88>, params: {"_method"=>"patch", "authenticity_token"=>"[FILTERED]", "user"=>{"email"=>"user#localhost.com", "name"=>"User", "organization_id"=>"12", "partner_id"=>"1", "invite_now"=>"1", "social_listening_access"=>"0"}, "roles"=>{"global"=>[""], "partner"=>[""]}, "commit"=>"Update User", "controller"=>"admin/users", "action"=>"update", "id"=>"27"} }`
Here's how I set the permitted params:
permit_params :email, :name, :organization_id, :partner_id, :social_listening_access, :invite_now, roles_attributes: [:global, :partner]
Basically, I believe I need to permit params for both the user and roles. I can't move the roles attributes inside user.
I tried different ways of setting my permit_params. I am open to suggestions.

Related

Rails ActiveAdmin - pass polymorphic association parameters in link_to new

I'm new to ActiveAdmin and I have this problem:
I have two models: Picture and Content. Picture has many contents through polymorphic association.
I succeeded to make the button "Create content" from Picture show page pass contentable_type and picture id as contentable in the new_admin_content request as parameters using the following in app/admin/picture.rb :
action_item :new, only: :show do
link_to "Add content", new_admin_content_path(contentable_type: "Picture", contentable: picture)
end
and to receive the parameters I wrote the following in app/admin/content.rb :
permit_params :list, :of, :attributes, :on, :model, :contentable, :contentable_type
form do |f|
f.object.contentable_type = params[:contentable_type]
f.object.contentable = params[:contentabl]
puts f.object.contentable_type
puts f.object.contentable
f.inputs "Details" do
.
.
.
end
actions
end
But in this case, I get this error:
NoMethodError in Admin::Contents#new
undefined method `primary_key' for String:Class
The error is triggered by line f.object.contentable = params[:contentable]
When I pass only contentable_type to f.object whithout passing contentable, f.object saves contentable_type in its field, but submitting the form doesn't create a new record.
How can I save contentable as in its form field and succeed to make a create action on form submit?
The problem was solved by changing f.object.contentable = params[:contentabl] to f.object.contentable_id = params[:contentabl] (like the column name in the database) and adding all form parameters to permitted params

Problems acessing parameter from controller (collection_select)

I'm using a collection_select to pass the param product_id from a view to the controller, but i'm having problems on acessing that value. The other parameters are fine. If i do something like aux = params[:product_id] it saves the value 0 instead of 1, that is the value that the controller receives as you can see in the request log. Any help would be appreciated!
PS: I think it may be related with the curly brackets you can see around the product_id param as you can see in the request log
<%= collection_select(:params, :product_id, Product.all, :id, :name, :prompt => true) %>
Parameters:
{"utf8"=>"✓",
"authenticity_token"=>"hE4qrSZnI8LLy6sNzR2fkRxKZpFoOHZLun6Z/cIsHDxGcCaC+zVPLk1qYFhf6iuhbmsZX0us75FIiqJ7c06Mxw==",
"params"=>{"product_id"=>"1"},
"quantity"=>"1",
"event_id"=>"5",
"commit"=>"GO!",
"method"=>"post"}
You're sending a key params inside of the real params, to access your value do it like:
params[:params][:product_id]
or use:
collection_select(:object, :product_id, Product.all, :id, :name, prompt: true)
to get product_id in params

Can't save more that one object using nested form?

I am using nested_form gem and have following form:
= simple_nested_form_for #profile do |f|
= f.fields_for :person do |p|
= p.fields_for :web_profiles do |e|
= e.input :name
= e.input :url
= e.link_to_remove 'Remove'
= p.link_to_add 'Add Web Profile', :web_profiles
Person
has_many :web_profiles
has_one :profile
Profile
belongs_to :person
WebProfile
belongs_to :person
Forms are generated correctly but when I am trying to add second and more web profile, after submitting form I see only last web profile in database.
Started POST "/profiles" for 127.0.0.1 at 2013-06-17 14:45:48 +0200
Processing by ProfilesController#create as HTML
Parameters: {"utf8"=>"✓", "authenticity_token"=>"x2EEQXF9Blnvp0Ib1HG9XjHtaCEHwCNaBzPnZwxnlbI=", "profile"=>{"person_attributes"=>{"gender"=>"", "title"=>"", "first_name"=>"Darek", "last_name"=>"", "birthday"=>"", "place_of_birth"=>"", "company_id"=>"", "web_profiles_attributes"=>{"1371473134262"=>{"name"=>"zzzz", "url"=>"aaaa.pl", "_destroy"=>"false"}}}, "available_at"=>"", "last_contact_at"=>"", "remind_at"=>"", "blacklisted"=>"0", "overall_rating"=>""}, "commit"=>"Create Profile"}
Redirected to http://0.0.0.0:3000/profiles/2
Completed 302 Found in 18ms (ActiveRecord: 1.1ms)
This is log after I added two web profiles but in parameters for web_profiles_attributes I see only one web profile object.
Recently gem was updated and problem stopped occurs.
https://github.com/ryanb/nested_form/issues/271

Need help understanding how to add and/remove objects within nested form

I am having trouble adding the data from a nested model to my database.
I have a Photo model with the following relationship to a Monster model. I am able to add and remove my nested form for monsters, but can't figure out how to actually add a new Monster object when updating my Photo. This is based on 1 nested level form from the http://railscasts.com/episodes/196-nested-model-form-revised example, basically the Survey - Question relationship demonstrated.
class Photo < ActiveRecord::Base
attr_accessible :title, :monsters_attributes
has_and_belongs_to_many :monsters
accepts_nested_attributes_for :monsters, allow_destroy: true
end
class Monster < ActiveRecord::Base
attr_accessible :first_name, :last_name, :selected
has_and_belongs_to_many :photos
end
I'm sure the trouble is in my photos_controller and my poor understanding of how to use the params submitted when I update my photo form. I get the following params hash when submitting an edit photo form with two monsters. The first one is already associated and the second one I am trying to declare by clicking an "Add Monster" link, entering the first and last name, and submitting the form.
The "Add Monster" link is defined in my _form.html.erb file for a Photo and I am using a partial to render the fields. The helper function is declared in application_helper.rb.
_form.html.erb
…
<!-- this uses the _monster_fields partial -->
<%= f.fields_for :monsters do |builder| %>
<%= render 'monster_fields', f: builder %>
<% end %>
</table>
<%= link_to_add_fields "Add Monster", f, :monsters %>
...
application_helper.rb
module ApplicationHelper
def link_to_add_fields(name, f, association)
new_object = f.object.send(association).klass.new # make instance of monster association record, Monster.new
id = new_object.object_id # get id of new monster object, id = Monster.new.object_id
fields = f.fields_for(association, new_object, child_index: id) do |builder|
render(association.to_s.singularize + "_fields", f: builder) # render partial _monster_fields.html.erb
end
link_to(name, "#", class: "add_fields", data: {id: id, fields: fields.gsub("\n", '&#xA')}) # returns a link
end
end
params shown from edit (update) of photo to show:
{
"utf8"=>"✓",
"_method"=>"put",
"authenticity_token"=>"8dYkFrzdkulBv8YrZCHU2wfFh4v5LQc9q/JWPmsbDkc=",
"photo"=>{"title"=>"Family Gathering",
"monsters_attributes"=>{
"0"=>{"first_name"=>"Franken", "last_name"=>"Stein", "_destroy"=>"false", "id"=>"1"},
"1358291763102"=>{"first_name"=>"Ware", "last_name"=>"Wolf", "_destroy"=>"false"}
},
"monster_ids"=>"1"
},
"commit"=>"Update Photo",
"action"=>"update",
"controller"=>"photos",
"id"=>"1"
}
In my applcation.js file I define how to handle the adding and removal of fields. In .add_fields, the example uses the id for my newly created object but I don't see how to save my newly created monster in the monsters table.
application.js
$(document).ready(function() {
$('form').on('click', '.remove_fields', function(event) {
$(this).prev('input[type=hidden]').val('1');
$(this).closest('tr').hide();
event.preventDefault();
});
$('form').on('click', '.add_fields', function(event) {
time = new Date().getTime();
regexp = new RegExp($(this).data('id'), 'g');
$(this).before($(this).data('fields').replace(regexp, time))
event.preventDefault();
});
});
Overall, it's a pretty simple process but I'm not sure how to proceed further when it comes to actually saving a new Monster from my Photo edit view(creating a new active record object).
Could I have issues using a many-to-many relationship?
The example from railscasts is good but it doesn't quite go that far. I am grateful for any assistance someone may have.
http://railscasts.com/episodes/196-nested-model-form-revised
updated 1/16/13: My params hash and the error I get
ActiveModel::MassAssignmentSecurity::Error in PhotosController#update
Can't mass-assign protected attributes: monster_ids
app/controllers/photos_controller.rb:76:in block in update'
app/controllers/photos_controller.rb:75:inupdate'
{"utf8"=>"✓",
"_method"=>"put",
"authenticity_token"=>"1l62Sged52hnEHQoj0WPf/asRjcIpIKBBzsb7gVDRm0=",
"photo"=>{"title"=>"Family Gathering",
"monsters_attributes"=>{"0"=>{"first_name"=>"Franken",
"last_name"=>"Stein",
"_destroy"=>"false",
"id"=>"1"},
"1358356969634"=>{"first_name"=>"Scary",
"last_name"=>"Frank",
"_destroy"=>"false"}},
"monster_ids"=>"1"},
"commit"=>"Update Photo",
"id"=>"1"}
The beauty of the nested attributes stuff is that the implications for your controller code are non existent. You can keep doing
photo.update_attributes params[:photo]
and monsters will get updated and deleted as appropriate: photo.monsters_attributes= will be called by update_attributes with the data from params[:photo][:monsters_attributes]. Calling accepts_nested_attributes_for will have (among other things) created a suitable monsters_attributes= method

Update_attributes with a Date field

I ask the user to choose hos borthday from a date select :
<%= select_date Date.today, :prefix => :birthday %>
So in my action, i got a post like this :
{"utf8"=>"✓",
"_method"=>"put",
"authenticity_token"=>"k6uuDBVy2sM0YU7MtFIk7MsYpTQvQNnW5xuNRwn+OO0=",
"user"=>{"civility"=>"mr",
"password"=>"[FILTERED]",
"password_confirmation"=>"[FILTERED]",
"current_password"=>"[FILTERED]",
"newsletter_register"=>"1"},
"birthday"=>{"year"=>"2011",
"month"=>"10",
"day"=>"25"},
"commit.x"=>"69",
"commit.y"=>"9"}
But when i use :
#user.update_attributes(params[:user])
The birthday field is not updated. Any idea?
Thank you for help.
If you look at the submitted parameters, the birthday field is not included in the user hash, but rather as a separate field.
You probably want something like this instead (if you give us the code for your current form, I can give a more specific answer):
<%= f.date_select :birthday %>

Resources