Select prompt option disappears when validation fails in Rails - ruby-on-rails

Let me preface by saying I'm a noob to Rails and StackOverflow so please go easy on me. I'm using Rails 2.3.8 with sqlite3 on my dev box.
I have created a select pulldown in a form using the following:
<%= select( "communication", "gig_id", { "Add New Gig" => "new"}, {:prompt => "-- Select Gig --"}, :onchange => "toggle(this, 'gigInfo')") %>
However, when something else in the form fails validation and the "new" page is re-rendered, my prompt goes away and the only option left is the "Add New Gig" option. This is the case with ALL my forms and I can't seem to find any answer as to why.
My controller uses the basic scaffolding so I'm sort of at a loss. Any help would be greatly appreciated.

Your observation is accurate, and you astutely observed that :include_blank remains even when :prompt does not.
You can achieve the results you want by setting :include_blank to the string you were using for prompt (it doesn't need to be a boolean).
:prompt, it seems, only appears when there is no value for Rails to supply the given field. When your app re-renders the new view, there is a value to supply that field because it created an instance of your Communication model. (It failed to save that instance, but it looks as though that instance has its gig_id field set.)

Related

Conditionally Omit/Hide Button in Rails

My first time asking. I'm in an internship so I'm not that proficient in Rails. Currently I'm working in a code that isn't mine, it has no automated tests and no documentation whatsoever, so I spent lots of time trying to understand what the functions does before actually writing some code.
My question:
What I'm trying to accomplish is to hide/omit a button, under a set of conditions.
My research:
Already searched everywhere but with no luck to my specific problem. Tried the solutions here: How to disable all form_for input fields in Ruby on Rails app? and here: In Rails edit action form, hide a form field. Also read rails documentation about form helpers: https://guides.rubyonrails.org/v4.2.10/form_helpers.html
The problem:
There is an object called school that is divided into Schools and SchoolGroup, theoretically schools that have children should be SchoolGroup. I'm trying to omit the Delete button to every SchoolGroup that have Schools associated with it.
If I put like this nothing happens
<% if school.children.length < 1 %>
<%= button_to 'Delete', school, :data => {:confirm => 'Are you sure you want to delete this school?'}, :method => :delete, :class=>"btn btn-sm btn-delete" %>
<% end %>
However if I put like this
<% if school.children.length > 0 %>
every button disappears!
So, I'm inferring that every school probably have children, even though it shouldn't and I need to ask for a different condition. Or should I try a controller-wise solution? If so, what can I do?
I'm using ruby 2.2.10 and rails 4.2.10.
Thank you all for your time and any help :)
The school.children.length returns the size of the collection. So,new child is also counted.
length() Returns the size of the collection calling size on the target. If the collection has been already loaded, length and size are equivalent. If not and you are going to need the records anyway this method will take one less query. Otherwise size is more efficient.
You can use school.children.count to count the records that persisted in the database. This method call will fire a database query.

How to get value in controller of an bootstrap-multiselect-rails

I'm new to rails and web. I use the 'bootstrap-multiselect-rails' gem to create a select box and normal inputs.
When I click the submit button, I don't retrieve the select box informations in the params variable (the inputs are well retrieve).
This is probably due to the bootstrap-multiselect gem that overrides the behavior of the select box by replacing it with a ul,li system.
%select#select-type{:multiple => "multiple"}
- #list_of_type.each do |type|
%option{value: "#{type.label}"} #{type.label.upcase_first}
I have seen answers that explains in retrieving the information in javascript but how to perform treatments on its information in the controller?
In your select field I cannot see that you assigned a param to it. Usually select goes like this: <%= select :input, etc... where :input is the param you want to save into your model.
Rails docs give following example:
select("post", "person_id", Person.all.collect {|p| [ p.name, p.id ] }, { include_blank: true })
So the problem is that you did not specify the param. It is also not empty, it is not mentioned at all in your Parameters. Correct?! Than please check the basic docs again on how to create a select field and add the param as requested. Unfortunately, I do not know the exact syntax using your gem.
I am also not sure about the :multiples=>"multiple". or should it be multiple: true? But I think your version might work with the gem. However, your haml only creates the HTML select with #id and multiple: true. But what you want is something like this
<select name="post[category]" id="post_category">
So that the param is clear in the select.
Afterwards, when you have saved the param you can use it also in your controller action.
In conclusion: Add the param to your select field. I don't know how to do this with the gem but you might find some docs may be, or check basic rails docs which might help you out here for sure. Also SO has many questions on select with rails. Good luck!
Can you show the rails code for the multi form? You just need to add a value to each select option which will be stored by rails if you have permitted the params in controller and set up a column for it migrations.
If questions on how to add value to select_tag check out the rails docs. In your html markup i cannot see if the options have value.

