Rails duplicate of same form in view leaves fields empty - ruby-on-rails

I have a view associated with one model but there are multiple versions of the same form that are hidden until a jquery function shows them. When I try to submit one, all the fields are empty.
Here is the view in question:
= form_for #rfi do |f|
- if #rfi.errors.any?
#error_explanation
h2 = "#{pluralize(#rfi.errors.count, "error")} prohibited this rfi from being saved:"
ul
- #rfi.errors.full_messages.each do |message|
li = message
.field
= f.label :svg_ref, "SVG PO Number"
= f.text_field :svg_ref
.field
= f.label :vendor_ref, "Vendor SO Number"
= f.text_field :vendor_ref
.field
= f.label :due
= f.text_field :due
= f.hidden_field :rfi_type, value:"order"
.actions
= f.submit
.rfi_type.rfi_type_quote
= form_for #rfi do |f|
- if #rfi.errors.any?
#error_explanation
h2 = "#{pluralize(#rfi.errors.count, "error")} prohibited this rfi from being saved:"
ul
- #rfi.errors.full_messages.each do |message|
li = message
.field
= f.label :reference, "Quote number"
= f.text_field :reference
= f.hidden_field :rfi_type, value:"quote"
.field
= f.label :due
= f.text_field :due
.actions
= f.submit
This is the Jquery involved
$ ->
$(".rfi_type").hide()
$(".rfi_type_order").show()
$("input:radio[name=rfi_type]").change ->
$(".rfi_type").hide()
$(".rfi_type_"+$(this).val()).show()
return
return

I believe 'one' of the problems you may be facing is your use of the hidden_field with the same name. I would suggest you restructure both your haml and coffee-script to something like this
$ ->
$("#quote-fields").hide()
$("input:radio[name=rfi_type]").change ->
$(".show-hide-container").hide()
type = $(this).val()
$("#" + type + "-fields" ).show()
$("#rfi_type-field").val(type)
= form_for #rfi do |f|
- if #rfi.errors.any?
#error_explanation
h2 = "#{pluralize(#rfi.errors.count, "error")} prohibited this rfi from being saved:"
ul
- #rfi.errors.full_messages.each do |message|
li = message
#order-fields.show-hide-container
.field
= f.label :svg_ref, "SVG PO Number"
= f.text_field :svg_ref
.field
= f.label :vendor_ref, "Vendor SO Number"
= f.text_field :vendor_ref
#quote-fields.field.show-hide-container
= f.label :reference, "Quote number"
= f.text_field :reference
.field
= f.label :due
= f.text_field :due
= f.hidden_field :rfi_type, value: "order", id: "rfi_type-field"
.actions
= f.submit

Related

How to add corrupted submit button?

I want to have form for possible answers in poll and two submit buttons. First one passing all text fields, second one passing one of these fields as nil causing validation error. This is my _form.html.haml file:
= form_for [#poll, #question] do |f|
- if #question.errors.any?
#error_explanation
%h2= "#{pluralize(#question.errors.count, "error")} prohibited this question from being saved:"
%ul
- #question.errors.full_messages.each do |msg|
%li= msg
.field
= f.label :title
= f.text_field :title
%p
= f.label :kind
.radio
-#kind_options.each do |option|
%label
=f.radio_button :kind, option[1]
=option[0]
=f.fields_for :possible_answers do |c|
%p
=c.text_field :title, class: "form-control", placeholder: "Enter a possible answer"
.actions
= f.submit 'Save', class: "btn btn-primary"
- x = f
= x.hidden_field :kind, :value => nil
= x.submit 'Corrupted Sumbit Button', class: "btn btn-primary"
It seems that there is no difference if I submit x or f, :kind is blank even I choose it in radio button and click on f.submit button. How to work this out?

cocoon rails, how to get atached file name

Im using cocoon to attach files. I need to be able to remove file when editing a question. Im stuck on getting the exact file name to remove when rendering edit
div.edit_question
=form_for #question, remote: true do |f|
= f.label :title, class: 'label_hidden'
= f.text_field :title
br
= f.label :body, class: 'label_hidden'
= f.text_area :body
br
= f.fields_for :attachments do |f|
.nested-fields
= link_to_remove_association "remove #{ NAME HERE }", f
br
= f.submit 'Update'
Case is closed ))
=form_for #question, remote: true do |f|
= f.label :title, class: 'label_hidden'
= f.text_field :title
br
= f.label :body, class: 'label_hidden'
= f.text_area :body
br
- #question.attachments.each do |att|
= f.fields_for att do |f|
.nested-fields
= link_to_remove_association "remove #{ att.file.filename }", f
br
= f.submit 'Update'

Haml form that is expecting $end?

