I',m trying to achieve a simple nested model form in Rails 4. This is probably something very simple, but just can't figure it out.
So I have an edit view, in which I create the nested fields:
<%= f.fields_for :bankaccounts do |bankaccount| %>
<%= render 'bankaccount_fields', :f => bankaccount %>
<% end %>
These work fine and even the link:
<%= link_to_add_association 'add', f, :bankaccounts , :class => "link"%>
works fine. The fields appear as they should when clicked.
The real problem is, that when I submit the form. It doesn't send the parameters back to web server which have been inputted into these added fields. (Can't see them anywhere in the webrick console) The form only submits the old bankaccount parameters as it should:
"bankaccounts_attributes"=>{"0"=>{"iban"=>"121221", "swift"=>"121212", "id"=>"3"}, "1"=>{"iban"=>"33333", "swift"=>"33333", "id"=>"9"}, "2"=>{"iban"=>"121212", "swift"=>"211212", "id"=>"10"}
Above attributes should contain four bank accounts.
FWIW: Turbolinks is also disabled.
EDIT: Here's the code from bankaccount_fields partial:
<div id="tilit">
<div class="area-form">
<div class="col">
<div class="box-row">
<label for="iban" class="label">Iban</label>
<div class="input-row no-italic">
<%= f.text_field :iban, :id => "iban", :class => "input-text" %>
</div>
</div>
</div>
<div class="col">
<div class="box-row">
<label for="swift" class="label">Swift</label>
<div class="input-row no-italic">
<%= f.text_field :swift, :id => "swift", :class => "input-text" %>
</div>
</div>
</div>
</div>
pic of inspect element
EDIT: Debugger outputs reg. parameters:
(rdb:2) #_params
{"utf8"=>"✓", "_method"=>"patch", "authenticity_token"=>"181xsndllcPmpvpMaDi70IXh8SpaSYxs0eiSo19qwfI=", "producer"=>{"name"=>"Anne", "RH_nro"=>"", "ytunnus"=>"", "prepaymentpercentage"=>"", "streetaddress"=>"", "zip"=>"", "city"=>"", "www"=>"", "bankaccounts_attributes"=>{"0"=>{"iban"=>"13123123", "swift"=>"121212", "id"=>"3"}, "1"=>{"iban"=>"33333", "swift"=>"33333", "id"=>"9"}, "2"=>{"iban"=>"121212", "swift"=>"211212", "id"=>"10"}}}, "commit"=>"TALLENNA", "action"=>"update", "controller"=>"producers", "id"=>"54"}
Should contain one more bankaccount_attributes record, but only contains the ones which are not dynamically added.
I've been scratching my head for hours now, so any help would be greatly appreciated.
Also sorry if the question is formed badly, this is my first :)
I have found the problem to my application, If don't know if anyone find this useful anymore or not.
I am guessing you have written your form_for inside a HTML table(that's what I did). If so the browser generates a inconsistent DOM. If you generate form inputs dynamically it wont put it inside the form unless you are selecting it through form selector. So the solution is put the form outside the HTML table.
What are your strong params & controller like?
They should be like this for nested attributes:
def params
params.require(:form_for_variable).permit(bankaccounts_attributes: [:iban, :swift])
end
Also, you'll need to include accepts_nested_attributes_for :bankaccounts in the model you're submitting the form to
Related
I'll try to ask this as clearly as I can. I'm doing a Udemy course and we are using nested fields to take in data. The form renders a single nested field initially and then there is a link to add additional fields. This all works and there are no errors in the console. If i put something in the first nested field, and add two more nested fields, only what was entered in the last two nested fields gets saved. When I look at the final result after saving, the first item is not there. I'm sure this has to do with how I'm rendering the initial field, but I can't figure out what it is. Both the initial render and the link_to_add_nested are using the same partial file.
Screenshot of the form
Code from _form.html.erb:
<div>
<%= form_with model: #technologies do |form| %>
<%= render partial: 'technology_fields', :locals => { :form => form } %>
<% end %>
</div>
<div>
<%= link_to_add_nested(form, :technologies, '#Add-Technology', partial: 'technology_fields') %>
</div>
<div class="form-group">
<%= form.submit "Save Portfolio Item", class: 'btn btn-primary btn-block'%>
</div>
<% end %>
</div>
Code from the partial _technology_fields.html.erb:
<div class="form-group nested-fields" id=Add-Technology>
<%= form.label :name %>
<%= form.text_field :name, class: 'form-control' %>
<%= link_to_remove_nested(form, link_text: 'Remove Item') %>
</div>
I can add code from the models and controller if needed, but as the form is working with the dynamically added nested fields, I don't think that's where the problem lies.
Bonus question: The same form is used for new and edit actions. Ideally when the form is used to edit an existing item, I would like the page to render with fields populated with the existing data. As it currently stands I have no way to delete or modify once it has been saved. I can only add new entries.
Thank you very much for any help you can give. I've done a lot of searching but haven't been able to locate the right information.
I am using Smart listing gem for real time filtering.
The following is a simple form without a submit and url.
<%= smart_listing_controls_for(:search) do %>
<%= simple_form_for :search do |f| %>
<%= f.input :certificates, collection: Certificate.all, :as => :check_boxes, include_hidden: false, :input_html => {:multiple => true} %>
<% end>
<% end>
The above code generates multiple check-boxes with 'Certificate id's as values.
As soon as one of the check-boxes is checked, smart listing sends a request to the controller with the params.
Parameters: {"utf8"=>"✓", "search_smart_listing"=>{"_"=>"1", "page"=>"", "per_page"=>"10"}, "authenticity_token"=>"z25rULU5JeeWcEZdpsy0+bz7OJFDWPmXrVGnzPvdG0cjM0ufpc3ydB9+5GywDQkUmcm6RGJnF0C4Yrd0sWpJ6g==", "search"=>{ "certificates"=>["6"]}}
The problem is when I select multiple check-boxes, the certificates array just has the latest value and not all the selected check-boxes values.
Also, when a check-box is selected and de-selected, the certificates array value in the params remains the same. I want the value to be removed from the certificates array in the params if the check-box is deselected and only want the certificates array to just have all the selected check-boxes values.
The following is the html code generated for one of the multiple check-boxes.
<span class="checkbox">
<label for="search_certificates_5">
<input class="check_boxes required" type="checkbox" value="5" name="search[certificates][]" id="search_certificates_5">
Certificate 1
</label>
</span>
Thanks in advance :)
Since both smart_listing_controls_for and simple_form_for create a form, one problem you might have is that you are creating a form inside a form, and that is nor recommended nor standard. That might lead to unexpected results.
Maybe try doing it without the simple_form helper, something like this (assuming Certificate has a description attribute):
<%= smart_listing_controls_for(:search) do %>
<%= Certificate.all.each do |certificate| %>
<div class="checkbox">
<label class= "checkbox inline">
<%= check_box_tag 'search[certificates][]', certificate.id %>
<%= certificate.description %>
</label>
</div>
<% end %>
<% end %>
Update
Also, there's a problem with current release (v1.1.2) of the smart listing gem that doesn't allow to work with array inputs. Problem is in this part of the javascript code. This is fixed on current master branch and was updated recently on latest commit as you can see here.
To solve this use the latest commit in your Gemfile like this:
gem 'smart_listing', :git => 'https://github.com/Sology/smart_listing.git', :ref => '79adeba'
bundle install after updating Gemfile and try the above code again.
I have a form to build several objects following Ryan Bates’ nested form tutorials (#196 and #197). My form looks like this:
<%=form_for #group_poll, :remote => true do |f|%>
<%=f.fields_for :questions do |builder| %>
<%=render "group_polls/question_fields", :f => builder%>
<%end%>
<p><%= link_to_add_group_question "#{t(:addquestion)}", f, :questions%></p>
<%end%>
The partial looks like:
<div class="row-fluid">
<div class="span3"><%=f.label "#{t(:question)}:"%></div>
<div class="span3"><%=f.text_field :admin_question, :required => true%></div>
<div class="span1"><%=f.submit "#{t(:send)}", :name => "send_#{f.object.id}", :class=>"btn-white"%></div>
<div class="span1"><%=f.submit "#{t(:save)}", :name => "save_#{f.object.id}", :class=>"btn-white"%></div>
</div>
I want to preload some questions that exist in the DB when accessing the page in the app and also have the chance to add new questions. In the controller action I have
#group_poll.questions.build()
for each poll. It works OK for generating the questions' fields, but they are empty. I don’t know how to preload those polls with the data in DB. I tried passing the question object #group_poll.questions.build(question), but I had no luck. I followed this SO question’s answer, but I get lost.
Is it posible what I want to do?
if you want to preload existing questions in the DB, you shouldn't use build() because it builds and empty object.
If you take build() out, does it work?
#group_poll.questions
I would like to create a login field everywhere on the top of my page, so I've add a :
in application.html.erb :
<%= render :partial => 'sessions/new' %>
in .../views/sessions/_new.html.erb
<%= form_tag do %>
<div>
<label for="name">Email :</label>
<%= text_field_tag :name, params[:name] %>
<label for="password">Mot de passe :</label>
<%= password_field_tag :password, params[:password] %>
</div>
<div>
<%= submit_tag "Connection" %>
</div>
</fieldset>
But it's work only if I am in a sessions controller when I test it in my browser,
I think that :
<%= submit_tag "Connection" %>
refers to his current controller (sessions) that's why it's doesn't work in ads/index for exemple but do its job in sessions/index.
What can I do ?
Do I have to specify the controller in the submit_tag ?
Thanks a lot :)
You need to tell the form tag the url that the form should submit to. Maybe by default it submits to the current action or something? You should never rely on the default whatever it is.
Read the api
http://railsbrain.com/api/rails-2.3.2/doc/index.html?a=M002551&name=form_tag
oh and btw the submit tag is just a button, it doesn't have anything to do with why the form does or doesn't work. There's a lot of confusion among rails novices about forms - a lot of people don't really understand how forms work. Before using any rails helpers at all, i'd strongly recommend making your form in pure html. That way you will understand what is actually going on, and the form helpers will be just that, ie "things that help you to do something more quickly" rather than being these magical things that leave you totally clueless when they don't do what you expect.
You need to specify the controller but on the form_tag not the submit_tag
e.g. <%= form_tag :controller => 'sessions', :action => 'new' %>
I am using rails3 beta3 and couchdb via couchrest. I am not using active record.
I want to add multiple "Sections" to a "Guide" and add and remove sections dynamically via a little javascript. I have looked at all the screencasts by Ryan Bates and they have helped immensely. The only difference is that I want to save all the sections as an array of sections instead of individual sections. Basically like this:
"sections" => [{"title" => "Foo1", "content" => "Bar1"}, {"title" => "Foo2", "content" => "Bar2"}]
So, basically I need the params hash to look like that when the form is submitted. When I create my form I am doing the following:
<%= form_for #guide, :url => { :action => "create" } do |f| %>
<%= render :partial => 'section', :collection => #guide.sections %>
<%= f.submit "Save" %>
<% end %>
And my section partial looks like this:
<%= fields_for "sections[]", section do |guide_section_form| %>
<%= guide_section_form.text_field :section_title %>
<%= guide_section_form.text_area :content, :rows => 3 %>
<% end %>
Ok, so when I create the guide with sections, it is working perfectly as I would like. The params hash is giving me a sections array just like I would want. The problem comes when I want edit guide/sections and save them again because rails is inserting the id of the guide in the id and name of each form field, which is screwing up the params hash on form submission.
Just to be clear, here is the raw form output for a new resource:
<input type="text" size="30" name="sections[][section_title]" id="sections__section_title">
<textarea rows="3" name="sections[][content]" id="sections__content" cols="40"></textarea>
And here is what it looks like when editing an existing resource:
<input type="text" value="Foo1" size="30" name="sections[cd2f2759895b5ae6cb7946def0b321f1][section_title]" id="sections_cd2f2759895b5ae6cb7946def0b321f1_section_title">
<textarea rows="3" name="sections[cd2f2759895b5ae6cb7946def0b321f1][content]" id="sections_cd2f2759895b5ae6cb7946def0b321f1_content" cols="40">Bar1</textarea>
How do I force rails to always use the new resource behavior and not automatically add the id to the name and value. Do I have to create a custom form builder? Is there some other trick I can do to prevent rails from putting the id of the guide in there? I have tried a bunch of stuff and nothing is working.
Thanks in advance!
Ok, I think I have figured out something that works. Changing the first line of the partial to:
<%= fields_for "sections", section, :index => "" do |guide_section_form| %>
Seems to work just fine. This way both new and edit form looks the same under the hood and the params hash works just like I need it to. If anyone sees anything wrong with this or has another alternative, please let me know.