Rails simple_simple form, feature test failing to populate field/sometimes

I have a finicky test(s).
I'm using simple_form and rails to create a pretty standard job/college type application for users to fill out.
Issue is: Sometimes in the feature test, when a user is editing the application(created via Factoryfirl), rails/simple_form fails to populate a datefield with some of the variables of the date. This also seems to only happen when include_blank: is true.
I know it's being created with a date. The field is failing to be populated with the date, sometimes and only the year column.
I don't think this is an issue with anything I wrote as the tests do work sometimes. Could be a config issue so:
rails-4.2.5.1
rspec-core-3.3.2
capybara-2.5.0
the call to the field:
= required_field_on_submit(f, :profile, :birthdate, as: :date, start_year: years_ago(100), end_year: years_ago(Application::MINIMUM_AGE - 1), include_blank: true, order: order(:m, :d, :y))
required_field_on_submit is just a custom wrapper for f.input, bc of the way applications are being validated. Nothing funky happening there, already checked.
As I mentioned in the comments - A usual cause of a date select not being correctly filled by a page during tests is the data being generated (factory, etc) being outside the acceptable range of the date select
Are you able to select the value for which is given by (years_ago(100)) from front end manually? If yes then make sure that the page is loaded properly, before passing the values. You can wait for an element or verify a message on the page.
You can also try debugging the failing cases, by adding "puts" statement to print the date in the console, so that it can be verified that whether the date is in correct range or not.

How to differentiate multiple, identical form submissions based on their parent object in the view

I'm building a rails view that has lots of nested objects. Each system has a subsystem, so I'm displaying a list of subsystems underneath each system. I also want a little form bellow each system to add new subsystems.
= semantic_form_for #subsystem, :validate => true, :class => "subsystem_form" do |subsystem_form|
= subsystem_form.input :name
= subsystem_form.input :maintenance_interval , :label => "Maintenance Interval(days)"
= subsystem_form.input :run_hours
= subsystem_form.buttons do
= run_form.submit
I don't want the user to have to specify which system they're assigning the subsystem to. That should be dependent on which system the form is under. So my question is: how do I pass that information to the controller so that the subsystem ends up with the correct system_id? I was thinking about trying to manually add it to the params hash, but that doesn't seem right.
If you have nested object then why you are not using nested forms.
Check this out https://github.com/ryanb/nested_form
It will made your life much easier

Data inserted in Cucumber tests not populating Dropdown

DISCLAIMER: I am still a Rails/Cucumber newbie but I am trying to learn. I am still using the "web_steps" for Cucumber while I work through the RailsInAction book and I have experimented with writing my own steps. I understand why they were taken out and I will get there.
Problem:
In one of my Cucumber Scenarios I am populating some data and trying to use that data to populate a dropdown. The data is definitely being inserted, I know this because I wrote a step to test to see if the data is in fact there and it is.
When then DropDown is suppose to populated and I try "select" from that DropDown I get the error "*cannot select option, no option with text 'Steven' in select box 'result_winner' (Capybara::ElementNotFound)*"
I am sure I am missing something basic but I have lost about an hour to this now and I just need some help.
My Code:
The tests:
When I follow "Record new Result"
And the users "Steven, Joshua" exist
And I select "Steven" from "result_winner"
The test steps:
When /^the users "([^"]*)" exist$/ do |playerNames|
#names = playerNames.scan(/[\w']+/)
for name in #names
#newPlayer = Player.new
#newPlayer.name = name
#newPlayer.save
end
end
Then the view code in the _form.html.erb:
<p>
<%= f.label :winner %>
<%= select("result", "winner", #players.map {|p| [p.name, p.id]}) %>
</p>
The Controller:
def new
#result = Result.new
#players = Player.find(:all)
end
If I manually populate the development database with names and load it up in the browser, the dropdowns have data populated in them and work perfectly. I am pretty sure I am misunderstanding when/where test data is visible, or how to test for and select data from a dropdown.
Any help would be greatly appreciated.
It looks to me like a simple problem with the step sequence. You're visiting the page, and then populating the database, but the page doesn't get reloaded to take account of the new database values. If you swap the order of the first 2 steps around, I expect that would fix it.
To be clear, cucumber will literally step through your scenario one line at a time:
Load up the webpage (which doesn't contain your test data)
Insert your data into the DB
Look for that data in the dropdown (which won't be there as the page was loaded before the data was in the DB)
Your "the users "Steven, Joshua" exist" should be in a Given step.
It describes a state before an action is done.
Given the users "Steven, Joshua" exist
When I follow "Record new Result"
And I select "Steven" from "result_winner"

Resources