I feel like I'm going insane. This seems so simple but somehow I cannot find an answer on the internet.
I have a form with a checkbox in it. If the checkbox is checked, I want to display an input field. How do I do that in Ruby on Rails without Javascript? All the solutions I saw were with Javascript for like 9 years old.
= simple_form_for(#register) do |f|
.form-inputs
= f.input :first_name
= f.input :last_name, required: true
= f.check_box :special_request
= f.label :special_request
# and now here should be the if-else statement that should go something like this
if :special_request.checked?
= f.input :request
But I have yet to find out how exactly this is supposed to be implemented. It seems like the easiest task, but I have yet to find an answer. Also, no matter if the checkbox is checked or not, the value is always 1 when I look at it in with inspect.
The closest you'll get to this is the "Checkbox Hack"
.control-me {
display: none;
}
#toggle:checked ~ .control-me {
display: block;
}
<input type="checkbox" id="toggle">
<div class="control-me">
<input type="text">
</div>
Adapted from:
https://css-tricks.com/the-checkbox-hack/
Related
Perhaps I'm being silly but I've been trying to set a default value for an input with ruby on rails for hours and haven't cracked it.
I'm making a partial which can either allow users to create new records but will also show existing records if they exist. Code as follows
<input type="text" <%= (#prices.empty? || #prices.first.name.length == 0) ? 'placeholder="General admission"' : "value=" + #prices.first.name.to_s %> >
Which works perfectly for any value that exists UNLESS there is a space, for example, if price.name = "general admission" OR if price.name = "" (in which case it prints the placeholder) I get the following produced
<input type="text" admission'="" value="'general" id="event_price_name" name="[event][price][0][name]" aria-label="..." class="form-control">
It seems to get tripped up by the space.
Am I trying to use Rails in a way it wasn't designed to be done in? I'm more used to PHP which may be it!
Thanks
You should use the text_field_tag helper to build the input, this is preferred over building it with partial interpolation. Also, placeholder will automatically be overwritten if there is a value so you don't need to handle that part in the code, that's how it behaves by default on the browser.
<%= text_field_tag :admission, #prices.first.name.to_s, {placeholder: 'General Admission'} %>
I have been using simple_form for a few years now, but always either by itself or with Bootstrap. I'm using Foundation in a new project, and I have followed Zurb's instructions for installing the foundation-rails gem. I have also run:
rails g simple_form:install --foundation
And it has created the requisite files.
Now for the confusing part. When I used to generate a controller or a scaffold with simple_form for Bootstrap, it would make the form look great by default.
But when I run those same generators in my new Rails project using simple_form for Foundation, the forms don't appear to be styled at all. The fields stretch all the way across the screen.
Passing in the wrapper HTML classes that are specified in the config/initializers/simple_form_foundation.rb file don't do anything.
For example:
<%= simple_form_for(#organization, html: { class: "horizontal-form" }) do |f| %>
I can see in the HTML source that it is indeed putting "inline-form" as a class on the form, but there's nothing else going on. None of the wrapper stuff around any of the divs is different, it's just plain:
<div class="input string optional organization_official_name">
<label class="string optional" for="organization_official_name">Official Name</label>
<input class="string optional" id="organization_official_name" name="organization[official_name]" type="text" value="Voolith">
</div>
I have not done anything fancy in my application layout yet, other than the nav bar which is using Zurb classes and works perfectly.
I think I am confused on what exactly simple_form is supposed to do here? I don't understand the connection between the configuration in the simple_form.rb and simple_form_foundation.rb files and CSS classes.
Right now, passing the "vertical-form" or "horizontal-form" classes as I have done above doesn't do anything. Isn't simple_form somehow supposed to read that value and render the form differently?
I had this same problem and googling didn't yield quick answers.
I believe instead of
<%= simple_form_for(#organization, html: { class: "horizontal-form" }) do |f| %>
try
<%= simple_form_for(#organization, :wrapper => :horizontal_form) do |f| %>
This references the config.wrappers in config/initializers/simple_form_foundation.rb
I'm having trouble adding a particular html attribute to a Rails form submit.
= form_for :model do |f|
...
= f.submit 'Submit', tabindex: '3'
The tabindex property isn't showing up in the form. I also tried a html hash to no avail.
It just produces this html markup:
<button type="submit" value="Submit">Submit</button>
EDIT: The only alternative way I can think of is to use jQuery.
$('button[type=submit]').attr('tabindex', '3');
But that seems overkill. It seems like there should be a way in Haml.
The result of f.submit 'Submit', tabindex: '3' should be:
<input type='submit' value='Submit' tabindex='3'></input>
However, you appear to be getting a button element, and the tabindex is not showing up at all - so something else is definitely going on here.
If you have a custom form builder and override the submit method, this could certainly be the result. If you need that custom form builder and still want your submit element to be a button you'll need to make sure you're allowing an options hash through the submit method. Not sure what your current method looks like, but you might update it to something like this:
def submit(value, options={})
options.reverse_merge!(
type: 'submit',
value: value
)
button_tag(value, options)
end
How do you replace a collection_select (with :multiple => true) with a list of check_box options, such that there is a check_box option for each object in the collection?
Is there an elegant way to implement this using form builders (i.e. without using *_tag helpers)? I'd like to lean on ActiveRecord's built in functionality as much as possible...
I don't think there's a built-in "elegant" way to do this.
This railscast should get you going, though:
http://railscasts.com/episodes/17-habtm-checkboxes
http://asciicasts.com/episodes/17-habtm-checkboxes (text versionn)
You can do it like this (example using HAML).
%fieldset
Colors I like
- %w[red blue].each do |color|
= f.label color
= f.check_box :colors_liked, {multiple: true}, color, nil
The nil option at the end prevents Rails from building a hidden checkbox by the same name with a 0 value, which you definitely don't want if you're going for multiple selection.
This produces:
<label for="colors_liked_red">Red</label>
<input id="my_form_colors_liked_red" \
name="my_form[colors_liked][]" type="checkbox" value="red">
<label for="colors_liked_blue">Blue</label>
<input id="my_form_colors_liked_blue" \
name="my_form[colors_liked][]" type="checkbox" value="blue">
When the form is submitted, the parameters will contain an array of the values of the checked options.
I am building a dynamic form builder.. And i have a problem which i can't seem to fix.
So i have a db table called "forms"
forms can have "fields"..
The problem is that when a user creates a new 'field' (click add-field) then it should ajax the new field for .. that field.
The problem is that i can't just do something like this:
<%= Form.fields_for Field.new do |field| %>
<%= field.text_field :name%>
<% end %>
Does anybody have an idea? Yes i watch railscasts, yes i googled, yes i found the "complex-forms' repo on github.
But no luck (yet)
If you want an all javascript approach (instead of calling your server to produce the field names) then basically you just need to increment the field names for any new fields.
For example, if you have
class Form < ActiveRecord::Base
has_many :fields
accepts_nested_attributes_for :fields
and the HTML in the form has an input field that has something like
<label for="form_fields_attributes_0_name">
<input id="form_fields_attributes_0_name" name="form[fields_attributes][0][name]" type="text" />
then you need to write some javascript to make it look like
<label for="form_fields_attributes_1_name">
<input id="form_fields_attributes_1_name" name="form[fields_attributes][1][name" type="text" />
You can do something like
$('#form_fields_attributes_1_name').attr('id').split('_');
and
$('#form_fields_attributes_1_name').attr('name').split(/\]\[/);
to get at those numbers.
Here's an example which is refactored here.