Hi I'm really new to Rails, and Haml of course and I've been trying to figure this out for couple days already.
when rendering pages with this partial I get error:
app/views/todos/_form.html.haml:19: syntax error,unexpected keyword_end, expecting $end
(Please excuse my indentation errors, I didn't have an option to copy and paste)
_form.html.haml contents:
1 = form_tag :todo do |f|
2 %br
3 = f.label :done
4 = f.check_box :done
5 = f.label :title
6 = f.text_field :title
7 %br
8 = f.label :urgent
9 = f.check_box :urgent
10 %br
11 = f.label :important
12 = f.check_box :important
13 %br
14 = f.label :description
15 %br
16 = f.text_area :description
17 %br
18 = f.submit "Save"
19
In HAML, you cannot have any direct sub elements to a = unless it's a block. As an = is ruby code and not part of the html template as such.
If you want sub elements to a = thay will need to passed to a ruby block like the = form_tag do |f| does.
So it's your check_box's that are causing this error.
In this example I would move both the form helpers to a single line like this:
= form_tag :todo do |f|
%br
= f.label :done, f.check_box(:done)
= f.label :title
= f.text_field :title
%br
= f.label :urgent, f.check_box(:urgent)
%br
= f.label :important, f.check_box(:important)
%br
= f.label :description
= f.text_area :description
%br
= f.submit "Save"
You could also tell the label helper to accept a block by adding do at the end of the method call:
= form_tag :todo do |f|
%br
= f.label :done do
= f.check_box(:done)
...
Or even use an haml element instead of the rails form helper to make the label:
= form_tag :todo do |f|
%br
%label{:for => 'done'}
= f.check_box :done
...
Your indentations and nesting aren't consistent. I usually go with 2 spaces.
Try:
= form_tag :todo do |f|
%br
= f.label :done
= f.check_box :done
= f.label :title
= f.text_field :title
%br
= f.label :urgent
= f.check_box :urgent
%br
= f.label :important
= f.check_box :important
%br
= f.label :description
%br
= f.text_area :description
%br
= f.submit "Save"
This converts nicely to erb:
<%= form_tag :todo do |f| %>
<br>
<%= f.label :done %>
<%= f.check_box :done %>
<%= f.label :title %>
<%= f.text_field :title %>
</br>
<br>
<%= f.label :urgent %>
<%= f.check_box :urgent %>
</br>
<br>
<%= f.label :important %>
<%= f.check_box :important %>
</br>
<br>
<%= f.label :description %>
</br>
<% end %>

Error 'block_is_haml?' Haml = f.label in Ruby 1.9

app/views/users/_form.html.haml, line 10
= form_for #user do |f|
- if #user.errors.any?
#error_explanation
%h2= "#{pluralize(#user.errors.count, "error")} prohibido que este usuario se guarde:"
%ul
- #user.errors.full_messages.each do |msg|
%li= msg
.field
= f.label 'Usuario'
= f.text_field :username
.field
= f.label :email
= f.text_field :email
.field
= f.label 'Teléfono'
= f.phone_field :phone
.field
= f.label 'Contraseña'
= f.password_field :password
.field
= f.label 'Reingresar Contraseña'
= f.password_field :password_confirmation
.control-group
//= f.label 'Permisos'
%ul.unstyled
- for role in Role.find(:all)
%li
= check_box_tag "user[role_ids][]", role.name, #user.roles.include?(role)
= role.name
%br
.actions
= f.submit 'Guardar',:class => 'btn btn-primary'
Error seems to be in the = f.label I've deleted all the = f.label and no errors were thrown, also changed = f.label 'Usuario' for = f.label :username with no luck.
Error Message:
ArgumentError in Users#new
Showing C:/Sites/AutosCostaRica/app/views/users/_form.html.haml where line #10 raised:
syntax error in "<reader>", line 3, column 18:
next_label: >>
^
Update:
I just found out that it works perfect on Ruby 1.8, but I waned to be 1.9.
Any ideas?

Rails, MongoMapper nested Array in HAML form trouble

I'm getting
undefined method `Carrots' for # (referencing ln#18)
When trying to edit with the below form:
= form_for #harvest do |f|
- if #harvest.errors.any?
#error_explanation
%h2= "#{pluralize(#harvest.errors.count, "error")} prohibited this harvest from being saved:"
%ul
- #harvest.errors.full_messages.each do |msg|
%li= msg
.field
= f.label :created_at
= f.text_field :created_at, :disabled => true
%br
= f.label :photo
= f.text_field :photo
%h2 Crops
- #harvest.harvested_crops.each do |harvested_crop|
= f.label :harvested_crop['crop']
= f.select harvested_crop['crop'], Crop.all.collect {|p| [ p.name, p.id ] }, {:include_blank => ''}
= f.label :harvested_crop['amount']
= f.text_field harvested_crop['amount']
%br
.actions
= f.submit 'Save'
Using the data below:
{ "_id" : ObjectId("5067846f37bca62bccc3729e"), "user_id" : "5067844637bca62bccc3729c", "photo" : "carrotsnspuds.jpg", "harvested_crops" : [ { "crop" : "Carrots", "amount" : 1112.15 }, { "crop" : "Potatoes", "amount" : 3212.44 } ] }
I've tried related Stack Overflow questions for MongoMapper, Rails and Embedded documents but I am not having any luck, perhaps due to this being a nested Array rather than EmbeddedDocument. I'm not using Formtastic or anything yet, would just like to understand the syntax required here first.
This is definitely not efficient, but this allowed me to get the job done:
= form_for #harvest do |f|
- if #harvest.errors.any?
#error_explanation
%h2= "#{pluralize(#harvest.errors.count, "error")} prohibited this harvest from being saved:"
%ul
- #harvest.errors.full_messages.each do |msg|
%li= msg
.field
= f.label :created_at
= f.text_field :created_at, :disabled => true
%br
= f.label :photo
= f.text_field :photo
%h2 Crops
- x = 0
- #harvest.harvested_crops.each do |harvested_crop|
= f.fields_for "harvested_crops[]", harvested_crop do |hc|
%b Harvested Crop
%select{:name => "harvest[harvested_crops][" + x.to_s + "][crop]"}
- Crop.all.collect.each do |crop_name|
- if harvested_crop['crop'] == crop_name[:name]
%option{:selected => "selected", :value => crop_name[:name]}
= crop_name[:name]
- else
%option{:value => crop_name[:name]}
= crop_name[:name]
%b Amount
%input{:name => "harvest[harvested_crops][" + x.to_s + "][amount]", :value => harvested_crop['amount']}/
%br
- x += 1
%br
.actions
= f.submit 'Save'

Resources