rails accept nested atributes for a polymorphic association - ruby-on-rails

I have an error "Can't mass-assign protected attributes: Upload", but I have assigned it to be accessible.
This is a nested form with a polymorphic association.
Models
class Upload < ActiveRecord::Base
attr_accessible :link, :post_id
belongs_to :uploadable, polymorphic: true
end
class Post < ActiveRecord::Base
attr_accessible :description, :title, :uploads_attributes
has_many :uploads, as: :uploadable
accepts_nested_attributes_for :uploads, :reject_if => lambda { |a| a[:content].blank?
}, :allow_destroy => true
end
I tried too put accept_nested ... for :uploadable but tells me dont exist the association
The action new on the controller is this one
def new
#post = Post.new
#post.uploads.new
end
and here is the form for create
<%= form_for [:admin,#post], remote: true, :html => {:multipart => true} do |f| %>
<div class="field">
<%= f.label :title %><br />
<%= f.text_field :title%>
</div>
<div class="field">
<%= f.label :description%><br />
<%= f.text_area :description %>
</div>
<div>
<%= f.fields_for :upload do |builder| %>
<%= render 'upload_fields', f: builder %>
<% end %>
<%= link_to_add_fields "Add Upload", f, :uploads %>
</div>
<div class="actions">
<%= f.submit%>
</div>
<% end %>
The partial ...
<fieldset>
<%= f.label :file %><br />
<%= f.file_field :file %>
<%= f.hidden_field :_destroy %>
<%= link_to "remove", '#', class: "remove_fields" %>
</fieldset>
Dont think the javascript affects, so Im not going to put it here.
How I cna solve "Can't mass-assign protected attributes" on polymorphic asociations ?
Plz need help on this anyone. Cant belive I cant upload files, looks so simple on tutorials, and Its not working, or I get a Can't mass assign orthe upload its not saved ....

Try to use #post.uploads.build instead of #post.uploads.new

The associated model needs to know the identity of her parent to save the relationship.
I recommend you the following railscast: Polymorphic Association.
#uploads_controller.rb
before_filter :load_uploadable
def create
#upload = #uploadable.uploads.new(params[:upload])
....
end
private
def load_uploadable
resource, id = request.path.split('/')[1, 2] # /posts/1
#uploadable = resource.singularize.classify.constantize.find(id)
end

This line inside your view:
<%= f.fields_for :upload do |builder| %>
Should be this:
<%= f.fields_for :uploadable do |builder| %>
Because the association on the Post model is called "uploadable", not "upload".
For nested attributes to work, you will need to specify the model does accept nested attributes for this model, which can be done by putting this line underneath the belongs_to in your model:
accepts_nested_attributes_for :uploadable
And then you will need to make these attributes accessible, which you can do with this:
attr_accessible :uploadable_attributes

Related

Create multiple new records from checkboxes in form using nested attributes in Rails

I'm trying to come up with a contact form that creates a contact record and potentially multiple location records, if multiple locations are checked in a list of checkboxes. I thought of having all location records created and then destroyed, if they aren't checked. I don't think that's optimal though.
I'm using many to many relationships in the models.
This is what they look like at the moment:
contact.rb
class Contact < ApplicationRecord
has_many :contact_locations, dependent: :destroy
has_many :locations, through: :contact_locations
accepts_nested_attributes_for :contact_locations, allow_destroy: true, reject_if: :empty_location?
private
def empty_location?(att)
att['location_id'].blank?
end
end
location.rb
class Location < ApplicationRecord
has_many :locations, dependent: :destroy
has_many :contacts, :through => :contact_locations
has_many :contact_locations
end
contact_location.rb
class ContactLocation < ApplicationRecord
belongs_to :location
belongs_to :contact
end
contacts_controller.rb
def new
#contact = Contact.new
#locations = Location.all
4.times {#contact.contact_locations.new}
end
private
def contact_params
params.require(:contact).permit(:name, :phone, ..., contact_locations_attributes: [:location_ids])
end
new.html.rb
<%= form_with model: #contact do |f| %>
...
<%= #locations.each do |location| %>
<%= f.fields_for :contact_locations do |l| %>
<%= l.check_box :location_id, {}, location.id, nil %><%= l.label location.name %>
<% end %>
<% end %>
...
<% end %>
Does anyone how to make it work properly?
I'm working on Ruby 2.5.1 and Rails 5.2.1.
Thanks a lot.
I think your solution is the form objects pattern.
You can have something like this:
<%= form_for #user do |f| %>
<%= f.email_field :email %>
<%= f.fields_for #user.build_location do |g| %>
<%= g.text_field :country %>
<% end %>
<% end%>
And convert it in something more readable that permits you to instance the locations inside the registration object, checking the value of the checkboxes.
<%= form_for #registration do |f| %>
<%= f.label :email %>
<%= f.email_field :email %>
<%= f.input :password %>
<%= f.text_field :password %>
<%= f.input :country %>
<%= f.text_field :country %>
<%= f.input :city %>
<%= f.text_field :city %>
<%= f.button :submit, 'Create account' %>
<% end %>
Here you will find how to apply the pattern: https://revs.runtime-revolution.com/saving-multiple-models-with-form-objects-and-transactions-2c26f37f7b9a
I ended up making it work with Kirti's suggestion on the following question:
Rails Nested attributes with check_box loop
It turns out I needed to make a small adjustment in my form's fields_for tag.
Thanks a lot the help!

Rails - Nested Form add works, but records are not deleted

I'm using nested forms in my rails app and I have a simple parent-child setup:
Models:
class Experiment < ActiveRecord::Base
has_many :exptypes, :dependent => :destroy
accepts_nested_attributes_for :exptypes, :allow_destroy => true
class Exptype < ActiveRecord::Base
belongs_to :experiment
Views:
Parent:
<%= nested_form_for(#experiment) do |f| %>
<%= f.fields_for :exptypes do |builder| %>
<%= render 'exptype_fields', :f => builder %>
<% end %>
<p><%= f.link_to_add "Add an Experimental Type", :exptypes %></p>
Child:
<h2>Experiment type</h2>
<p>
<div class="field">
<%= f.link_to_remove "Remove this Experiment" %>
</div>
<div class="field">
<%= f.label :type_name %><br>
<%= f.text_field :type_name %>
</div>
</p>
The addition of many children works fine, but when I try to remove any from the list (by clicking the button created by link_to_remove), the nested form disappears, but when I hit update, the show form still shows the "deleted" child.
Found the error, I didn't have :_destroy in my permitted parameters in my controller.
Hope this helps someone else!
Edit:
My controller's strong params method looks as follows:
def experiment_params
params.require(:experiment).permit(:_destroy, :experiment_number, :date, :title, :pi,
:biocontrol_run_num, :goals, :description, :str, :exp_type)
end

Rails - :_destroy method not working

I am trying to use :_destroy method in a nested form, but it just does not work
There are two models:
class Setting < ActiveRecord::Base
attr_accessible :category, :name, :setting_items_attributes, :_destroy
attr_accessor :_destroy
has_many :setting_items, :dependent => :destroy
accepts_nested_attributes_for :setting_items, :reject_if => lambda { |a| a[:content].blank? }, :allow_destroy => true
end
class SettingItem < ActiveRecord::Base
attr_accessible :setting_id, :value
belongs_to :setting
end
In the controller I create a instance:
def edit
#setting = Setting.find(params[:id])
#setting.setting_items.build
end
And the form looks like this:
<%= form_for(#setting) do |f| %>
<div class="field">
<%= f.label :category %>
<%= f.text_field :category %>
</div>
<div class="field">
<%= f.label :name %>
<%= f.text_field :name %>
</div>
<hr>
<h3>Params:</h3>
<%= f.fields_for :setting_items do |s| %>
<span>
<div class="fields">
<%= s.text_field :value %>
<%= s.hidden_field :_destroy %>
<%= link_to_function "delete", "remove_fields(this)"%>
</div>
<% end %>
<div class="actions">
<%= f.submit "Update", :class => "btn btn-primary"%>
</div>
<% end %>
Also the function I use is here:
function remove_fields(link){
$(link).prev("input[type=hidden]").val("1");
$(link).parent().fadeOut("slow");
}
So the setting_items form is simply not working at all, It shows the update is successful, but nothing is actually added or deleted.
for example->your model associations are as follows: just follow the below steps to make use of magical _destroy attribute
####parent model
plan.rb
has_many :members,:dependent => :destroy
#this is important you want to destroy nested records
accepts_nested_attributes_for:members,:allow_destroy => true
attr_accessible :members_attributes
##child model
member.rb
belongs_to :plan
######controller
def edit
#plan.Plan.find(params[:id])
end
#####edit.html.erb
<%= form_for #plan do |f| %>
<%= f.fields_for :members do |member| %>
<div class="member">
<%= member.text_field :title%>
<%= image_tag 'delete.png',:class =>'remove_member',:id=>member.id %>
<!-- we need to set this hidden field value as 1 to mark it to be deleted during save/update of parent-->
<%= member.hidden_field :_destroy, :class => 'delete_member', :member_id => member.id %>
<!--similar to
<input id="plan_members_attributes_0__destroy" class="delete_member" type="hidden" value="false" name="plan[members_attributes][0][_destroy]" member_id="#{id of member}">
-->
</div>
<%end%>
<%end%>
##add js onclick of remove button/image
$('.delete_member').click(function(){
//remove div from screen
$(this).closest('.member').remove();
//get relevant id to remove/mark as delete
id =jQuery(this).attr('id');
//remove/mark the nested model/record as ready for deletion for rails by adding true/1 value
$("input[member_id="+id+"]").attr('value',1);
})

Rails Polymorphic Nested Attribute

I have a polymorphic model and want to have a nested form use this model. I am getting no errors but the form is not displaying the nested field. Here are my models and stripped down form:
Polymorphic Model
class SeoMapping < ActiveRecord::Base
belongs_to :mappingtable, :polymorphic => true
attr_accessible :seo_url
validates :seo_url, :presence => true, :uniqueness => true
end
Page model using the Polymorphic Model
class Page < ActiveRecord::Base
has_one :seo_mappings, :as => :mappingtable, :dependent => :destroy
accepts_nested_attributes_for :seo_mappings
attr_accessible :content, :h1, :meta_description, :title, :seo_mappings_attributes
.........
end
Now stripped down form
<%= form_for(#page) do |f| %>
<% if #page.errors.any? %>
.......
<% end %>
<div class="field">
<%= f.fields_for :seo_mappings do |builder| %>
<%= builder.label :seo_url %><br />
<%= builder.text_field :seo_url %>
<% end %>
</div>
<div class="field">
<%= f.label :title %><br />
<%= f.text_field :title %>
</div>
.........
<div class="actions">
<%= f.submit %>
</div>
<% end %>
I cant see why it does not display the fields_for elements. If I comment out accepts_nested_attributes_for the field then displays. Can you see where I am going wrong ?
TY
Possibly a silly question but is this the create or edit action you're talking about? And if it's create, did you call #page.build_seo_mapping or something in the controller?
Also (may be unrelated) if you use has_one, usually you want to use a singular noun, so has_one :seo_mapping instead of mappings.

Creating forms for Multiple Nested Resources in Rails 3

I have been following Ryan Bates' tutorial on nested forms Railscast 196
The form for the new action shows the nested attributes for quizzes but does not show nested attributes for the key. I am guessing this is because quizzes have a has_many relationship where key has a has_one relationship... But I cannot figure out what I'm doing wrong?
Any help is much appreciated!
This is my model:
class Repository < ActiveRecord::Base
has_many :quizzes, :dependent => :destroy
has_one :key, :dependent => :destroy
accepts_nested_attributes_for :key, :quizzes
end
This is my controller:
def new
#repository = Repository.new
3.times { #repository.quizzes.build }
#repository.key = Key.new
respond_to do |format|
format.html # new.html.erb
format.xml { render :xml => #repository }
end
end
This is my view:
<div class="field">
<%= f.label :wp_uid %><br />
<%= f.text_field :wp_uid %>
<% f.fields_for :quizzes do |quiz_fields| %>
<p>
<%= quiz_fields.label :name, "Name" %><br />
<%= quiz_fields.text_field :name %>
</p>
<% end %>
<% f.fields_for :key do |key_fields| %>
<div class="field">
<%= key_fields.label :value, "Value" %><br />
<%= key_fields.text_field :value %>
</div>
<div class="field">
<%= key_fields.label :expiry, "Expiry" %><br />
<%= key_fields.date_select :expiry %>
</div>
<% end %>
</div>
<div class="actions">
<%= f.submit %>
</div>
<% end %>
You should try modifying your fields_for blocks to use <%= %>
Try changing to:
<%= f.fields_for :key do |key_fields| %>
The railscast could have been made before the change in Rails 3 to use <%= %> instead of <%%>.
Ryan has a nested_form gem that you may find useful for this as well. I haven't tried using it yet, but plan to next time I start a new project.
https://github.com/ryanb/nested_form
Try building the key object as
#reposity.build_key
From the rails docmentation
http://api.rubyonrails.org/classes/ActiveRecord/Associations/ClassMethods.html#label-Unsaved+objects+and+associations
If you wish to assign an object to a has_one association without saving it, use the build_association method. The object being replaced will still be saved to update its foreign key.

Resources