I have some trouble here trying to include an include_blank option for 'option_groups_from_collection_for_select'
I would like to have include_blank option but have the currently selected value displayed instead of a blank selection on update action. I have tried this here but it still shows blank.
select_tag(:candidate_source, option_groups_from_collection_for_select(grouped_candidate_sources, :second, :first, :id, :source), { :include_blank => true, :selected => :source })
The last argument in the option_groups_from_collection_for_select is the selected option, which should map to your value.
Since your option_key_method (4th arg) is :id you're allowed to pass the form object directly as an argument. Example:
select_tag(:candidate_source, option_groups_from_collection_for_select(grouped_candidate_sources, :second, :first, :id, :source, f.object.source), { :include_blank => true }
Anyway, see here for more argument information: http://apidock.com/rails/ActionView/Helpers/FormOptionsHelper/option_groups_from_collection_for_select
Related
We use simple_form 2.0.2 in our rails 3.2 app. For some boolean fields, we want to make it readonly in certain situation and does not allow to update. Here is the code in our app:
<%= f.input :signed, :label => t('Signed'), :as => :boolean, :readonly => readonly?(#project, 'signed') %>
What we find out is that if :readonly => true, when mouse is over the field on simple form, there is a little red circle (with a slash in the circle) showing up. However the check box still could be changed and saved. Is there a way to make boolean check box on simple_form read only and can't be updated with :readonly? Thanks.
Not with :readonly, no.
The readonly HTML input attribute only prevents the user from changing the value of the field. It doesn't stop them from interacting with it, as clicking on it and toggling the checkmark shows. That only changes the state of the checkbox, whether it's on or off.
The specs on the readonly attribute say this:
readonly
This Boolean attribute indicates that the user cannot modify the value of the control.
If you don't want them changing the state at all, you may want to use disabled:
disabled
This Boolean attribute indicates that the form control is not available for interaction. In particular, the click event will not be dispatched on disabled controls. Also, a disabled control's value isn't submitted with the form.
But since the input is not sent along, the value will be missing. So consider pairing it with a hidden field that actually holds the value.
<%= f.input :signed_display, :label => t('Signed'), :as => :boolean, :disabled => true %>
<%= f.input :signed, :as => :hidden, input_html: {value: #project} %>
I have a form which has a select box with categories. If a particular category is selected, another select box/dropdown is displayed to the user. I need this second dropdown to be a required field when this category is selected.
I cannot add validates :course, :presence => {:message => 'Course cannot be blank.'} to the model because this field is not always required, I need some other way to make it required only when certain category is selected in the first dropbox.
Thanks for your help
You could try using the if argument to validates, like this:
validates :course, :presence => {:message => "Course cannot be blink."}, :if => Proc.new { |u| u.first_dropdown_value == 'value_that_you_validate_courses_for' }
For more information, check out the Rails documentation on conditional validation.
I have a edit form in Active Admin. I need some field as read only.
My current edit page is like
I need the page look like this
How can this be done. My code for the edit form page is like
form :html => { :enctype => "multipart/form-data" } do |f|
f.inputs "Users" do
f.input :device, :label => 'Device', :as => :select, :collection => DEVICE, :include_blank => false
f.input :current_address, :label => 'Current Address', :as => :string
end
end
Please help.
As Alex said, set to disabled. You could then use css to get the visual you wanted, if you can live with the semantics of that.
The syntax was slightly different for me to get this to work.
in your admin form:
f.input :finish_position, input_html: { disabled: true }
in your CSS active_admin.css
input[disabled="disabled"],
input[disabled] {
background-color: #F4F4F4;
border: 0px solid #F4F4F4 !important;
}
For a cleaner form definition within your ActiveAdmin.register{} block you may as well want to define a "readonly" input type to be used within active admin using formtastic:
Form block syntax is for activeadmin version 1.0.0.pre at 0becbef0918a.
# app/admin/inputs/readonly_input.rb
class ReadonlyInput < Formtastic::Inputs::StringInput
def to_html
input_wrapping do
label_html <<
template.content_tag('div', #object.send(method))
end
end
end
# app/admin/your_model.rb
ActiveAdmin.register YourModel do
# ...
form do |f|
# ...
input :current_address, as: :readonly
# ...
end
end
I was facing the same issue and tried using :disabled but it did not solve my problem as I wanted field value to be included in params object while sending it to the server. When you mark a form input as :input_html => {:disabled => true} , it does not include this field value in params.
So, instead I used :input_html => {:readonly => true} which solved both of my problems:
Does not allow user to edit
Includes the value in params
I hope this will help.
How about this?
form :html => { :enctype => "multipart/form-data" } do |f|
f.inputs "Users" do
f.input :device, :label => 'Device', :as => :select, :collection => DEVICE, :include_blank => false
f.li do
f.label :current_address
f.span f.object.current_address
end
end
end
Try to add , :disabled => true for address input field.
The trick is to use "object". Here is how you should code it:
form :html => { :enctype => "multipart/form-data" } do |f|
f.inputs "Users" do
f.input :device, :label => 'Device', :as => :select, :collection => DEVICE, :include_blank => false
f.label :current_address, f.object.current_address
end
end
This question already has an answer here:
Rails simple_form server side validations lost URL params
(1 answer)
Closed 8 years ago.
I have a simple_form in my rails app that I want to pre-populate
some of the fields with URL parameters.
Example URL is:
http://localhost:3000/surveys/new?phone=8675309
This pre-populate the phone field corretly using this code:
<%= simple_form_for #survey, :html => { :class => 'form-horizontal' } do |f| %>
<%= f.input :state, :required => true %>
<%= f.input :zip, :required => true %>
<%= f.input :phone, :required => true, :input_html => { :value => params['phone'] } %>
<% end %>
The problem is if the form is submitted without a required field
the form is reloaded but the phone input value is not retained.
If the form is submitted without a value for zip
the reloaded page with validation errors in red URL is now:
http://localhost:3000/surveys
If, say, the state field is correct but no zip code the form is reloaded
with an error msg saying zip is required.
The state value is retained however the phone is not.
I guess it has to do with :value => params['phone'] in the f.input for the phone.
Is there anyway I can populate the simple_form with URL paramenters
on it's initial load and have those value retained if the serverside validation fails?
Many thanks.
Remove the :value from your view:
<%= f.input :phone, :required => true %>
and use this URL:
http://localhost:3000/surveys/new?survey[phone]=8675309
That should generate the "standard" survey parameter hash expected by the controller. In my test, that works the same as if the user had typed in the value, with the usual validation handling.
Inside the controller, the hash is called params[survey] with individual parameters represented as params[survey][phone], params[survey][zip], etc.
With the help of this answer, I found that you can generate the URL with the link_to signature link_to(body, url_options = {}, html_options = {}):
<%= link_to 'New survey', { :controller => "surveys",
:action => "new",
:survey => { :phone => "8675309", :zip => "10001" } },
{ :target => "_blank" } %>
Note that url_options is the first hash, and within that hash you have a survey hash for pre-loading values. Finally comes the optional hash of html_options (where I added target="_blank" for illustration purposes).
Thanks Mark,
I re-posted this question again a while back and it was answered corretly here:
Rails simple_form server side validations lost URL params
I have a few checkboxes that submit and change an attribute's value on my profile. The attribute was created as a boolean with :default => true. For every other checkbox I set up like this, the attribute's value is either true/false. However this one checkbox is either true or nil.
Despite setting things up the same, how is that possible? I need the form to only change the value to either true or false. Not nil. Can anyone help me out?
Here's the migration that adds the column in my Profiles table:
class AddAccessItemsToProfiles < ActiveRecord::Migration
def self.up
add_column :profiles, :access_items, :boolean, :default => true
end
def self.down
remove_column :profiles, :access_items, :boolean, :default => nil
end
end
(Before I go on, I notice the self.down is :default => nil, but I have that in the other migrations that work. Plus I didn't think that would cause an issue.)
Here is my Controller action to update the value:
def access_items_settings
if current_user.profile.update_attributes(:access_items => params[:access_items])
redirect_to settings_path
else
redirect_to settings_path, :notice => 'Oops, something went wrong. Please try again.'
end
end
Here is the form I submit:
<%= form_tag(access_items_settings_path, :method => :put) do %>
<p class="setting"><%= check_box_tag(:access_items, 1, #user.profile.access_items ? true : false) %>
This lets your items be accessed.
<br><span class="indentClarify">(Some details.)</span></br></p>
<% end %>
(I removed the extra br, p, span, but that did nothing.)
And finally the jQuery:
$(function(){
$(':checkbox').live('click',function() {
$(this).closest('form').submit();
});
});
This has nothing to do with the database default value.
When a check box is unchecked the browse sends no value at all so params[:access_items] is null and that is what gets saved in the database (since your column allows nulls). The rails check_box helper (unlike check_box_tag) uses a hidden field so that an unchecked box also submits a value. You might want to use the same technique.
I don't know what is different about those other checkboxes so can't comment on those.