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 %>
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 am using a search box using the following form:
<div id='search-box'>
<%= form_tag "/search", :method => :get, :id => "search-form" do %>
<%= text_field_tag :q, params[:q], {:id => "search-text", :placeholder => "SEARCH FOR PRODUCTS"} %>
<%= submit_tag "Go" %>
<%end%>
</div>
The problem is that whenever I type something in the search box, it shows a dropdown with previous queries. I do not want to store the queries and show the dropdown. How can I purge the queries?
use autocomplete="off":
<%= text_field_tag :q, params[:q], {:id => "search-text", :placeholder => "SEARCH FOR PRODUCTS", :autocomplete => 'off'} %>
I am using link_to with some HTML elements and this is what I have :
<% link_to "", { :controller => "posts" }, :id => "posts", :class => "read-more" %>
But I want it that it will link to the posts with the id that each post has, any help would be most appreciated.
Thank You
config/routes.rb
resources :posts
<% #posts.each do |post| %>
<%= link_to "View post", post_path(post), :id => "posts", :class => "read-more" %>
<% end %>
Your hash is missing a few params...
<% link_to "", { :controller => "posts", :action => "show", :id => post.id}, :id => "posts", :class => "read-more" %>
But I recommend
<% link_to "", post_path(post), :id => "posts", :class => "read-more" %>
I think what you are asking is this. You have an array or active record relation #posts which you want to show in a webpage with links to each post. If I'm right you can do
<% #posts.each do |post| %>
<%= link_to "", { :controller => "posts" }, :id => post.id, :class => "read-more" %>br>
<% end %>
But you have to specify the action which will be triggered when the link is clicked.
I think this should work
<% #posts.each do |post| %>
<%= link_to "", { :controller => "posts" , :action => :show}, :id => post.id, :class => "read-more" %>br>
<% end %>
How about this?
<% #posts.each do |post| %>
<%= link_to "POST", post_path(post), :id => post.id %>
<% end %>
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' %>
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.