The following code gives a label of "Project address":
%h2 New Project
= simple_form_for(#project, html: {class: "form-horizontal"}, wrapper: :horizontal_form) do |f|
= f.error_notification
= f.input :project_address, required: true
= f.button :submit, "Create"
Is there any way of making the label title case ("Project Address") apart from using f.label?
If you wanted to title case all labels, you can configure simple form in the initializer:
# config/initializers/simple_form.rb
# How the label text should be generated altogether with the required text.
config.label_text = lambda { |label, required, explicit_label| "#{required} #{explicit_label ? label : label.to_s.titleize}" }
This will make all your labels title case unless you specify the label explicitly with the :label option.
Related
I added this function in user.model
def is_federated?(session)
session[:federatedIdHeader].present?
end
My form.html.haml file is this, i am trying to edit the user if it federated by passing the session in 7th line , this is basically in another repo, i am using it as a gem
.user-edit-form
= simple_form_for(editable_user,html: { autocomplete: "off" }) do |f|
%section
%header
%h3
= precede "User Information" do
-if editable_user.is_federated?(session)
= link_to "Change Password",main_app.try(:new_password_path)
.inputs
= f.input :needs_setup,as: :hidden,input_html: { value: false }
= f.input :name,required: true
= f.input :email,disabled: true
= f.input :phone,label: "Phone Number"
= f.input :title
- if editable_user_is_authorized?(editable_user)
%section
%header
%h3
= precede "Company Information" do
%small (Shared by all users in your company)
.inputs
= f.fields_for :account do |a|
= a.input :name,required: true
= a.input :url,label: "Web Address",required: true
.actions
= f.button :submit, "Save Profile", class: "button little green"
i am getting this error while deploying
users/edit/_form.html.haml:7: syntax error, unexpected ')' ));}\n", 0, false);if
editable_user.is_federated?(session)
/edit/_form.html.haml:9: syntax error, unexpected ')', expecting keyword_end ));}\n", 0,
false);end;
How to make it work
%header
%h3
= precede "User Information" do
- if editable_user.is_federated?(session)
= link_to "Change Password",main_app.try(:new_password_path)
This should pretty much fix your issue..
The issue being : the lines after where you open the precede block, the block contents needs to be indented to be within the block.
I hope that makes sense.
I have a form divided into two parts (the second part is displayed depending on user's first choice). Note that the second part is in a partial. The two parts must be sent to the same controller.
Using Haml, how can I get all the parameters sent to the controller, using only one submit button ?
ps : I'm using form_tag here
Main file
= form_tag({url: users_search_path}, id:'form1') do
label
= radio_button_tag 'user_type', 'Requestor', false, id: "radio-requestor"
= label_tag 'radio-requestor', "Demandeur"
label
= radio_button_tag 'user_type', 'Contributor', false, id: "radio-contributor"
= label_tag 'radio-contributor', 'Contributeur
.contributor_form
= render partial: 'contributor_search_form'
.requestor_form.hidden
= render partial: 'requestor_search_form'
----------------------------------------------
2nd part (partial contributor_search_form)
= form_tag({url: users_search_path}, id:"form2") do
label
= check_box_tag 'prof', 'prof', false, id: 'prof'
= label_tag 'prof', 'Prof'
label
= check_box_tag 'ticket', 'ticket', false, id: 'ticket'
= label_tag 'ticket', "Ticket"
= submit_tag "Afficher les résultats"
Don't use 2 form_tag if you want to submit them all in 1 click.
If you want the view change base on user interaction without server, you have to do via Javascript
As I guess, you want the param receiving in the controller looks like
{
user_type: "Requestor",
prof: true,
ticket: false
}
so you can write your partial without form_tag as
# contributor_search_form
label
= check_box_tag 'prof', 'prof', false, id: 'prof'
= label_tag 'prof', 'Prof'
label
= check_box_tag 'ticket', 'ticket', false, id: 'ticket'
= label_tag 'ticket', "Ticket"
= submit_tag "Afficher les résultats"
And your main file with partial view being rendered
# Main file
= form_tag({url: users_search_path}, id:'form1') do
label
= radio_button_tag 'user_type', 'Requestor', false, id: "radio-requestor"
= label_tag 'radio-requestor', "Demandeur"
label
= radio_button_tag 'user_type', 'Contributor', true, id: "radio-contributor"
= label_tag 'radio-contributor', 'Contributeur
.contributor_form
= render partial: 'contributor_search_form'
.requestor_form.hidden
= render partial: 'requestor_search_form'
Notice that the partial form has been rendered inside the main form
Now write some Javascript with jQuery
When user select Requestor, show .requestor_form and vice versa for Contributor
Only submit fields within the displayed zone, then you have to disable fields when hidden.
Here is an example
function toggleFormPartial (target, isShow) {
if (isShow) {
target.removeClass("hidden")
// Remove `disabled` attribute for fields within `target`
} else {
target.addClass("hidden")
// Add `disabled` attribute for fields within `target`
}
}
$("#radio-requestor").change(function (e) {
toggleFormPartial($(".requestor_form"), e.val())
})
$("#radio-contributor").change(function (e) {
toggleFormPartial($(".contributor_form"), e.val())
})
I have the following form but for some reason, I can't get the HTML class to come appear in the resulting code. The documentation suggests this should work, but they don't have an example that uses a block. I've tried a few different things but I haven't found the right answer yet. I have to use a block to get the custom data to appear with the select.
= simple_form_for([:admin, #theme]) do |f|
= f.input :title_bar
= f.input :apple_touch_icon_image, input_html: { class: 'imagepicker' } do
= f.select :apple_touch_icon_image_id, Theme::AppleTouchIconImage.where(company: #company).map{ |i| [i.id, i.id, { 'data-img-src' => i.image.url(:thumbnail) }] }
= f.button :submit, class: "btn-success"
I would try:
= f.input :apple_touch_icon_image do
= f.select :apple_touch_icon_image_id, Theme::AppleTouchIconImage.where(company: #company).map{ |i| [i.id, i.id, { 'data-img-src' => i.image.url(:thumbnail) }] }, {}, { class: 'imagepicker' }
I have a form where a user can elect to create more input areas (to provide more information). I have a link the user will click on and then the extra form inputs will be created. I'd like to use the rails form helpers so that I don't have to write the html myself. I've tried inserting the form helpers directly into coffeescript and saving the outputted html to a data tag on the link, but I can't get the coffeescript to execute the ruby code and I'm having escaping issues with the data attribute.
Here's the form:
= simple_form_for([#site, #zone]) do |f|
= f.error_notification
.form-inputs
= f.input :site_id
= label_tag "X"
= text_field_tag 'x_coords[]'
= label_tag "Y"
= text_field_tag 'y_coords[]'
= label_tag "X"
= text_field_tag 'x_coords[]'
= label_tag "Y"
= text_field_tag 'y_coords[]'
= label_tag "X"
= text_field_tag 'x_coords[]'
= label_tag "Y"
= text_field_tag 'y_coords[]'
= link_to "Add Point", "#", id: "add_point", data: { fields: label_tags }
.form-actions
= f.button :submit
When a user clicks the "Add Point" link, I'd like to add another block of:
= label_tag "X"
= text_field_tag 'x_coords[]'
= label_tag "Y"
= text_field_tag 'y_coords[]'
label_tags is in application_helper.rb:
def label_tags
label_tag "Z"
end
The problem is the output for the "Add Point" link is:
Z" id="add_point">Add Point
and the quotation marks are causing the link to come out with the text: "Z" id="add_point">Add Point"
I got the data attribute idea from this screencast
You cannot execute Ruby code from Javascript. When the page is requested all embedded ruby is evaluated and the results are what you get. The issue that I can see from you paste is that your label block is in the right data attribute but it's not escaped.
What you'll need to do is escape the quotes on the generated HTML going into that field and then unescape them via Javascript. You could use html_escape here like: data: { fields: h(label_tags) } (h is an alias for html_escape or you could do this yourself, manually.
def escape(str)
str.gsub(/</, "<").gsub(/>/, ">").gsub(/"/, """)
end
# later in the view for the form
data: { fields: escape(label_tags) }
And then your CoffeeScript would click handler would like:
function unescape(str) {
return str.replace(/((?:<)|(?:>)|(?:"))/g, function($1) {
switch($1) {
case ">":
return ">";
case "<":
return "<";
case """:
return '"';
}
});
}
$("a").on("click", function() {
var html = unescape(this.data("fields"));
$(".the-place-to-put-it").html(html);
});
I do not doubt a better solution exists and as of the posting of this answer I have not tested this to work (in theory it should). Ideally, you should just generate the elements with jQuery in Javascript and not depend on this method for doing this - yes, it's duplicate code duplicated between ruby and Coffee.
Simple solution for me was to replace the double quotes in my generated HTML with single quotes. In code:
= link_to "Add Point", "#", id: "add_point", data: { fields: label_tags.gsub("\"", "'") }
Also, had to use capture and concat in the helper method:
module ApplicationHelper
def label_tags
capture do
concat label_tag "X"
concat text_field_tag 'x_coords[]'
concat label_tag "Y"
concat text_field_tag 'y_coords[]'
end
end
end
Using Haml 3.1.4 (Separated Sally)
I am curious as to what I am doing wrong.
Why does this not show the first radio button selected?
btw, at execution, #organization.pdf_size does equal 'letter_size'
I would actually like the radio button selected based on the
#organization.pdf_size, however I am just trying to get a hard
coded selection to work atm. tyfyt
= form_for [#organization] do |f|
Select a PDF page size
= label_tag 'Letter (8.5x11)'
= f.radio_button :pdf_size, id: 'letter_size', :checked => true
= label_tag 'Half Legal (8.5x7)'
= f.radio_button :pdf_size, id: 'half_legal_size'
= f.submit 'Save', class: 'button'
I have also tried other examples I have seen on stackoverflow, in this fashion:
= f.radio_button :pdf_size, id: 'letter_size', :checked => #organization.pdf_size == 'letter_size' ? true : nil
Try this:
= f.radio_button :pdf_size, "value", id: 'letter_size', :checked => true
As documented here, the radio button needs a value before the options.
Remember to change "value" to something that makes sense for your application.
Try appending this to your link:
input_html: {checked: true}