Controller: projects_controller.rb
def new
#project = Project.new
#project.repositories.build
end
def edit
#project = Project.find(params[:id])
end
Model: project_sub_type.rb
class ProjectSubType < ActiveRecord::Base
has_many :repositories, :dependent => :destroy
accepts_nested_attributes_for :repositories
end
View: _form.html.erb
<%= form_for #project, :html => {:class => 'project'} do |f| %>
<%= f.label :name, "Project name" %>
<%= f.text_field :name %>
<%= f.fields_for :repositories do |ff| %>
<%= ff.check_box :repos_name, {} , "svn_software", nil %> Svn Software
<% end %>
<%= f.fields_for :repositories do |ff| %>
<%= ff.check_box :repos_name, {} , "git_software", nil %> Git Software
<% end %>
<%= f.submit "Save"%>
edit.html.erb
<h2>Edit project</h2>
<%= render 'form' %>
Question: During create it creates the checkbox like this
<input type="checkbox" value="svn_software" name="project[repositories_attributes][0][repos_name]" id="project_repositories_attributes_0_repos_name">
<input type="checkbox" value="git_software" name="project[repositories_attributes][1][repos_name]" id="project_repositories_attributes_1_repos_name">
And it works perfectly for me. But during edit it creates 2 fields extra:
<input type="checkbox" value="svn_software" name="project[repositories_attributes][0][repos_name]" id="project_repositories_attributes_0_repos_name">
<input type="checkbox" value="svn_software" name="project[repositories_attributes][1][repos_name]" id="project_repositories_attributes_1_repos_name">
<input type="checkbox" value="git_software" name="project[repositories_attributes][2][repos_name]" id="project_repositories_attributes_2_repos_name">
<input type="checkbox" value="git_software" name="project[repositories_attributes][3][repos_name]" id="project_repositories_attributes_3_repos_name">
And also creates a hidden field as below:
<input type="hidden" value="51" name="project[repositories_attributes][0][id]" id="project_repositories_attributes_0_id">
to .. 3
Can someone please point out my mistake. And the same problem exist if I use 1 fields_for and loop though an array.
Can anyone please help.
Thanks in advance
Reference to http://apidock.com/rails/v3.2.13/ActionView/Helpers/FormHelper/fields_for
Your code will cause duplication result, because The block given to the nested fields_for call will be repeated for each instance in the collection, so you have to :
<%= form_for #project, :html => {:class => 'project'} do |f| %>
<%= f.label :name, "Project name" %>
<%= f.text_field :name %>
<% #project.repositories.each do |r| %>
<%= f.fields_for :repositories, r do |ff| %>
<%= ff.check_box :repos_name, {} , r.repos_name, true %><%=r.repos_name%>
<% end %>
<% end %>
<%= f.submit "Save"%>
But when create, you should fill the check_box not include in repositories.
So you could:
<%= form_for #project, :html => {:class => 'project'} do |f| %>
<%= f.label :name, "Project name" %>
<%= f.text_field :name %>
<% #project.repositories.each do |r| %>
<%= f.fields_for :repositories, r do |ff| %>
<%= ff.check_box :repos_name, {} , r.repos_name, true %><%=r.repos_name%>
<% end %>
<% end %>
<% (['svn_software','git_software'] - #project.repositories.map(& :repos_name)).each do |name| %>
<%= ff.check_box :repos_name, {} , name, nil %><%=name%>
<% end %>
<%= f.submit "Save"%>
Related
I have model Project and in model have field documents(files). I am using gem dragonfly. When editing a project, how to make the list of current files? At this time on the edit page the list of files fields show: "The file is not selected". I do next:
projects_controller:
def edit
end
def update
if #project.update(project_params)
redirect_to #project
else
render :edit
end
end
private
def set_project
#project = Project.find(params[:id])
end
def set_payment_types
#payment_types = PaymentOption.all
end
def project_params
params.require(:project).permit(:title, :description, :price, :location,
:anonymity, :price_category, :category_id, :skill_list, documents_attributes: [:attachment], payment_option_ids: [])
end
edit.html.erb:
<%= form_for #project do |f| %>
<p>
<%= f.label 'Name' %>
<%= f.text_field :title %>
</p>
<P>
<%= f.label 'Budget' %>
<%= f.text_field :price %>
für
<%= f.select :price_category, Project::PRICE_CATEGORIES %>
</P>
<p>
<%= f.label 'Description' %>
<%= f.text_area :description %>
</p>
<p>
<%= f.label 'Category' %>
<%= f.collection_select :category_id, Category.all, :id, :name %>
</p>
<p>
<%= f.label 'Skills Freelancer (15 pieces) *' %>
<%= f.text_field :skill_list %>
</p>
<p>
<%= f.label 'Location' %>
<%= f.text_field :location %>
</p>
<ul>
<% #payment_types.each do |type| %>
<li><%= check_box_tag 'project[payment_option_ids][]', type.id %>
<%= type.name %>
</li>
<% end %>
</ul>
<p>
<b>Anonymity order</b>
</p>
Setup allows for players to remain anonymous. Note that this may reduce the number of responses.
<p>
<%= f.label 'Place Order anonymously' %>
<%= f.check_box :anonymity %>
</p>
<p>
<%= f.fields_for :documents do |d| %>
<%= render 'document_fields', f: d %>
<% end %>
<div class="links">
<%= link_to_add_association 'add file', f, :documents %>
</div>
</p>
<P>
<%= f.submit 'Edit' %>
</P>
<% end %>
_document_fields.html.erb:
<div class="nested-fields">
<%= f.label :attachment %>
<%= f.file_field :attachment %>
<%= link_to_remove_association "remove file", f %>
</div>
document.rb
class Document < ApplicationRecord
belongs_to :project
dragonfly_accessor :attachment
end
On the edit page the list of files fields show: "The file is not selected", but must be specified current files.
file_field is for uploading new files only, not for displaying what is already in the database. For displaying a list of uploaded files, you should just display the name of the file and/or an image tag with the thumbnail and provide a destroy link.
You can also provide a link to create a new attachment.
edit.html.erb
<div>
<ul>
<%= #project.documents.each do |document| %>
<li>
<%= document.attachment.name %>
<%= link_to "Delete", document_path(document), method: :delete, data: { confirm: 'Are you sure you want to delete this?' } %>
</li>
<% end %>
</ul>
<div class="links">
<%= link_to_add_association 'add file', f, :documents %>
</div>
</div>
I'm searching for a solution to extend my form without page reload.
First I tried to render a partial with coffee or javascript, but escape_javascript didnt work.
Here's the view
<%= form_for #recipe = current_user.recipes.build do |f| %>
<%= f.label :name, "Recipe Name" %>
<%= f.text_field :name %>
<%= button_to "Add Ingredient", '#', class: "btn btn-lg btn-primary", id: "add" %>
<p></p>
<%= f.submit class: "btn" %>
<% end %>
The form above should be extented with following partial by every click on the button
<div id="recipe-ingredients">Quantity
<%= f.fields_for :quantities, #recipe.quantities.build do |quantity| %>
<%= render 'quantity_fields', f: quantity %>
<% end %>
</div>
_quantity_fields
<%= f.label :amount, "Amount:" %>
<%= f.text_field :amount %>
<%= f.collection_select(:ingredient_id, Ingredient.all, :id, :name) %>
This approach did not work (recipes.js.erb)
$(document).ready(function() {
$("#add").click(function() {
$("p").append("<%= escape_javascript
render(:partial => 'quantities') %>");
});
});
There's a workaround (see below) but I'm searching for a better solution.
$(document).ready(function() {
$("#workout-week").append(<%= escape_javascript(
Haml::Engine.new(File.read(File.join(Rails.root,'app/views',
'_show_period.html.haml'))).render(Object.new, period: #period)) %>);
});
A second approach is to write following lines in Coffee or JavaScript:
<%= f.fields_for :quantities, #recipe.quantities.build do |quantity| %>
<%= f.label :amount, "Amount:" %>
<%= f.text_field :amount %>
<%= f.collection_select(:ingredient_id, Ingredient.all, :id, :name) %>
<% end %>
I'm a newbie so take this with a grain of salt, but have you tried using render :foo instead of redirect_to :foo in the appropriate controller function?
I solved it with cocoon
<%= form_for #recipe do |f| %>
<div class="field">
<%= f.label :name %>
<br/>
<%= f.text_field :name %>
<%= f.fields_for :quantities do |quantity| %>
<%= render 'quantity_fields', :f => quantity %>
<% end %>
<div class="links">
<%= link_to_add_association 'add', f, :quantities %>
</div>
</div>
<%= f.submit %>
<% end %>
Note: I've read a couple posts similar to this. But non of the solutions answer my question
I have two objects, Bid and Moz. When I build my Bid object, everything seems to save okay, except for the Moz objects.
Model
class Bid < ActiveRecord::Base
belongs_to :user
has_many :mozs, :dependent => :destroy
accepts_nested_attributes_for :mozs, :allow_destroy => true
end
class Moz < ActiveRecord::Base
belongs_to :bid
end
Bids::Build Controllers
class Bids::BuildController < ApplicationController
include Wicked::Wizard
steps :intro, :problems, :solutions, :pricing
def show
#bid = Bid.find(params[:bid_id])
render_wizard
end
def update
#bid = Bid.find(params[:bid_id])
#bid.attributes = build_params
4.times { #bid.mozs.build } if step == steps.second
render_wizard #bid
end
def new
#bid = Bid.new
redirect_to wizard_path(steps.first, :bid_id => #bid.id)
end
def build_params
params.require(:bid).permit(:client_name, :intro, :prob1, :prob2, :prob3, :site_feel, :search_phrase, :page_score, :total_links,
:internal_links, :external_links, :competition, :complete, :user_id, :us_company, :philosophy_1,
:philosophy_2, :website_conclusions, :is_onsite_seo, :onsite_seo, :is_ongoing_seo, :ongoing_seo,
:is_ppc, :ppc, :is_social_media, :social_media, :is_google_places, :google_places, :is_adwords_express,
:adwords_express, moz_attributes: [:url, :id, :_destroy]
)
end
private
def finish_wizard_path
root_url
end
end
solutions.html.erb
<%= form_for (#bid), url: wizard_path do |f| %>
<% if #bid.errors.any? %>
<div id="error_explanation">
<h2><%= pluralize(#bid.errors.count, "error") %> prohibited this bid from being saved:</h2>
<ul>
<% #bid.errors.full_messages.each do |msg| %>
<li><%= msg %></li>
<% end %>
</ul>
</div>
<% end %>
<% if #bid.is_onsite_seo? %>
<div class="field">
<%= f.label :onsite_seo %><br>
<%= f.text_area :onsite_seo %>
</div>
<% end %>
<% if #bid.is_ongoing_seo? %>
<div class="field">
<%= f.label :ongoing_seo %><br>
<%= f.text_area :onsite_seo %>
</div>
<% end %>
<div class="field">
<%= f.label :ppc %><br>
<%= f.text_area :ppc %>
</div>
<div class="field">
<%= f.label :social_media %><br>
<%= f.text_area :social_media %>
</div>
<div class="field">
<%= f.label :google_places %><br>
<%= f.text_area :google_places %>
</div>
<div class="field">
<%= f.label :adwords_express %><br>
<%= f.text_area :adwords_express %>
</div>
<%= f.fields_for :mozs do |builder| %>
<%= render partial: "moz_fields", locals: {f: builder} %>
<% end %>
<%= link_to_add_association "Add URL", f, :mozs %>
<div class="actions">
<%= f.submit %>
or <%= link_to "skip this step", next_wizard_path %>
</div>
<% end %>
_moz_fields.html.erb
<div class="field fields">
<%= f.label :url, "Comparative URL" %><br>
<%= f.text_field :url %>
<%= f.hidden_field :destroy %>
<%= link_to_function "remove", "remove_fields(this)"%>
</div>
I don't understand why they won't save. In addition, I noticed something odd -- when I don't use a partial for the nested object and use the f form builder for the #bid object (as opposed to 'builder'), I get an error no method or variable :url, but a Moz object is saved (although, not with any of the desired attributes).
My opinion that you misspelled with permit attrbibutes hash, try to change moz_attributes to mozs_attributes.
params.require(:bid).permit(..., :mozs_attributes: [:url, :id, :_destroy])
If you send the parameter _destroy: 1 through your hidden field
<%= f.hidden_field :destroy %>
you instruct Rails to destroy the child moz object, or in your case, prevent it from being created.
As for the second part of your question, if you inline the partial from this
<%= f.fields_for :mozs do |builder| %>
<%= render partial: "moz_fields", locals: {f: builder} %>
<% end %>
to this
<%= f.fields_for :mozs do |builder| %>
<div class="field fields">
<%= f.label :url, "Comparative URL" %><br>
<%= f.text_field :url %>
<%= link_to_function "remove", "remove_fields(this)"%>
</div>
<% end %>
it won't work, because the model object for the scope f is your #bid, not moz. Bids have no url attribute, hence the error.
With the input fields being created in the wrong form builder scope, you did not actually transmit any attributes for your moz object, and so it was created blank. As a side effect, this also meant not sending the _destroy parameter, so the object was saved.
Instead, inline the partial like this (I renamed builder to moz for clarity):
<%= f.fields_for :mozs do |moz| %>
<div class="field fields">
<%= moz.label :url, "Comparative URL" %><br>
<%= moz.text_field :url %>
<%= link_to_function "remove", "remove_fields(this)"%>
</div>
<% end %>
I have a 2 views that I want to use before the create action.
1st view is add_sample_details.html.erb
<% form_tag add_expt_details_samples_path :method => :post do %>
<% for sample in #samples %>
<% fields_for "samples[]", sample do |form| %>
<fieldset>
<legend>Sample Name: <%= sample.name %></legend>
<p><center><%= form.label :sample_title %>
<%= form.text_field :sample_title, :size => 25 %></center></p>
<table>
<tr>
<td>
<%= form.label :taxon_id %>
<%= form.text_field :taxon_id, :size => 15 %>
</td>
<td>
<%= form.label :scientific_name %>
<%= form.text_field :scientific_name, :size => 20 %>
</td>
<td>
<%= form.label :common_name %>
<%= form.text_field :common_name, :size => 15 %>
</td>
</tr>
</table>
<p>
<center>
<%= form.label :sample_descripition %><br \>
<%= form.text_area :description %>
</center>
</p>
</fieldset>
<% end %>
<% end %>
<p><center><%= submit_tag "Next" %></center></p>
<% for samp in #samples %>
<%= hidden_field_tag("sample_ids[]", samp.id) %>
<% end %>
<% end %>
in the SamplesController, the add_expt_details looks like this
def add_expt_details
# Collect the samples that were sent
#expt_samples = Sample.find(params[:sample_ids])
end
and the add_expt_details.html.erb looks like this
<% form_for #expt_samples, :url => create_study_samples_path, :html => { :method => :put } do |f| %>
<% #expt_samples.each do |samp|%>
<% f.fields_for :expts do |form| %>
<%= form.label :experiment_title %>
<%= form.text_field :expt_title, :size => 15 %><br \>
<% end %>
<% end %>
<p><center><%= submit_tag "Next" %></center></p>
<% end %>
and my create_study action is like this
def create_study
#study = Study.new(params[:study])
# this method collects all the samples from the add_sra_details view from the hidden_field_tag
if current_user.id?
# Put the current user_id in the study.user_id
#study.user_id = current_user.id
if #study.save # Save the values for the study
# Update the Sample attributes
#samples = Sample.update(params[:samples].keys, params[:samples].values).reject { |p| p.errors.empty? }
# For all those sample_ids selected by the user, put the study ids in the sample_study_id
#samples.each do |samp|
#study.samples << samp
end
# get the ids_sent from the add_expt_details
#expt_samples = Sample.find(params[:id])
# A Flash message stating the details would be prefereable..! (Means you have to do it)
flash[:success] = "Your Study has been Created"
redirect_to add_expt_details_samples_path
else
flash[:error] = "Study Faulty"
render :action => "add_sra_details"
end
end
end
The error message I get when I keep the #expt_samples = Sample.find(params[:id]) is "undefined method `keys' for nil:NilClass"
and If I dont have the #expt_samples, it gives me an error "Couldn't Find id in Sample"
Can some one suggest how can I update set of sample ids from one form and also create new Expt model records that are associated to the sample_ids in another form.
All suggestions are appreciated.
Cheers
You need to refer to Rails' accepts_nested_attributes_for
To see how to link existing objects with forms, you can see examples here:
http://josemota.net/2010/11/rails-3-has_many-through-checkboxes/
rails 3 has_many :through Form with checkboxes
has_many through checkboxes
On a single page I might have a single Video and then checkboxes to add multiple dogs to it. Easy enough (as follows)...
View:
<%= check_box_tag "video[dog_ids][]", dog.id %>
Controller:
params[:video][:dog_ids] ||= []
But what I can't figure out how to do is have multiple videos, each with multiple dogs.
I currently have this:
<% #videos.each do |video| %>
<%= fields_for "item[]", video do |f| %>
<%= f.hidden_field :id, :index => nil %>
<%= f.text_field :title, :index => nil%>
<%= f.text_area :body, :index => nil %>
<% video.dogs.each do |dog| %>
<%= check_box_tag "item[][video[dog_ids][]]", dog.id %>
<% end %>
<% end %>
<% end %>
But when I do that, dogs_ids is always nil when it's submitted.
Any idea what I'm doing wrong?
Such a setup with fields_for "item[]" and f.text_field :title, :index => nil produces:
<input id="item__name" name="item[][title]" size="30" type="text" value="vid1">
This indicates that the checkbox name should be item[][dog_ids][].
A working example:
<% #videos.each do |video| %>
<%= fields_for "item[]", video do |f| %>
<%= f.text_field :title, :index => nil %>
<% video.dogs.each do |dog| %>
<%= check_box_tag "item[][dog_ids][]", dog.id %> <%= Dog.name %>
<% end %>
<br>
<% end %>
<% end %>
This produces result in params:
{"item"=> [
{"title"=>"vid1", "dog_ids"=>["1", "2"]},
{"title"=>"vid2", "dog_ids"=>["2"]},
{"title"=>"vid3", "dog_ids"=>["2", "3"]}
]}