I'm trying to disable a field inside a select input.
There is no problem for the standard field:
(this disable the field 'one')
= f.input_field :job, collection: %w[one two three], disabled: 1, include_blank: 'Select one', required: true
What i would like to do now is to disable the blank field after a user selected an other field. I tryed this:
= f.input_field :job, collection: %w[one two three], disabled: 0, include_blank: 'Select one', required: true
But it didn't work... any idea ?
Thanks for your help and your time :).
To disable blank value we use disabled: "" and to select it before user selects anything we use selected: "". Notice that it works with prompt option instead of include_blank only.
= f.input_field :name, collection: %w[one two three], disabled: "", selected: "", prompt: 'Select one', required: true
Related
I have a Series model which $hasMany RainLab\Blog\Models\Posts and when I display this model with 'type' => 'relation', in Posts form it looks perfectly fine:
So, I can select one Series and assign it to a Post. What I wanted to do is to be able to create new series while being on Posts page. To do this I tried to use 'type' => 'partial' for Series field. Partial itself contains just:
<?= $this->relationRender('series', ['readOnly' => false]) ?>
config_relation.yaml contents:
series:
label: Series
view:
list: $/plugin/models/series/columns.yaml
form: $/plugin/models/series/fields_simple.yaml
toolbarButtons: link|unlink|create|add|remove
manage:
form: $/plugin/models/series/fields_simple.yaml
columns.yaml contents:
# ===================================
# List Column Definitions
# ===================================
columns:
title:
label: Title
searchable: true
post_count:
label: Posts
sortable: false
created_at:
label: Created At
type: date
invisible: true
updated_at:
label: Updated At
type: date
invisible: true
fields_simple.yaml:
# ===================================
# Form Field Definitions
# ===================================
fields:
title:
label: Title
span: left
required: true
slug:
label: Slug
span: right
placeholder: Slug
required: true
preset:
field: title
type: slug
So, basically, nothing special here. But:
As you see, it looks terrible by default. And one more issue - when deleting the series (or unlinking, I tried both) it does not refresh the form, values remain there, though the relation is removed in DB.
Am I doing anything wrong here? What is the proper way to nicely display this relation with ability to edit it?
my rails checkbox is always checked in the dom. How can I make it so when the page loads it's checked if the object is true or not?
= f.check_box :split_funding, {class: "split_funding", :disabled => #survey.completed?, id: "split_funding_#{school.id}", "data-attribute" => "split_funding", "data-id" => school.id}
Pass it the checked attribute like so:
= f.check_box :split_funding, {checked: #survey.split_funding, class: "split_funding", :disabled => #survey.completed?, id: "split_funding_#{school.id}", "data-attribute" => "split_funding", "data-id" => school.id}
I'm not sure what you mean by 'if the object is true', so I am assuming that :split_funding will return true/false. If it's a relation, you can chain :present? on the end to check for presence of the relation.
There are a number of times in Rails when I would like to be able to add an attribute to something only if the attribute is of a particular value.
Let me give you an example. Let's say I want to disable a button based on a particular property such as check_if_user_can_be_added:
link_to 'Create account', new_user_path, disabled: (user_can_be_added?)
This all looks fine and well except that disabled happens to be applied in HTML regardless of what value you give it. If you give a button the attribute disabled: false then it will still be disabled.
What I need
# if the button is disabled
link_to 'Create account', new_user_path, disabled: true
# if the button is not disabled
link_to 'Create account', new_user_path
The way that I can see to do it
Getting this means that you need a solution similar to the following which sets up the options hash first and then passes it in subsequently:
options = user_can_be_added? ? {disabled: true} : {}
link_to 'Create account', new_user_path, options
The way I would like to do it...
This doesn't work but trusting in Ruby's beauty I suspect there's something similar out there. This is basically what I'd like to do
link_to 'Create account', new_user_path, ({disabled: true} if user_can_be_added?)
Can I do it, is there perhaps something using the splat operator that gets me there...?
You can just set nil to cause Rails to ignore the attribute:
link_to 'Create account', new_user_path, disabled: (user_can_be_added? ? true : nil)
For this particular case, you can also use || like so:
link_to 'Create account', new_user_path, disabled: (user_can_be_added? || nil)
I'm not familiar with rails, but inline if statements can be achieved as follows in ruby:
(condition ? true : false)
I would assume your code would look like this:
link_to 'Create account', new_user_path, (user_can_be_added? ? disabled: true : nil)
This would essentially pass in nil in place of disable: true if user_can_be_added? resolves to false.
I'm not sure how the link_to function would handle that, though. I guess the reason disabled: false doesn't work, is the link_to function recieves a hash of attributes as the final parameter, which are applied to an html link. Since the disabled attribute in html doesn't need a value, it would just stick in <a href="" disabled> regardless of its value. Someone feel free to correct me though, I haven't used rails.
Years ago I used a Rails plugin for HTML attributes that is still available on Github. That plugin allowed to write html tags like this:
content_tag(:div, :class => { :first => true, :second => false, :third => true }) { ... }
# => <div class="first third">...
Plugins are unsupported by current versions of Rails and plugin only worked for tag helpers, but perhaps this helps you to write a helper yourself.
For many arguments, using the splat will work:
p "Here's 42 or nothing:", *(42 if rand > 0.42)
This won't work for a hash argument though, because it will convert it to an array.
I wouldn't recommend it, but you could use to_h (Ruby 2.0+):
link_to 'Create account', new_user_path, ({disabled: true} if user_can_be_added?).to_h
To me that looks like a great place to use a helper
module AccountHelper
def link_to_create_account disabled = false
if disabled
link_to 'Create account', new_user_path, disabled: true
else
link_to 'Create account', new_user_path
end
end
end
In your ERB it's simply link_to_create_account(user_can_be_added?)
Not everyone likes helpers, but they work for me.
Using Ruby formhelper,
I want the user to see a popup like "fill this out" when they try to submit the form with any empty fields or unselected selects.
To require a text field, this works:
<%= f.text_field :name, :required => "required" %>
To require a select, I'm trying this but it doesn't work:
<%= f.collection_select :metric, Metric.all, :id, :name, :prompt => true, :required => "required" %>
The select is there and its options are populated correctly in the dropdown. But the user should see a popup if they try to submit without selecting another option besides the default "please select" - it never appears.
You need to use Ruby's select tag combined with Ruby's options_from_collection_for_select. You can set your prompt to true or add a custom prompt and required must be in curly braces and its value set to true. For example:
<%= select('trophy','metric', options_from_collection_for_select(Metric.find(:all), :id, :name),{:prompt => 'Select Metric'},{:required => true})%>
Where:
-trophy is the name of an instance variable or a model object
-metric is is the attribute of that instance variable. This is typically a field/column of the table whose data you're displaying.
-:id is the key
-:name is the result
Can someone tell me how to set these check boxes to checked? I'm sure it's simple, but after an hour of trying i think I need to ask! Thanks!
= form_tag movies_path, :id => 'ratings_form', :method => :get do
Include:
- #all_ratings.each do |rating|
= rating
= check_box_tag "ratings[#{rating}]",
= submit_tag 'Refresh', :id => 'ratings_submit'
Ref check_box_tag
check_box_tag "ratings[#{rating}]", 1, !!(rating.rating)
Your 2nd parameter must be value of checkbox
Your 3rd parameter must be a boolean condition which return true/false and depends on it checkbox is checked/unchecked
check_box_tag "ratings[#{rating}]", 1, #selected.include?("#{rating}")
where #selected is an array with the element selected.
According to the api dock, check box tag takes the following options:
check_box_tag(name, value = "1", checked = false, options = {})
This means the first value is the name, the second value is a 'value' and the third value is whether the box is checked, which is default to false. So, in order to check or uncheck the box you can do the following:
- if (some condition)
= check_box_tag "ratings[#{rating}]", "anystring", true
- else
= check_box_tag "ratings[#{rating}]"
The second line just puts a random string into the value field because in this case it does not matter.
Use true for Checked or false for Unchecked at the end of the line
check_box_tag "ratings[#{rating}]", true #checked
or
check_box_tag "ratings[#{rating}]", false #unchecked
Building on the answer by Sali. Strangely, the check_box_tag returns a checkbox with no label text. Here's how you can display text if you're iterating over an array.
- Puppies.each do |puppy|
= check_box_tag(puppy.name, puppy.name, puppy.goodboy?)
= puppy.name
= check_box_tag "ratings[#{rating}]",{},{:checked => ""}