Cocoon First Option Not Saving - Nested Fields - ruby-on-rails

I'm using Cocoon for a nested form, however the first set of fields do not save into the database, if I create a second row they seem to save just fine?
I'm guessing its just something I'm overlooking
class Form < ActiveRecord::Base
has_many :questions
accepts_nested_attributes_for :questions, :reject_if => :all_blank, :allow_destroy => true
end
class Question < ActiveRecord::Base
belongs_to :form
end
----- form_controller.rb
def new
#form = Form.new
#form.questions.build
end
def create
#form = Form.new(form_params)
if #form.save
redirect_to action: "show", id: #form.id
else
render('new')
end
end
def form_params
params.require(:form).permit(:title, :description, :status, questions_attributes: [:form_id, :question_type, :question_text, :position, :answer_options, :validation_rules, :done, :_destroy])
end
<%= simple_form_for Form.new ,:url => {:action => :create} do |f| %>
<div class="section-row">
<div class="section-cell">
<%= f.label :title, "Form Title" %>
<%= f.text_field :title, placeholder: "Form Title" %>
<br/></br>
<%= f.label :description, "Form Description" %>
<%= f.text_area :description, placeholder: "Form Description" %>
<%= f.hidden_field :status, value: "online" %>
</div>
</div>
<div class="section-row">
<div class="section-cell">
<div id="questions">
<%= simple_fields_for :questions do |question| %>
<%= render 'question_fields', :f => question %>
<%= link_to_add_association 'add question', f, :questions %>
<% end %>
</div>
</div>
</div>
<div class="section-row">
<div class="section-cell">
<%= f.submit "Create Ticket", class: "btn btn-primary btn-lg" %>
</div>
</div>
<% end %>
---- _question_fields.html.erb
<div class='nested-fields'>
<%= f.label :question_type, "Question Type" %>
<%= f.select :question_type,
options_for_select([['Text Box','textbox'],['Dropdown Options','select'], ['Mutiple Choice','radio']], params[:question_type]),
{}, { :class => '' } %>
</div>

You forgot the f in front of the simple_fields_for : then the simple_fields_for has no knowledge of the form, nor the associations, so it will name the parameters posted to the controller differently, so it will be blocked by your strong parameters definition.
So if you write
f.simple_fields_for :questions do |question|
it should just work ;)
Smaller remarks:
the link_to_add_association should be outside of the loop, otherwise it will not be visible if there are none, and will be displayed for each question (which may or may not make sense in your UI).
you should add the id inside the strong parameters for questions_attributes (important for editing/deleting)

You wrote the link_to_add_association inside the simple_fields_for
you should write your form as follows (as documented):
<div id="questions">
<%= f.simple_fields_for :questions do |question| %>
<%= render 'question_fields', f: => question %>
<% end %>
<div class="links">
<%= link_to_add_association 'add task', f, :questions %>
</div>

Related

edit associated model by input form?

