I have this in my form:
<%= f.select :status, options_for_select(Article::STATUS), :include_blank => true, :selected => #article.status, :class => 'select_field' %>
Model:
class Article < ActiveRecord::Base
attr_accessible :author, :content, :publication_date, :status, :title, :tag_tokens
attr_reader :tag_tokens
STATUS = ['Rascunho', 'Em Revisão', 'Publicado']
end
Why does not populate the field?
I believe you can simply pass an array in [#select][1]. You don't need to call options for collection. I also think you can ommit the :selected option if this is a model form, Rails will figure it out using the attribute value.
Try this:
<%= f.select :status, Article::STATUS, :include_blank => true, :class => 'select_field' %>
Related
These things i have all ready done, but at that time the value of text_field is not insert into database table, reaming value insert successfully but one value is not inserted.
I can understand where am i wrong and what is missing...
_employee_details.html.erb
<%= form_for(:employee_details, :url => {:controller => 'hr', :action => 'create'}) do |f| %>
<%= f.text_field :employee_id, { :value=> "123548", :disabled=>true , :required => true, placeholder: 'E12345678', class: 'form-control' } %>
<% end %>
hr_controller.rb
class HrController < ApplicationController
def new
#employees = EmployeeDetail.new
end
def create
#employees = EmployeeDetail.new(employee_params)
if #employees.save
redirect_to :action => 'internal_employee_page'
else
redirect_to :action => 'internal_employee_page'
end
end
private
def employee_params
params.require(:employee_details).permit(:offer_letter_id, :employee_id, :bank_ac, :bank_ifsc, :spouse_name, :gender, :work_end_date)
end
end
All value is successfully insert into employee_details table accept employee_id
employee_details.rb this is my modal
class EmployeeDetail < ActiveRecord::Base
belongs_to :user
validates :offer_letter_id, presence: true
end
I have already try this but this is not working
Disabled input will not submit data.
Instead of disabled Use the readonly attribute:
<%= f.text_field :employee_id, { :value=> "123548", :readonly => true , :required => true, placeholder: 'E12345678', class: 'form-control' } %>
<%= form_for(:employee_details, :url => {:controller => 'hr', :action => 'create'}) do |f| %>
<%= f.text_field :employee_id, { :value=> "123548", :readonly => true , :required => true, placeholder: 'E12345678', class: 'form-control' } %>
<% end %>
Here is the documentation of it
I'm trying to add country selection in Devise registration and I use country_select gem from https://github.com/stefanpenner/country_select#example
There explains the simple usage by using country_select("user", "country") use model and attribute as parameters:
Problem: When I push submit button user was created and Everything is good except country column doesn't has data from my selection
Purpose: After submit registration I want to insert country which I have selected from Signup form into database(table: users, column: country) also
sign_up.html.erb
<h2><center>Sign up</center></h2>
<%= simple_form_for(resource, as: resource_name, url: registration_path(resource_name)) do |f| %>
<%= f.error_notification %>
<div class="form-inputs" style="float; margin:0 auto;width:35%">
<%= f.input :email, required: true, autofocus: true %>
<%= f.input :password, required: true %>
<%= f.input :password_confirmation, required: true %>
<%= f.label :country %>
<%= country_select("user", "country") %> <<-- My model's name is user.rb and in my users table has a country column
</div>
<div class="form-actions" style="float; margin:0 auto;width:10%">
<%= f.button :submit, "Sign up" %>
</div>
<% end %>
**My model's name is user.rb and in my users table has a country column
Thanks for advance
try this out
<%= f.input :country, as: :country %>
I have solved my problem by following this Question and adapting to my issue Adding extra registration fields with Devise
I'm creating override controller(Registrations Controller in this case) for allow Devise to adding country variable to database
Create new registrations_controller.rb
I have added :country in this file
class RegistrationsController < Devise::RegistrationsController
before_filter :configure_permitted_parameters, :only => [:create]
protected
def configure_permitted_parameters
devise_parameter_sanitizer.for(:sign_up) { |u| u.permit(:email, :password, :password_confirmation, :country) }
end
end
You can see the original registrations_controller.rb at this link: https://github.com/plataformatec/devise/blob/master/app/controllers/devise/registrations_controller.rb
Create a route to allow Rails can routing to override controller by add these lines
in routes.rb
devise_for :users, :controllers => { :registrations => 'registrations' }
Important Please make sure that you don't have any devise_for :users line in your routes.rb, If you have, delete it
Well, now I can use <%= country_select("user", "country") %> to save country selection into database without any problem
I have the following collection select which acts as a filter in a Rails app.
<%= form_tag( "/appointments", :method => "get", :id => "filter_form") do %>
<%= collection_select :doctor, :id, #doctors, :id, :full_name, {:include_blank => 'All'} %>
<% end %>
This always generates a name attribute of the select element like name="doctor[id]" which results in the browser to ?utf8=✓&doctor%5Bid%5D=1, which is not quite readable.
How can I change the name attribute to just name = "doctor" or basically just remove the brackets from it?
http://api.rubyonrails.org/classes/ActionView/Helpers/FormOptionsHelper.html#method-i-collection_select
The collection_select method contains the parameters "options" and "html_options". "options" allow you to add specific information, like {:include_blank => 'All'}, but does not replace html attributes.
You have to add the name to the next hash, like this:
<%= form_tag( "/appointments", :method => "get", :id => "filter_form") do %>
<%= collection_select :doctor, :id, #doctors, :id, :full_name, {:include_blank => 'All'}, {:name => 'doctor'} %>
<% end %>
Have you tried:
<%= form_tag( "/appointments", :method => "get", :id => "filter_form") do %>
<%= collection_select :doctor, :id, #doctors, :id, :full_name, {:include_blank => 'All', :name => 'doctor'} %>
<% end %>
i have created a dropdown in my application using the following code:
<% form_for :categories, :url=> {:controller => 'products', :action => 'cat'} do |f| %>
<%= f.select(:category , Categories.all.map{ |u| [u.name, u.id] }, :prompt => "Select a category") %>
<input type="submit" value="go"/>
<%end%>
controller:
def cat
#products = Product.search_category params[:category]
end
it is returning 'nil' as the parameter when i select any category from dropdown. what could be the reason?
You should expect the category id to be in params[:categories][:category] because that's how the form was set up. If you want to use params[:category], pass a name option to select
<%= f.select :category, Categories.all.map{ |u| [u.name, u.id] }, { :prompt => "Select a category" }, { name: 'category' } %>
That aside, I find it better to use collection_select when you're dealing with an ActiveRecord table
<%= f.collection_select :category, Categories.all, :id, :name, { prompt: 'Select a category' }, { name: 'category' } %>
I think the problem lies with your symbol used with form_for. Generally, a object is passed to form for as it will generate the proper routes.
This question is discussed here
fields_for doesnt working when form_for use symbol
and
Ruby on Rails : symbol as argument in form_for
I'm trying to use the HTML5 multiple attribute on a file field in a nested form.
The models are as follows:
class Album < ActiveRecord::Base
has_many :album_images
has_many :images, :through => :album_images
accepts_nested_attributes_for :images
end
class Image < ActiveRecord::Base
has_many :album_images
has_many :albums, :through => :album_images
mount_uploader :filename, ImageUploader
validates_presence_of :filename
end
The view:
<%= semantic_form_for #album, :url => upload_path do |f| %>
<%= f.inputs do %>
<%= f.input :name, :label => 'Album title' %>
<% end %>
<%= f.input :images, :as => :file, :input_html => {:multiple => true} %>
<%= f.buttons do %>
<%= f.commit_button 'Upload' %>
<% end %>
<% end %>
When I use for the file field:
<%= f.input :images, :as => :file, :input_html => {:multiple => true} %>
I get:
<input id="album_images" multiple="multiple" name="album[images][]" type="file">
Which doesn't doesn't seem right since I think I want to set the filename on the object directly, but I'm not sure about this. When I try to upload with this field, the incoming params look like:
"album"=>{"name"=>"2011-01-09", "images"=>["IMG_0052.JPG", "IMG_0053.JPG", "IMG_0054.JPG", "IMG_0055.JPG"]}
However, I get the following error:
ActiveRecord::AssociationTypeMismatch (Image(#2157004660) expected, got String(#2151988680)):
OK, that error is probably due to the fact that it just received a filename and not an image object. So instead, I use for the file field:
<%= f.input :images, :as => :file, :input_html => {:multiple => true, :name => 'album[images][][filename]'} %>
for which Formtastic generates:
<input id="album_images" multiple="multiple" name="album[images][][filename]" type="file">
The incoming params look like:
"album"=>{"name"=>"2011-01-09", "images"=>[{"filename"=>"IMG_0052.JPG"}, {"filename"=>"IMG_0053.JPG"}, {"filename"=>"IMG_0055.JPG"}]}
But then I get this error:
Image(#2153868680) expected, got ActiveSupport::HashWithIndifferentAccess(#2158892780)
So how does one go about setting up this multiple file input filed mapping in Rails?
Thanks.
You need to include :html => { :multipart => true } in your form_for (or in your case semantic_form_for) call so that your <form> tag is set to support file uploads.
Then revert back to your original syntax for f.input and you should be right then.