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'
I'm working on this reconciliation system where users can choose which fields they would like to use when importing records. I finally got the select to pre-populate with the correct info but can't for the life of me get the checkboxes to do the same. Here's my form:
#views/match_rule/edit.html.erb
<%= form_for(#match_rules) do |f| %>
<%= f.label :name, "Name" %>
<%= f.text_field :name, placeholder: "Rule Set Name" %>
<%= f.label :algorithm, "Choose an algorithm" %>
<%= f.select :algorithm, build_algorithm_select_options %>
<%= f.label :match_fields, "Available fields" %>
<% #field_options.each do |field| %>
<%= f.check_box :match_fields %>
<%= f.label :match_fields, "#{field.last}" %><br/>
<% end %>
<%= f.submit "Save", class: "button primary" %>
<%= link_to "Cancel", match_rule_index_path, class: "button" %>
<% end %>
Currently the corresponding controller is simply building a hash of fields so not to clutter up the view with repetitive code
#controllers/match_rule_controller.rb
def edit
#field_options = {
name: "Name",
email: "Email",
phone: "Phone Number",
gender: "Gender",
marital_status: "Marital Status",
dob: "Date of Birth",
campus: "Campus",
address: "Street Address",
city: "City",
state: "State",
zip: "Zip"
}
end
My model is serializing the fields
#models/match_rule.rb
class MatchRule < ActiveRecord::Base
has_many :inboxes
enum algorithm: [ :adjustable, :classic_minimal, :classic_standard ]
serialize :match_fields
end
and my database fields are storing all the options that should populate the checkboxes in the match_fields column
If it helps to see how I seeded the db with an array here's that:
#db/seeds.rb
classic_minimal = MatchRule.create(algorithm: 'classic_minimal', match_fields: ['name', 'email', 'phone', 'gender', 'zip'], name: 'Classic Minimal')
classic_standard = MatchRule.create(algorithm: 'classic_standard', match_fields: ['name', 'email', 'phone', 'gender', 'marital_status', 'dob', 'campus', 'address', 'city', 'state', 'zip'], name: 'Classic Standard')
adjustable = MatchRule.create(algorithm: 'adjustable', match_fields: ['name', 'phone', 'campus', 'marital_status', 'zip'], name: 'Adjustable')
I've checked out similar questions on here, the one closest to helping me have a breakthrough was the answer to this question: Set checkboxes on edit method in Rails 4 where he says to use .include? to determine the checked status, but I'm not sure what I would put in the params because I need to parse through the hash inside match_fields for each and every field and that would get pretty gnarly.
Any advice or guidance is greatly appreciated, thanks!
UPDATE
Update! Tried using fields_for but still not posting or populating.
<%= f.label :match_fields, "Available fields" %>
<%= fields_for :match_fields do |field| %>
<%= field.check_box :name %>
<%= field.label :name, "Name" %><br/>
<%= field.check_box :email %>
<%= field.label :email, "Email" %><br/>
<%= field.check_box :phone %>
<%= field.label :phone, "Phone" %><br/>
<% end %>
now when I check params on update in the console, it's saying:
"match_fields" => {
"name" => "1",
"email" => "1",
"phone" => "0"
},
but not actually posting or populating.
Figured it out.
<%= form_for(#match_rules) do |f| %>
<%= f.label :name, "Name" %>
<%= f.text_field :name, placeholder: "Rule Set Name" %>
<%= f.label :algorithm, "Choose an algorithm" %>
<%= f.select :algorithm, build_algorithm_select_options %>
<%= f.label :match_fields, "Available fields" %>
<%= f.check_box :name, { checked: #match_rules.match_fields.include?("name") } %>
<%= f.label :name, "Name" %><br/>
<%= f.check_box :email, { checked: #match_rules.match_fields.include?("email") } %>
<%= f.label :email, "Email" %><br/>
<%= f.check_box :phone, { checked: #match_rules.match_fields.include?("phone") } %>
<%= f.label :phone, "Phone" %><br/>
<%= f.check_box :gender, { checked: #match_rules.match_fields.include?("gender") } %>
<%= f.label :gender, "Gender" %><br/>
<%= f.check_box :marital_status, { checked: #match_rules.match_fields.include?("marital_status") } %>
<%= f.label :marital_status, "Marital Status" %><br/>
<%= f.check_box :dob, { checked: #match_rules.match_fields.include?("dob") } %>
<%= f.label :dob, "Date of Birth" %><br/>
<%= f.check_box :campus, { checked: #match_rules.match_fields.include?("campus") } %>
<%= f.label :campus, "Campus" %><br/>
<%= f.check_box :address, { checked: #match_rules.match_fields.include?("address") } %>
<%= f.label :address, "Street Address" %><br/>
<%= f.check_box :city, { checked: #match_rules.match_fields.include?("city") } %>
<%= f.label :city, "City" %><br/>
<%= f.check_box :state, { checked: #match_rules.match_fields.include?("state") } %>
<%= f.label :state, "State" %><br/>
<%= f.check_box :zip, { checked: #match_rules.match_fields.include?("zip") } %>
<%= f.label :zip, "Zip Code" %><br/>
<%= f.submit "Save", class: "button primary" %>
<%= link_to "Cancel", match_rule_index_path, class: "button" %>
You have to pass the .include? method inside the options hash. Now I just need to find a cleaner way to put this in the view.
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 %>
in my app I have form that looks like this
= simple_form_for #user do |f|
= f.input :name, error: false
= f.input :surname, error: false
Is there any way to avoid this repetitions (error: false)?
If they're all of the same type, something like this should work:
= simple_form_for #user do |f|
- [ :name , :surname ].each do |field|
= f.input field, error: false
If not, you could use a hash or something, instead of an array, and specify the type, as well.
It appears that simple form has the following option:
If you want to pass the same options to all inputs in the form (for
example, a default class), you can use the :defaults option in
simple_form_for. Specific options in input call will overwrite the
defaults:
<%= simple_form_for #user, defaults: { input_html: { class: 'default_class' } } do |f| %>
<%= f.input :username, input_html: { class: 'special' } %>
<%= f.input :password, input_html: { maxlength: 20 } %>
<%= f.input :remember_me, input_html: { value: '1' } %>
<%= f.button :submit %>
<% end %>
From https://github.com/plataformatec/simple_form
So, in your case:
= simple_form_for #user , defaults: { error: false } do |f|
= f.input :name
= f.input :surname
You could loop through an array of symbols
simple_form_for #user do |f|
[:name, :surname].each do |element|
f.input element, error: false
end
end
I have this in my Form
<%= f.label :price, "price"%>
<%= f.text_field :price, :size=>20, :id =>"price" %>
<%= f.label :quantity, "Quantity"%>
<%= f.text_field :quantity, :size=>20, :id =>"qty" %>
<%= f.label :amount, "Amount"%>
<%= f.text_field :amount, :size=>20, :id =>"amount"%>
I want {price*quantity} to happen inside the 'amount' field as soon as i enter values inside 'price' and 'quantity' i.e calculations should happen before submit. I am new to jQuery/Ajax, so any help will do. Thanks in advance.
You can do something like this
$(function() {
$("#price, #qty").keyup(function() {
var p = $("#price").val();
var q = $("#qty").val();
$("#amount").val(q * p);
});
});
But you have to give the amount field the id "amount" and not reuse "price" as the id.