Rails HAML View: display alongside checkbox and label - ruby-on-rails

I'm working in a View using HAML and Rails, I need to display alongside the checkbox and its label, my closest result is the next:
And this is the code I'm currently using:
%td
=f.check_box :make_default, class: "checkbox"
=f.label :make_default, class: "checkbox"
As you can see the checkbox and the label are vertically; in case it is useful, the complete code of the form is the next one:
=form_for :document_administration, url: admin_document_administration_path do |f|
%tr
%td=f.text_field :name, placeholder: "Add New Document", class: "form-control"
%td
=f.check_box :make_default, class: "checkbox"
=f.label :make_default, class: "checkbox"
%td
=button_tag( :class => "btn") do
%span.ix.ix-plus
Add
My question is: what is the code to display first the checkbox and alongside the label?
Thanks a lot for your time
UPDATE:
Following one comment I decided to check column width and height and it is 192.933 x 38; also I decided to remove one column, with that change inside the last cell I have the checkbox+label+button, take a look at this:
So, it seems by default all objects inside of the cell are in vertical mode; my new question is: what should be the way to change the orientation mode from vertical to horizontal inside the cell?
The code looks like this:
=form_for :program_type, url: admin_program_types_path do |f|
%tr
%td=f.text_field :name, placeholder: "Add New Program Type", class: "form-control"
%td
=f.check_box :make_default
=f.label :make_default, class: "checkbox"
=button_tag( :class => "btn") do
%span.ix.ix-plus
Add
Thanks a lot

Related

How to change label of simple form input

I Have a code in my project like
<div class="form-inputs">
<%= f.input :twitter %>
</div>
It gives label as Twitter in my website. How it produce label with out giving label parameter. I want to change label to something else like Tweet it.
You can add label in very simple way
<%= f.input :twitter, label: 'Tweet it' %>
Check this simple_form usage
If you want to disable label
<%= f.input :input_field_name, label: false %>
Add custom class to label
<%= f.input :field_name, label_html: { class: 'my_class' } %>

Model-independent Rails 4 file upload in dynamically loaded (nested) form field

