I have the following form to edit
<%= form_for #post do |f| %>
<%= f.text_field :title %> #This shows correctly
<%= f.collection_select :product, Product.all, :id, :name %>
<% end %>
product is the column which will save the id(primary key) of Product table. How to show the saved value in my select box.
Just use :selected option
<%= f.collection_select :product, Product.all, :id, :name, :selected => #post.product %>
But this works
<%= f.collection_select :product, Product.all, :id, :name, :selected => #post.product.id %>
Related
I am getting this error in the Production site and this is the code in my views
<%= form_for [:admin, #course], :remote => true do |f| %>
<%= f.label :name %>
<%= f.text_field :name %>
<%= f.label :duration %>
<%= f.number_field :duration, class: "input-md form-control mb-20"%>
<%= f.label :program_id %>
<%= f.collection_select :program_id, Program.where('id'), :id, :name, {}, {class: "input-md form-control mb-20" } %>
<%end%>
This works in my local server where i have sql db setup.
Program model
has_many :courses
can anyone guide me?
Where clause isn't calling anything to compare with, so PG doesn't know what to include in the results. A where clause must evaluate to true/false.
just replace
<%= f.collection_select :program_id, Program.where('id'), :id, :name, {}, {class: "input-md form-control mb-20" } %>
by
<%= f.collection_select :program_id, Program.all, :id, :name, {}, {class: "input-md form-control mb-20" } %>
If you have some problem with some of the Program in your data base, add a column as status in programs table and make changes here as
<%= f.collection_select :program_id, Program.where("status =?", true), :id, :name, {}, {class: "input-md form-control mb-20" } %>
class Customer < ApplicationRecord
attr_accessor :date
validates_presence_of :name, :principalAmount,:interestRate,:accountType,:duration,:date
end
Except date all are there in my customers table. I am using simple_form for getting those values. But problem is that there is no validation happening for :date.For others it displays message if the field is empty. How do I display presence_of validation message in simple_form for :date. I am using bootstrap datepicker.
<%= simple_form_for #customer do |f| %>
<%= f.input :name,:autocomplete => :off %>
<%= f.input :principalAmount,:autocomplete => :off %>
<%= f.input :interestRate %>
<%= f.input :accountType %>
<%= f.input :duration,:autocomplete => :off %>
<%= f.text_field :date, "data-provide" => 'datepicker',"data-date-format"=>"dd-mm-yyyy" %>
<%= f.button :submit %>
<% end %>
Edit: When I change f.text_field to f.input it shows the validation message. But it is no more a date picker.
I want to include BOTH manager first name and last name in a select box below. How can I accomplish this?
Form:
<%= simple_form_for #office do |f| %>
<%= f.input :street_address %>
<%= f.input :city %>
<%= f.input :postal_code %>
<%= f.input :description, as: :text %>
<%=f.input :manager_id, collection: Manager.all, :id, :last_name, include_blank: true %>
<%= f.submit 'Add Office' %>
You can add something like
label_method: lambda { |manager| "#{manager.first_name} #{manager.last_name}" }
to your f.input
or you can create a new method "name" in your model, and use this one instead
def name
"#{first_name} #{last_name}"
end
then
label_method: :name
I'd like to implement automatic selections of prefectures depending on the country that user choose.
I mean, it shows California, New York, and etc in prefecture select if user choose U.S.
Country model has
id
name
Prefecture model has
id
country_id
prefecture
Now I'm showing these field like this
<% resource.build_user_profile if resource.user_profile.nil? %>
<%= form_for(resource, :as => resource_name, :url => registration_path(resource_name), :html => { :method => :put, :class => 'form-horizontal' }) do |f| %>
<%= devise_error_messages! %>
<%= f.fields_for :user_profile do |profile_form| %>
<label class="control-label"><%= profile_form.label :country_id %></label>
<%= profile_form.collection_select("country_id", Country.find(:all), :id, :name_en) %>
<label class="control-label"><%= profile_form.label :prefecture_id %></label>
<%= profile_form.collection_select("prefecture_id", Prefecture.find(:all), :id, :name) %>
<% end %>
Try to change form like this
<%= f.label :country_id %><br />
<%= f.collection_select :country_id, Country.all, :id, :name_en, include_blank: true %>
<%= f.label :state_id, "State or Province" %><br />
<%= f.grouped_collection_select :prefecture_id, Country.all, :prefectures, :name_en, :id, :name, include_blank: true %>
And paste in proper coffee file (if model is profile than profile.js.coffee) If it's in other model change profile in file name and inside file
jQuery ->
$('#resource_name_user_profile_prefecture_id').parent().hide()
prefectures = $('#resource_name_user_profile_prefecture_id').html()
$('#resource_name_user_profile_country_id').change ->
country = $('#resource_name_user_profile_country_id :selected').text()
escaped_country = country.replace(/([ #;&,.+*~\':"!^$[\]()=>|\/#])/g, '\\$1')
options = $(prefectures).filter("optgroup[label='#{escaped_country}']").html()
if options
$('#resource_name_user_profile_prefecture_id').html(options)
$('#resource_name_user_profile_prefecture_id').parent().show()
else
$('#resource_name_user_profile_prefecture_id').empty()
$('#resource_name_user_profile_prefecture_id').parent().hide()
I have this link in my code:
link_to "New question", new_question_url(:category_id => #category.id)
I have this code in my new question form:
<p>
<%= f.label :category_id %><br />
<%= f.collection_select :category_id, Category.all, :id, :name %>
</p>
How can I make Rails automatically select the category from the category_id querystring item, so it is the default one in the collection_select?
Thanks.
Your need to pass in a reference to the current object like this:
<%= f.collection_select(:your_object, :category_id, Category.all, :id, :name) %>
collection_select(:post, :author_id, Author.all, :id, :name_with_initial, {:prompt => true})
"The value returned from calling method on the instance object will be selected. If calling method returns nil, no selection is made without including :prompt or :include_blank in the options hash."
See here:
http://api.rubyonrails.org/classes/ActionView/Helpers/FormOptionsHelper.html