Order has_many jobs
Job belongs to order
And I want to edit attributes of #job.order:
<% order = #job.order %>
<%= simple_form_for [#job, order],
url: job_path(#job),
method: :put,
remote: true do |f| %>
<%= f.input :order_status, input_html: {class: 'form-control'} %>
(...)
<% end %>
any way to do it by just using input in simple form?
in job.rb
accepts_nested_attributes_for :order
in form.html.erb
simple_form_for #job do |f|
f.simple_fields_for #job.order do |order_form|
order_form.input :status
end
end
in jobs_controller.rb
params.require(:job).permit(:something, :something_else, :order_attributes => [:status])
You can use the excellent Cocoon gem https://github.com/nathanvda/cocoon to manage nested relationships, including the ability to easily add new nested relationships.
class Job < ActiveRecord::Base
has_many :orders
accepts_nested_attributes_for :orders, reject_if: :all_blank, allow_destroy: true
end
class Order < ActiveRecord::Base
belongs_to :job
end
Note the pluralization.
_form.html.erb*
<%= form_for #job do |f| %>
<%= f.label :job_name %>
<%= f.text_field :name %>
<div id='order'>
<%= f.fields_for :orders do |order| %>
<%= render 'order_fields', f: order %>
<% end %>
<div class='links'>
<%= link_to_add_association 'add order', f, :orders %>
</div>
<%= f.submit %>
<% end %>
_order_fields.html.erb partial
<div class='nested-fields'>
<%= f.label :order_name %>
<%= f.text_field :order_name %>
</div>
<%= link_to_remove_association "remove order", f %>

Rails: File to Upload does not get passed from Form to the controller

This is the Form. All of the fields get passed (and saved) except the one containing the File.
I have checked that using the
render plain: params[:article].inspect method
giving out this (I have entered the value "n" for all fields):
{"country"=>"n", "region"=>"n", "town"=>"n", "street"=>"n", "company"=>"n", "title"=>"n", "content"=>"n"}
I am leaving out superfluous fields here to make the Form shorter:
<%= form_for(#article, html: { multipart: true }) do |f| %>
<%= render 'shared/error_messages', object: f.object %>
<%= f.label :country %>
<%= f.text_field :country, :required => true,
placeholder: "enter country" %>
</div>
<%= f.label :content %>
<%= f.text_field :content, :required => true, placeholder: "town..." %>
</div>
</div>
</div>
</div>
<span class="picture">
<%= form_for #article, html: { multipart: true } do |f| %>
<%= f.text_field :title %>
<%= fields_for :pictures do |ff| %>
<%= ff.file_field :picture %>
<% end %>
<% end %>
</div>
I have also tried the slight variation here, but no change
http://api.rubyonrails.org/classes/ActionView/Helpers/FormHelper.html#method-i-fields_for
The create method at the Controller is like this:
def create
#article = current_user.articles.build(article_params)
if #article.save
flash[:success] = "Article created!"
redirect_to root_url
else
render 'articles/new'
end
end
and yes, the new method in the Articles controller, is like I was indicated by peers here:
def new
#article = current_user.articles.build
#article.pictures.build
end
The Article Model
class Article < ActiveRecord::Base
belongs_to :user
has_many :pictures
accepts_nested_attributes_for :pictures, allow_destroy: true
And the pictures Model
class Picture < ActiveRecord::Base
belongs_to :article
mount_uploader :picture, PictureUploader
end
Change your <%= fields_for :pictures do |ff| %> to <%= f.fields_for :pictures do |ff| %>

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 nested form edits

I am trying to figure out the whole nested form thing, and am having issues rending the form for editing.
I'm not doing this the "typical way" with a new and create action, because i'm not actually going to be saving anything. I'm just using this form to generate some tests.
Here is what I have..
model.rb
class Model < ActiveRecord::Base
has_many :columns
has_many :associations
accepts_nested_attributes_for :associations, :reject_if => lambda { |a| a[:type].blank? }
accepts_nested_attributes_for :columns, :reject_if => lambda { |a| a[:name].blank? }
attr_accessible :name, :associations_attributes, :columns_attributes
end
column.rb
class Column < ActiveRecord::Base
belongs_to :model
attr_accessible :db_index, :mass_assign, :max_length, :min_length, :name, :required, :unique, :columns_attributes
validates :name, :presence => true
end
generator_controller.rb
def model
if params['submit']
#model = Model.new(params[:model])
#model.columns.build
else
#model = Model.new
3.times { #model.columns.build }
end
render 'generator/model'
end
form view
<%= nested_form_for(#model, :url => '/model', :html => {:style => 'width:100%;'}) do |f| %>
<%= flash_helper() %>
<h3>Model Name</h3>
<%= f.text_field :name %>
<h3>Add Table Columns</h3>
<div id="columns">
<% f.fields_for :columns do |builder| %>
<div class="float_left column">
<%= builder.label :name %>
<br>
<%= builder.text_field :name, :size => 20 %>
</div>
<div class="float_left column">
<%= builder.label :mass_assign %>
<br>
<%= builder.check_box :mass_assign %>
</div>
<div class="float_left column">
<%= builder.label :required %>
<br>
<%= builder.check_box :required %>
</div>
<div class="float_left column">
<%= builder.label :unique %>
<br>
<%= builder.check_box :unique %>
</div>
<div class="float_left column">
<%= builder.label :db_index %>
<br>
<%= builder.check_box :db_index %>
</div>
<div class="float_left column">
<br>
<%= image_tag "delete-icon24x24.png", :class => "btnDel clickable" %>
</div>
<br class="clear_float">
<% end %>
<%= f.link_to_add image_tag('add-icon.png'), :columns %>
</div>
<br><br>
<input type="submit" name="submit">
<p>
<%= #results %>
</p>
<% end %>
So the problem is, the form when new is not displaying the columns that "3.times { #model.columns.build }" is supposed to do. Also, when I submit the form, I need to figure out why it is getting rid of all of those nested attributes that were added. So each time I submit the form, and it renders the form again, all the attributes are gone. (The "model" attributes are still there, but the "column" attributes reset each time)
Any ideas on what i'm doing wrong?
Thanks
I am embarrassed to even admit this, but hopefully it will help someone else with one more thing to check if they have the same problem.
<% f.fields_for :columns do |builder| %>
changed to:
<%= f.fields_for :columns do |builder| %>
Yeah.. can't believe I did that...

How to make text fields for nested_attributes in rails for selected records?

I have a three models Report,Question,Answer
Answer
belong_to :question
Question
belong_to :reports
has_many :answers, :dependent => :destroy
accepts_nested_attributes_for :answers, :allow_destroy => true
Reports
has_many :questions, :dependent => :destroy
accepts_nested_attributes_for :questions, :allow_destroy => true
While creating a new report some questions are randomly picked to be added to the report and show form in this way :
Report Form
<%= form_for #report do |f| %>
<div class="field">
<%= f.fields_for :questions do |builder| %>
<%= render "question_fields", :f => builder %>
<% end %>
</div>
<div class="actions">
<%= f.submit "Submit Report"%>
</div>
<% end %>
---Partial Question_Fields---
<h4 class="question_name">
<%= f.object.name %>
</h4>
<%= f.fields_for :answers do |answer,index| %>
<%= render 'answer_fields', :f => answer %>
<% end %>
---Partial Answer_Fields---
<%= f.text_field :name, :placeholder => "Add your answer here" %>
But when I try to edit/create a new report it fetches all the existing answers for that particular question. Whereas I want to implement something like :
---Partial Question_Fields---
<h4 class="ques_title">
<%= f.object.name %>
</h4>
<% f.object.answers_for_report(#report).each do |answer| %>
<%= render 'answer_fields', :f => answer %>
<% end %>
---Partial Question_Fields---
<b>What should be code here so that it again acts same as nested attributes and gets updated succesfully !!!</b>
Question Model
belong_to :reports
has_many :answers, :dependent => :destroy
accepts_nested_attributes_for :answers, :allow_destroy => true
def answers_for_report(#report)
self.answers.where("report_id = ? ",report.id)
end
Here is the answer to my question :
Report Form is like
<%= form_for #report do |f| %>
<div class="field">
<%= f.fields_for :questions do |builder| %>
<%= render "question_fields", :f => builder %>
<% end %>
</div>
<div class="actions">
<%= f.submit "Submit Report"%>
</div>
<% end %>
Then Question 'question_fields' is like
<%= question.fields_for :answers, question.object. answers_for_report(#report) do |answer| %>
<%= render 'answer_fields', :f => answer %>
<% end %>
This passes only a collections / set of records of answers and renders fields for those selected answers only.

Resources