I can't seem to get this prompt text to show, despite it seemingly being constructed exactly as my others are. What am I overlooking? Thanks for any help!
This one doesn't work
<div class="form-group">
<%= form.label :role %>
<%= form.select :role, options_for_select([["University Staff", "staff"], ["Private Clinician", "clinician"]]), {prompt: 'Please Select Your Role'}, { class: "form-control" } %>
</div>
This one does
<div class="form-group">
<%= form.label :university %>
<%= form.select :university_id, options_for_select(University.all.map{ |uni| [uni.name, uni.id] }), {prompt: 'Please Select Your University'}, { class: "form-control" } %>
</div>
I was advised to create a user model class method for selecting the role, and this somehow allowed the prompt to work.
Related
I am trying to use a collection_select for state just to filter cities, but I don't want to save the state_id in db. My "college" model has only city and college field. That's why it's throwing while instantiating :state_id I believe, I am new to rails. I am not able to figure it out. :(
<%= form_for #college do |f| %>
<div class="form-group">
<%= f.label "Select city"%>
<%= f.collection_select(:state_id, State.all, :id, :state, {}, {class: "form-control"})%>
</div><br>
<div class="form-group">
<%= f.label :city_id, "Select city"%>
<%= f.collection_select(:city_id, City.all, :id, :city, {}, {class: "form-control"})%>
</div><br>
<div class="form-group">
<%= f.label :college, "college Name"%>
<%= f.text_field :college, class: "form-control", placeholder: "Enter city name", required: true%>
<br>
</div>
<div class="form-group">
<%= f.submit class: "btn btn-primary"%>
</div>
<% end %>
just define state_id attribute accessor in your college.rb model :
attr_accessor :state_id
inside your model you should define state_id as attr_accessor like :-
class College
attr_accessor :state_id
end
I am trying to let users RELIST 'gigs' that they have previously posted by duplicating the old one and saving it with a new ID. At the moment everything duplicates fine and can be saved but I need one of the fields (datetime) to be blank as it is vital that they manually set this. I do not know how to set this as nil/blank from the controller.
Relist link:
<%= link_to t('gigs.show.relist'), gig_relist_path(:gig_id => #gig.id), class: 'gig-edit-dash' %>
Gig controller:
def relist
#oldgig = Gig.find(params[:gig_id])
#gig = #oldgig.dup
end
Form:
<div class="create-form">
<%= simple_form_for #gig do |form| %>
<div class="create-title">
<%= form.input :title, label: t('gig.title'), placeholder: t('placeholder.title') %></div>
<div class="create-location">
<%= form.input :location, label: t('gig.location'), placeholder: t('placeholder.location') %></div>
<div class="create-genre">
<ul></ul>
<%= form.label :genres %>
<%= select_tag "choose_genres", options_from_collection_for_select(Genre.all, 'id', 'name',#gig.genres.map{ |j| j.id }), :multiple => true %> </div>
<div class="create-description">
<%= form.input :description, as: :text, label: t('gig.description'), placeholder: t('placeholder.description') %></div>
<div class="create-date">
<%= form.input :date, label: t('gig.date'), placeholder: t('placeholder.date') %></div>
<div class="create-salary">
<div class="salary-input">
<p class="salary-title"> <%= t('gig.salary') %> </p>
<%= form.input :salary_currency, label: false,
collection: ["€", "£", "$" ], prompt: "Choose one" %>
<%= form.input :salary, label: false, placeholder: t('placeholder.salary') %>
</div>
</div>
Setting the default value to nil/blank in the form doesn't work as a value is being passed.
#gig.date = nil
doesn't work either, this sets the time to the current time.
Oh dear, I just figured this out about 2 minutes after posting the question. I'll leave the answer rather than deleting the post just in case someone else finds it useful.
I set #gig.date = nil in the controller after #gig = #oldgig.dup
Then added , :include_blank => true to the form field in the view.
The reasoning is pretty self explanatory.
I have a select_tag with options_for_select in a form_for to create an instance of a model but it's a huge list so I want to add a search (allow the user to type their selection and see the narrowed down results as they keep typing) instead of a select. Should I replace the select with a form_tag? How would I go about adding a search field within the form_for?
views/dogs/new:
<%= form_for [#master, #dog], role: "form" do |f| %>
<div class="form-group">
<div><%= f.label :name %><br />
<%= f.text_field :name, :autofocus => true, class: "form-control" %></div>
</div>
<div class="form-group">
<div><%= f.label :age %><br />
<%= f.number_field :age, class: "form-control" %></div>
</div>
<div class="form-group">
<div><%= f.label :breed %><br />
<%= select_tag "breed", options_for_select([['French Bulldog' ,'French Bulldog'], ['Pitbull', 'Pitbull']]) %>
</div>
currently appears as:
You would need to use a text field instead of a select field and use jQuery autocomplete(http://jqueryui.com/autocomplete/).
There's a nice example on how to use it.
You would need just to populate a variable in js with all the breeds in it and then use the plugin to filter the list and use one of its callback in case you need to customize your text field further; e.g. after a user has chosen the breed.
Hope this helps you
I'm fairly new to rails and I'm building my first app. I've searched the web for a correct answer but couldn't find any that worked for my case.
I'm using simple form, with rails 4 and bootstrap 3. I have a :location dropdown on a (#employees) model and I want to populate it with a job_title column from my #positions model. I've used a scaffold to generate my position MVC + job_title:string job_description:string.
How can I populate my :location dropdown (on my employees form) with values from :job_title (from #positions model)? I currently have my code as:
<div class="col-md-6 pad-10">
<% options = options_from_collection_for_select(#positions, 'id', 'job_title') %>
<%= f.select :location, options, :input_html => { class: "form-control" } %>
</div>
But as you know, that doesn't work. Any help is appreciated!
Solution:
<div class="col-md-6 pad-10">
<% options = options_from_collection_for_select(Position.all, 'id', 'job_title') %>
<%= f.select :location, options, :input_html => { class: "form-control" } %>
</div>
I used Position.all instead of #positions.
options_from_collection_for_select needs an Array. Docs
So try using Modelname.all instead of #positions or re-declare #positions with #positions = Modelname.all
Because you are using simple_form, you can render a select box for a collection like this:
<%= f.input :location, collection: #positions, label_method: :job_title, value_method: :id, input_html: { class: "form-control" } %>
This should do it for you if you want the id ofan Position object be written to the location field. If you want the job_title cahnge :id to :job_title.
<div class="col-md-6 pad-10">
<%= f.label :location %><br />
<%= f.collection_select :location, #positions, :id, :job_title %>
</div>
I used Position.all instead of #positions.
<div class="col-md-6 pad-10">
<% options = options_from_collection_for_select(Position.all, 'id', 'job_title') %>
<%= f.select :location, options, :input_html => { class: "form-control" } %>
</div>
I have the following (and working) dynamic menu / dropdown which allows you to select a property type and then a property subtype with a regular rails form:
properties.js.coffee
jQuery ->
prop_sub_types = $('#property_prop_sub_type_id').html()
$('#property_prop_type_id').change ->
prop_type = $('#property_prop_type_id :selected').text()
escaped_prop_type = prop_type.replace(/([ #;&,.+*~\':"!^$[\]()=>|\/#])/g, '\\$1')
options = $(prop_sub_types).filter("optgroup[label='#{escaped_prop_type}']").html()
if options
$('#property_prop_sub_type_id').html(options)
else
$('#property_prop_sub_type_id').empty()
_form.html.erb
<%= form_for(#property) do |f| %>
<div class="field">
<%= f.label :name %><br />
<%= f.text_field :name %>
</div>
<div class="field">
<%= f.label :prop_type_id, 'Property Type' %><br />
<%= f.collection_select :prop_type_id, PropType.order(:name), :id, :name, :prompt => "-- Select Property Type --" %>
</div>
<div class="field">
<%= f.label :prop_sub_type_id, 'Property Subtype' %><br />
<%= f.grouped_collection_select :prop_sub_type_id, PropType.order(:name), :prop_sub_types, :name, :id, :name, :prompt => "-- Select Property Subtype --" %>
</div>
<div class="actions">
<%= f.submit %>
</div>
<% end %>
This works fine. However, I'd like to integrate this into a larger app that is already set up with the simple form gem. I'm also using twitter bootstrap via bootstrap-sass.
The closest I can get in my form is:
<div class="field">
<%= f.association :prop_type, :input_html => { :id => "prop_type_id", :class => "span5" }, :prompt => "-- Select Property Type --" %>
<%= f.association :prop_sub_type, :input_html => { :id => "prop_sub_type_id", :class => "span5" }, :prompt => "-- Select Property Subtype --" %>
</div>
Note: I had to change :prop_type_id to :prop_type to keep the app from throwing errors.
But this is not working - the second drop down won't map to the first. I a doing something wrong in my java/coffeescript? Is there such a thing as 'grouped_association' or something along those lines for the second dropdown?
Is this even doable, or should I convert the entire form back to the standard rails format?
UPDATE
I was able to get it to work but sticking the erb into divs as follows:
<div class="field">
<%= f.collection_select :prop_type_id, PropType.order(:name), :id, :name, :prompt => "-- Select Property Type --" %>
</div>
<div class="field">
<%= f.grouped_collection_select :prop_sub_type_id, PropType.order(:name), :prop_sub_types, :name, :id, :name, :prompt => "-- Select Property Subtype --" %>
</div>
You should have a look at the Group section in the simple_form_for github page. I was working on something similar to what you were doing and found this section. It gave me the direction that I needed to go. Instead of doing f.association which I used on other parts, I ended up using f.input with the :group_select and :group_method
f.input :country_id, collection: #continents, as: :grouped_select, group_method: :countries
from
simple_form_for github pag