Edit a submit btn for nested form - ruby-on-rails

I would like to customize the submit file btn. To do that, I'm using a pure css trick. But the problem is that this is not working as well than before the editing. I'm using cocoon to add images to my instance as a nested file.
I'm changing this (works perfectly) :
<div class="nested-fields col m4 s6">
<div class="flat_photo_box">
<%= f.file_field :image %>
</div>
</div>
Into this (partially works):
<div class="nested-fields col m4 s6">
<div class="image-upload">
<label for="file-input<%=f.object.id%>">
<span class="fa fa-download trash_nested_form"></span>
</label>
<%= f.file_field :image, id:"file-input#{f.object.id}" %>
</div>
</div>
With this css trick :
.image-upload > input
{
display: none;
}
.image-upload > label{
cursor:pointer;
}
ERROR = I can create as much nested form than I want, but paperclip only save the last image that I upload into the first nested element. Do you have an idea of where the mistake might come from

You assign an id to the file_field, a html-id is supposed to be unique. While the page will render correctly, upon form submit only one will be submitted to the server. Instead write your field like this:
<%= f.file_field :image, class:"file-input#{f.object.id}" %>
Why do you want to assign a specific id?

Related

Text overflowing that has the Bootstrap class: form-control

My text is overflowing within a span element which has the class: form-control
Code:
<div class="row">
<div class="form-group col-sm-12">
<label>Notes:</label>
<span class="form-control"><%= #stuff.notes %></span>
</div>
</div>
And when #stuff.notes has a lot of text, it overflows like this:
I did look at this question, as well as this ticket within bootstrap. I am still having trouble coming up with a solution.
Update:
I realized I could use a form helper (even though it isn't a form) like this:
<div class="row">
<div class="form-group col-sm-12">
<label>Notes:</label>
<%= text_area_tag :notes, #complaint.notes, {class: "form-control", disabled: "disabled"} %>
</div>
</div>
Which renders it like this:
But I don't like how the disabled html attribute grays out the box and mutes the text. It makes it difficult for the user to read.
You could use a basic panel to get a similar feel. See Bootply Here
<label>Notes:</label>
<div class="panel panel-default">
<div class="panel-body">
Basic panel example
</div>
</div>
And if you really want to match the look of the form-control class, you could add a custom .inset-box-shadow class which would override the box shadow on the panel and give it that formy look. That may confuse users though, they may want to click and type in it.

Twitter Bootstrap + SimpleForm: appending an icon to a form field doesn't show error messages

I want to append an icon to a SimpleForm form field using Twitter Bootstrap. Here is the solution that I found in another SO question:
<div class="input-append date" id="birthday-picker" data-date="06-04-1986" data-date-format="dd-mm-yyyy">
<%= f.input :birthday, :wrapper => :append do %>
<%= f.input_field :birthday, as: :string, placeholder: "06-04-1986", readonly: true %>
<span class="add-on"><i class="icon-th"></i></span>
<% end %>
</div>
However, when I add this piece of code to my form, the validation kicks in but the message is not shown for that particular field. Any ideas on what's happening please?
Update
Here is the HTML generated:
<div class="input-append date" id="birthday-picker" data-date="06-04-1986" data-date-format="dd-mm-yyyy">
<div class="control-group date required error">
<label class="date required control-label" for="user_birthday">Birthday <abbr title="required">*</abbr></label>
<div class="controls"><div class="input-append">
<input class="string required readonly" id="user_birthday" name="user[birthday]" readonly="readonly" size="50" type="text" value="31-03-2012">
<span class="add-on"><i class="icon-th"></i></span>
</div>
<span class="help-inline">You are too young</span>
</div>
</div>
As you can see, the error message is generated. But for some reason, the span that contains the error message gets applied the following styles:
.input-append, .input-prepend {
font-size: 0;
white-space: nowrap;
}
Even though it's outside the input-append div... Any ideas what's happening?
Update 2
The error message was in fact in an input-append div, which I removed. Now the error message appears but it's below the input, not inline like regular inputs...

Setting up error message html view in this way?

I'm using Twitter-Bootstrap and want to generate the correct html to display the error view how it does it on the main site, which is:
The html for the above field is:
<div class="control-group error">
<label for="inputError" class="control-label">Input with error</label>
<div class="controls">
<input type="text" id="inputError">
</div>
</div>
Note: I deleted Please correct the error, <span>, I just want the input field and label.
And if I was to use my sign up page as an example, the email field, it would be:
<div class="control-group">
<label for="user_email" class="control-label">Email*</label>
<div class="controls">
<input type="email" value="" name="user[email]" id="user_email" class="span3">
</div>
</div>
What do I have to do to get it to function like the former?
Don't re-invent the wheel. Use simple_form. The current version of the gem allows you to do the following:
rails generate simple_form:install --bootstrap
With that, you can use the simple_form helpers. They will generate the right things for you.
I've just encountered this issue, and have fixed it with a simple modification to the Bootstrap CSS.
My usual field code is:
<div class="control-group">
<%= f.label :fieldname, t('models.model.fieldname'), :class => "control-label" %>
<div class="controls">
<%= f.text_field :fieldname, :class => 'input-large' %>
</div>
</div>
Since I'm using f.label and f.text_field the label and input are both encapsulated with divs with the field_with_errors class (as Nicholas mentions), making the resulting HTML:
<div class="control-group">
<div class="field_with_errors"><label class="control-label" for="model_fieldname">Field name</label></div>
<div class="controls">
<div class="field_with_errors"><input class="input-large" id="model_fieldname" name="model[fieldname]" size="30" type="text" value=""></div>
</div>
</div>
To make these have the same look appearance as Bootstrap's <div class="control-group error"> style I add some extra selectors into bootstrap.css. I find all references to .control-group.error ... and add duplicate lines with .control-group .field_with_errors .... So I end up with this kind of thing:
.control-group.error > label,
.control-group.error .help-block,
.control-group.error .help-inline,
.control-group .field_with_errors > label,
.control-group .field_with_errors .help-block,
.control-group .field_with_errors .help-inline {
color: #b94a48;
}
It might not be the most elegant way of doing it for Rails, but to me it seemed a lot easier than more dependant gems or overriding the error processing. Yes, you'll have to make the same changes each time you update Bootstrap, but they're fairly simple changes, and you could probably make a patch file to do it automatically.
Rails automatically generates a div with the class field_with_errors when an error message appears. That div wraps the element with error. In order to customize it, you can add this line to application.rb:
config.action_view.field_error_proc = Proc.new { |html_tag, instance| %Q(<div class="field_with_errors">#{html_tag}</div>).html_safe }
This is the default, so in order to get the same structure as Twitter Bootstrap, you could play with it.
html_tag is a placeholder for the field with errors, e.g. <input name="model[attribute]" size="30" type="text" value="">
You could wrap this within another div, and also add a span saying "Please correct the error".
More info: http://guides.rubyonrails.org/configuring.html - item 3.9

rails - select all checkboxes inside a div before submitting form

I have a new batch form where user can select books from a div using the checkbox and click on a button to put them in another div which represents the selected books.
It works a bit like a listbox where user can move items between the different boxes except that here am using checkbox and div.
I have done the moving of books from one div to another using jquery.
See code below:
<% form_for #batch do |f| %>
<label style="width: 150px">Batch Name:</label><%= f.text_field :BAT_BATCH_NAME %>
<div id="all_books">
<% #books.each do |book| %>
<div id="book<%= book.BK_OID %>" bookid="<%= book.BK_OID %>" class="innertxt">
<ul>
<li>Book ID: <%= book.BK_OID %></li>
<li>Name: <%= book.BK_NAME %></li>
<li>
<input type="checkbox" name="batch[book_ids][]" id="select<%= book.BK_OID %>" value=<%= book.BK_OID %> class="selectit" />
</li>
</ul>
</div>
<% end %>
<div style="width:100px; text-align:center; margin-left:20px; padding-top: 100px; width:75px; float:left;">
Right »<br /><br />
« Left
</div>
<div id="selected_books"> </div>
<br/><br/>
<%= f.submit 'Update Batch Details' %>
<% end %>
Currently, only the selected books are saved, whether they are found in the 'all_books' div or 'selected_books' div.
However, I want all entries in the 'selected_books' div to be submitted when saving the batch, whether their checkboxes have been checked or not. and ignore all selected entries in the 'all_books' div.
In short, when saving the batch, i want to save only those books found in the 'selected_books' div and ignore the rest whether their checkboxes have been checked or not.
I hope i was clear enough.
I would be really grateful if someone could point me the right direction on how to do that.
Many many thanks for any suggestion provided.
$("#formid").submit(function(){ $("#selected_books").find("input:checkbox").attr({checked: "checked"}); });
That should do the trick.

custom validation markup in Rails(3)

I'm trying to accomplish the following mark-up for all my form elements
<div class="input-container">
<label>Topic Title</label>
<div class="input-holder">
<input type="text" />
</div>
</div>
<div class="textarea-container">
<label>Post</label>
<div class="textarea-holder">
<textarea></textarea>
</div>
</div>
invalid fields mark-up:
<div class="input-container alert">
<label>Topic Title</label>
<div class="input-holder">
<input type="text" />
</div>
</div>
<div class="textarea-container alert">
<label>Post</label>
<div class="textarea-holder">
<textarea></textarea>
</div>
</div>
here's my current haml markup:
.input-container
= f.label :title, 'Topic Title'
.input-holder
= f.text_field :title
.textarea-container
= f.label :body, 'Post'
.textarea-holder
= f.text_area(:body, :size => "60x10")
Now what would I need to do if I want the container divs to have the alert class when a field in my form is invalid?
I want to do the same thing but unfortunately haven't figured out a clean way to handle it.
You can override ActionView::Base.field_error_proc, but that doesn't help with the enclosing elements.
It can be done manually by checking errors on each field ('post.errors['body'].nil?') and outputting your alert class conditionally.
The next step could be extracting the logic into a view helper, and perhaps some further abstraction after that.
But it would still be nice to do this in a more automated, Rails-y fashion.
It might be best to use either the formtastic or simple_form gems which out of the box support highlighting an individual invalid field.
I'm almost certain they do this by changing the CSS class on the field or its container, which you could also hook into in your stylesheets.

Resources