I'm looking for a way for simple image upload onto the file system so that the image is not stored in the database. I understand this step is rather easy, just using a file_input tag, however I have problems making it happen because of how my form is built.
I have a project where I create static campaign pages by having a user specify a page layout, or a template, for the page. Once the user selects which template they want to load for their page, a form is loaded that contains a section for each of the widgets that the specified template contains. The user inputs their information, presses submit, and a static page is created.
One of the widgets is an image widget, where the user should select a file from their system and that should get uploaded to the server on submitting the form. The problem is that because of how I'm using the form helpers, I'm facing the issue, where file upload gives filename as a string instead of the IO object (https://www.ruby-forum.com/topic/125637). I need help on trying to work around this.
The form for inputting data onto the page (which is stored in the table campaign_pages) looks like this:
= form_for(#campaign_page, class: 'form-horizontal') do |f|
fieldset
legend Basic page information
.form-group
= f.label :featured, class: 'control-label col-lg-2'
.col-lg-10
= f.check_box :featured, class: 'form-control'
= f.label :title, class: 'control-label col-lg-2'
.col-lg-10
= f.text_field :title, placeholder: 'Unique and really catchy page title!', class: 'form-control'
= f.label :campaign_id, 'Campaign: ', class: 'control-label col-lg-2'
.col-lg-10
= f.select :campaign_id, options_from_collection_for_select(#campaigns, 'id', 'campaign_name', #campaign), {}, html_options= {:class => 'form-control'}
= f.label :language_id, 'Language: ', class: 'control-label col-lg-2'
.col-lg-10
= f.select :language_id, options_from_collection_for_select(Language.all,'id', 'language_name'), {}, html_options= {:class => 'form-control'}
label for='template_id' class='control-label col-lg-2' Template:
.col-lg-10
= select_tag 'template_id', options_from_collection_for_select(#templates, 'id', 'template_name', #template), class: 'form-control'
| data-no-turbolink="true"
This is where forms for content for each widget on the page gets loaded once a template has been selected:
#widget_location
= render :file => 'templates/show_form', layout: false
.form-group
= f.submit 'Save Page', class: 'btn btn-sm btn-primary'
The form for the image widget (which creates the problem) looks like so:
#image_widget_form.form-group
= label_tag 'widgets[image][image_url]', 'Image URL:', class: 'control-label col-lg-2'
.col-lg-10
= text_field_tag 'widgets[image][image_url]', options['image_url'], class: 'form-control'
= hidden_field_tag 'widgets[image][widget_type]', WidgetType.find_by(:widget_name => 'image').id
= file_field_tag 'widgets[image][image_upload]'
After reading the documentation (http://guides.rubyonrails.org/form_helpers.html#uploading-files) and other issue reports, my understanding is that the problem is "multipart => true", which you have to specify if you want your file field tag to work. That shouldn't be a problem, because I'm using form_for #campaign_page instead of form_tag. However, the widgets and their content do not belong to the campaign page table, and so they aren't specified in the model for a campaign page. Thus, I can't simply use f.file_field for the file input.
I'm looking for a solution for this. Would using nested attributes and then using field_for for the image upload work?
Another issue is that specifications and content for the widgets are stored in a Postgres JSONB field, and I'm not sure how nested attributes would work for those.
I assumed that the form had multipart set, because specifying multipart => true for a form_for helper is not necessary according to Rails documentation. The fix turned out to be pretty simple, however - it works after setting multipart: true in a html options hash for the form tag:
= form_for(#campaign_page, class: 'form-horizontal', html: {multipart: true}) do |f|
...

Solr Sunspot Autocomplete Text Field Not Working

I am using the sunspot_autocomplete gem to implement auto suggest to my search bar. Just need some help creating the text field. I am using rails 3.
Before I installed the gem, my search bar's code looked like this:
<%= form_tag dairies_path, :method => :get do %>
<p>
<%= text_field_tag :search, params[:search], style:"width:550px; height:30px;", :autofocus => true %><br>
<%= submit_tag "Search!", :name => nil, class: "btn btn-primary btn-lg", style: "margin-top:20px" %>
</p>
<% end %>
The autocomplete gem requires this code for a text field:
<%= autosuggest_text_field "dairy", "title", "http://127.0.0.1:8983/solr/", "dairy_title"%>
My question is do I replace all of the search bar code with that ^ or do I just replace the text_field_tag line? I have either been getting errors, or the entire code shows up on my website. Sorry for the beginner question. Still lots to learn! Thank you for your help.

Rails CSS for text_field_tag default value

Text field Tag:
<%= text_field_tag :note, "#{f.notes}", :placeholder => 'Add a note about this rating set..' %>
How can I style f.notes to have a background color so that the note looks highlighted after being submitted.
if you put
:class => 'cssclass'
it will add the css class style that you want

Disable a Text Box in Ruby on Rails?

I have the code:
<% generate_bullets = Bullet.all %>
<% generate_bullets.shuffle.first(4).each do |t| %>
<%= f.text_field, :bullets, :class => 'text_field disabled' %>
I want to disable a text box using embedded ruby, and am unable to do so. If I could receive any help on the situation I'm facing, it would be very greatly appreciated.
After I disable the text box I want to have a button generate four random ID's from the database table "bullets" and print them on the disabled text box in an array format, and utilize those four printed ID's to post them onto a created page. Any help with that would be even better.
Let me know if I'm reading this right: you're trying to disable the text field from the get-go in the HTML. Is that right?
If so, disabled isn't a class; it's its own attribute.
<%= f.text_field, :bullets, :class => 'text_field', :disabled => true %>
You can also use :readonly => true attribute.
For HAML
= f.text_field :name, :class => "form-control", :readonly => true
For ERB
<%= f.text_field :name, :class => "form-control", :readonly => true %>

Resources