Remove default dropdown option added by required true in Rails 5 - ruby-on-rails-5

I have a select dropdown option that is required but its option are dynamically added. So there's a scenario wherein the select dropdown is empty.
= s.input :test_id, :label => "Choose Category", :collection => #model.categories, :required => true
My question is how can I remove the blank option added automatically when you set your dropdown as required (I wanted my dropdown to be empty). I've tried adding include_blank: false but I'm getting an ArgumentError - include_blank cannot be false for a required field.:

Should work with:
= s.input :test_id, :label => "Choose Category", :collection => #model.categories, :required => true, prompt: ''

Related

option_groups_from_collection_for_select with include_blank option

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

capybara + selenium + rspec + element not found

I m using capybara + selenium + rspec to test my application. I have a page, none of whose elements are manipulated by ajax or javascript(hide/show/toggle). When ever i try to submit the form on the page using
click_button 'Save'
it gives me an error stating element not found. i used 'save_and_open_page' to view the html contents and everything looks fine. the page needs to be scrolled down the access the button. Just for curiosity, i changed the button position so that i dont need to scroll the page and can directly access the button when the page is loaded. In the later case, my capybara spec runs fine. I m not able to derive the root of the problem behind this weird behavior of capybara. The issue is, in most of my views, my submit buttons can be accessed after scrolling the page. How do i avoid the above problem?
the gem versions are
capybara 2.1.0
selenium 0.2.10
selenium-client 1.2.18
selenium-webdriver 2.33.0
the capybara code
fill_in 'user_name', :with => 'Abc Xyz'
fill_in 'user_phone', :with => '58743653'
fill_in 'user_password', :with => 'prasad'
fill_in 'user_password_confirmation', :with => 'prasad'
fill_in 'user_designation', :with => 'Programmer'
page.execute_script "window.scroll(0,10000)"
#find_by_id('save_user').native.send_key(:enter)
sleep 20
page.execute_script("$('form.of-setup').submit()") #current hack to make it work
#click_button 'save my profile'
current_url.include?("registrations/terms/#{#user.invitation_token}").should == true
if i uncomment the 'save my profile' button, it gives error as
Failure/Error: click_button 'save my profile'
Capybara::ElementNotFound:
Unable to find button "save my profile"
and if i uncomment the 'send key' line, it gives error as
Failure/Error: find_by_id('save_user').native.send_key(:enter)
Capybara::ElementNotFound:
Unable to find id "save_user"
the haml code is as
...# additional view code
.optional-area
.container
.row
.span2
.span14
%fieldset
= f.text_field :designation, :help => 'Type a job title', :placeholder => "(optional; like Senior Design Engineer)", :label => "Title"
= f.text_area :street, :rows => "2", :help => "Type the company's street number and name", :placeholder => "(optional; like 1111)", :label => "Street Address"
= f.text_field :city, :help => 'Type a city name', :placeholder => "(optional; like San Jose)"
= f.text_field :state, :help => 'Type a stae or province name or abbreviation', :placeholder => "(optional; like CA)", :label => "State/Province"
= f.text_field :postal_code, :help => 'Type a postal code', :placeholder => "(optional; like 95131)"
.row
.span12.offset4
= f.submit "save my profile", :class => "btn primary verylarge toUpperCase", :disable_with => "SAVE MY PROFILE", :id => 'save_user'
= link_to 'cancel', root_path, :class => "cancel-link of-link of-cancel toUpperCase quickhelp", 'data-placement' => "above", :title => "Abandon the action"
...#additional view code

Rails 3.2.8 Remote Form Not Working

I have this form, which is not bound to any model, that I want to ajaxify. I've tried to figure out how to get it to submit via ajax, but I must be doing something wrong because it is not working (it just does a regular POST).
I can confirm that the form tag renders with a 'remote' attribute, but there is not js added anywhere to the form. I also added the :confirm just to see if that would work as well. It does not.
jquery and jquery_ujs are both loaded on the page.
%form{ :action => "/newsletter", :confirm => "Are you sure?", :remote => true, :method => "post", :id => "newsletterForm"}
%p
= label_tag(:q, "Subscribe to our newsletter:")
%p
= text_field_tag(:q, nil, :placeholder => "Your email address")
= button_to("Subscribe", :remote => true)
I just wrote this doing something similar with a form get:
= form_tag('/signup', :method => "get", :remote => true, :id=> 'signup-form') do
%label{:id => 'signup-label', :for=> 'signup-box'}
Enter your email address
= text_field_tag "signup-box", params[:signup], :class => 'text', :required => true, :id => 'signup-box'
= submit_tag "Sign Up", :id => 'signup'
Controller:
class SignupController < ApplicationController
def index
puts "***************************************************"
puts "email sign up"
puts "***************************************************"
render :nothing => true
end
end

How to display only the value in edit page in Active Admin

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

add commit button to same fieldset as inputs in formtastic

How do I add my submit button into the same li as the inputs of my form?
= semantic_form_for :regression_test_environments do |env|
%ul.input-list
= env.inputs :name, :navigation, :url, :title => "New Environment"
%li.commit= env.submit "Save Environment", :class => "submit"
This just puts the li outside of the fieldset with class inputs. Is there a way to include it in the fieldset(and ul)?
Does this work:
= semantic_form_for :regression_test_environments do |env|
%ul.input-list
= env.inputs :name => "New Environment" do
= env.input :name
= env.input :navigation,
= env.input :url
%li.commit= env.submit "Save Environment", :class => "submit"
I am not sure if you have a field :title or you want to name your fieldset (in which case you need to use :name).