I've been reading this question (Ruby on Rails: Submitting an array in a form) but it didn't answer my question.
I have this form:
<div class="signup pull-right">
<div>
<p style="color: white">Create Doctor:</p>
<%= form_for(#doctor) do |d| %>
<%= d.text_field :name[1], :placeholder => "name" %>
<%= d.text_field :name[2], :placeholder => "surname" %>
<%= d.text_field :email, :placeholder => "email" %>
<%= d.text_field :password, :placeholder => "password" %>
<%= d.text_field :password_confirmation, :placeholder => "password confirmation" %>
<%= d.submit "Sumbit", class: "btn btn-small btn-primary pull-right", id: "boton" %>
<% end %>
</div>
where name is an array. What I want is to catch name and surname separately and join them inside name[]. I couldn't found the way to do it easily. Do you have any suggestion?
Thanks in advance.
You don't need such hack to solve the problem. Setting the attributes by virtual cattr_accessor would be simple and conventional.
class Doctor < ActiveRecord::Base
cattr_accessor :first_name, :surname
# others...
end
Then in form
<%= form_for(#doctor) do |d| %>
<%= d.text_field :first_name, :placeholder => "first name" %>
<%= d.text_field :surname, :placeholder => "surname" %>
Then in controller
def create
attr = params[:doctor]
attr[:name] = "#{attr.delete!(:first_name)} #{attr.delete!(:surname)}"
#doctor = Doctor.new(attr)
if #doctor.save
# blah blah
end
I would check till I have this html generated:
<input name='doctor[name][]'>
I would try:
<%= text_field_tag 'doctor[name][]' %>
Related
In my RoR application I have a form whereby users can search for their contacts by first name and select the ones that they want to send an email to. My problem is that after searching for say "Tom", and selecting the checkbox for "Tom", if they then reset the search to display all check boxes "Tom" is no longer selected.
Can someone please tell me how this can be sorted?
The code in the view is:
<div class="panel-body">
<%= form_tag '/emails/contact_search', :method => 'get' do %>
<p>
<%= text_field_tag :search_string, params[:search_string], :placeholder => "Search by firstname" %>
<%= submit_tag "Search" %>
</p>
<% end %>
<%= form_for(#email) do |f| %>
<fieldset>
<div class="form-group">
<label>From Email Address</label></br>
<% #useraccounts.each do |useraccount| %>
<%= f.radio_button :account_id, useraccount.account_id, :checked => false %>
<%= f.label :account_id, useraccount.account.email, :value => "true" %><br>
<% end %>
</div>
<div class="form-group">
<label>Contacts</label></br>
<%= f.collection_check_boxes :contact_ids, #contacts, :id, :fullname, checked: #selected_contact_ids %>
</div>
<div class="form-group">
<label>Groups</label></br>
<%= f.collection_check_boxes :group_ids, Group.where(user_id: session[:user_id]), :id, :name ,{ prompt: "name" } %>
</div>
<div class="form-group">
<label>Attachment</label>
<%= f.file_field :attachment, :size => 42 %><br>
</div>
<div class="actions">
<%= f.submit "Continue", {:class => 'btn btn-primary '} %>
<%= render "/error_messages", :message_header => "Cannot save: ", :target => #email %>
</div></br>
</fieldset>
<% end %>
</div>
The controller code for the search is:
def contact_search
#email = Email.new(session[:email_params])
#email.current_step == session[:email_step]
#useraccounts = Useraccount.where(user_id: session[:user_id])
#contacts = Contact.contact_search(params[:search_string])
if #contacts.empty?
flash[:notice] = "There are no emails with that subject"
#contacts = Contact.all
end
render :action => "new"
end
private
def email_params
params.require(:email).permit(:subject, :message, :account_id, { contact_ids: [] }, { group_ids: [] }, :attachment)
end
And in the model:
def self.contact_search(search_string)
self.where("firstname LIKE ?", search_string)
end
I cannot work out how to keep a checkbox selected after a user then searches for something else or resets the search, can someone please shed some light on how this can be achieved?
This seems to be one of those obscure and badly documented features of Rails. What has to be done is pass an array if ids to be checked into the checked parameter
= f.collection_check_boxes :contact_ids, #contacts, :id, :fullname, checked: #selected_contact_ids
and then check boxes with items of id 1, 2 and 3 will be checked. You will need to pass this array obviously from the controller somehow. E.g.
#selected_contact_ids = email_params.fetch(:contact_ids, [])
I'm making a Rails app and am trying to have the user's role displayed on a view. The code I am using for this is:
<%= current_user.role %>
Which does not evaluate to anything.
I am using devise for user registrations and my registration view is as follows:
<%= form_for(resource, as: resource_name, url: registration_path(resource_name)) do |f| %>
<%= devise_error_messages! %>
<div class="login-field">
<%= f.email_field :email, :placeholder => "Email" %>
</div>
<div class="login-field">
<%= f.password_field :password, autocomplete: "off", :placeholder => "Password (8 character min)" %>
</div>
<div class="login-field">
<%= f.text_field :firstname, :placeholder => "First Name" %>
</div>
<div class="login-field">
<%= f.text_field :lastname, :placeholder => "Last Name" %>
</div>
<div class="login-field">
<%= f.text_field :phone, :placeholder => "Phone Number" %>
</div>
<%= f.select(:role, ['Role 1', 'Role 2', 'Both']) %>
<br>
<div><%= f.submit "Signup" , :class => "btn btn-success btn-lg" %></div>
<% end %>
<%= render "devise/shared/links" %>
I have a feeling that I am doing something wrong in the form submission but cannot figure out what it is. Any help is appreciated
You can put this at the top of your create action:
return render text: params.inspect # get what the form sended
and or
return render text: create_params # get what the values you permitted
This way you will get what the data your from sended and if it is what you expected.
You should override the sign_up_params in the Devise registration. Once you've created your RegistrationsController that inherits from Devise::RegistrationsController, you can override the sign_up_params:
class RegistrationsController < Devise::RegistrationsController
private
def sign_up_params
params.require(:user).permit(:first_name, :last_name, :email, :phone, :role ... any other attributes)
end
end
You would also need to change you routes.rb to point to the newly created controller:
devise_for :users, controllers: {registrations: 'registrations'}
I have a form_for select where the options are being defined from within the model. I am trying to get it to display a placeholder option but cannot figure out how to.
The Model:
class Factoid < ActiveRecord::Base
attr_accessible :description, :name, :title
validates_presence_of :description, :name, :title
validates_uniqueness_of :title
NAMES = "Angela", "Geordie", "Jared", "Jennifer", "Kevin", "Matthew", "Oscar", "Owen", "Regina", "Todd", "Vaibhavi", "Zack"
UNRANSACKABLE_ATTRIBUTES = ["id", "updated_at"]
def self.ransackable_attributes auth_object = nil
(column_names - UNRANSACKABLE_ATTRIBUTES) + _ransackers.keys
end
end
The Form:
<%= form_for #factoid, :html => { :class => 'form-horizontal' } do |f| %>
<div class="control-group">
<%= f.label :title, :class => 'control-label' %>
<div class="controls">
<%= f.text_field :title, :class => 'text_field' %>
</div>
</div>
<div class="control-group">
<%= f.label :description, :class => 'control-label' %>
<div class="controls">
<%= f.text_area :description, :class => 'text_area' %>
</div>
</div>
<div class="control-group">
<%= f.label :name, :class => 'control-label' %>
<div class="controls">
<%= f.select :name, :collection => Factoid::NAMES %>
</div>
</div>
<div class="form-actions">
<%= f.submit nil, :class => 'btn btn-primary' %>
<%= link_to t('.cancel', :default => t("helpers.links.cancel")),
factoids_path, :class => 'btn' %>
</div>
<% end %>
The second issue is that the dropdown menu is displaying the word "collection" at the top of it (See screenshot below). How do I get read of that. Ideally I want to have a dropdown menu with a placeholder of "Names" that is also displayed at the top when the dropdown menu is opened.
For your text field try something like:
<%= f.text_field :title, :class => 'text_field', value: 'my_default_value' %>
for your select try:
<%= f.select :name, Factoid::NAMES %>
See the docs for select and the rails guide for typical usage (I think the method I've shown you will not work upon submitting the form, see the guides linked for explanation, I'm not sure though).
Hello i have a form like
<%= form_for #user,
:url => url_for(:controller => 'frontend',
:action => 'registration_completion') do |f| %>
<div class="control-group">
<%= f.label :name, "Jméno", :class => 'control-label' %>
<div class="controls">
<%= f.text_field :name, :class => 'text_field' %>
</div>
</div>
And user is displaying the form where he can see already registered values, but some of that value i want to hide if some condition (specifically i need hide value of username if it contains #) It is possible? thank you
solution: it seems that
<% if #user.name.include?('#') %>
<%= f.text_field :name %>
<% else %>
<%= f.text_field :name, :value => "" %>
<% end %>
works
I strongly suggest to put your logic inside a helper method:
module UserHelper
def name_input(instance, f, options = {})
options[:html_options] ||= {}
f.text_field :name, options[:html_options] if instance.name.include?('#')
end
end
Then in your view:
<%= name_input(#user, f, html_options: { class: 'text_field' }) %>
If you want to simply hide the value you can do:
<%= f.text_field :name, value: #user.name.include?('#') ? #user.name : '' %>
Feel free to move this to a helper method also.
To create a hidden field when the username contains an # symbol you could use the following helper method
module UserHelper
def hidden_name_field(user, f, options = {})
options[:html_options] ||= {}
if user.name.include?('#')
f.hidden_field(:name, options[:html_options])
else
f.text_field(:name, options[:html_options])
end
end
end
and use it in you form:
<%= hidden_name_field(#user, f, html_options: { class: 'text_field' }) %>
first off I'm very new to rails - I'm playing about with a little log in application had it all working and decided to try out simple form - however I can't get my log in form to work with the gem.
Here is what I had and had working;
<h2>Log In</h2>
<%= form_tag sessions_path do %>
<div class="field">
<%= label_tag :email %>
<%= text_field_tag :email, params[:email] %>
</div>
<div class="field">
<%= label_tag :password %>
<%= password_field_tag :password %>
</div>
<p><%= link_to "Forgotten Password?", new_password_reset_path %></p>
<div class="field">
<%= check_box_tag :remember_me, 1, params[:remember_me] %>
<%= label_tag :remember_me %>
</div>
<div class="actions"><%= submit_tag "Log In" %></div>
<% end %>
And here is what I tried to change it to using simple form.
<h2>Log In</h2>
<%= simple_form_for :sessions, :url => sessions_path, :html => { :class => 'form-vertical' } do |f| %>
<%= f.input :email, :required =>false, :label => 'Email Address',:placeholder => 'Email Address' %>
<%= f.input :password, :required =>false, :label => 'Password',:placeholder => 'Password' %>
<label class="checkbox">
<%= check_box_tag :remember_me, 1, params[:remember_me] %>
Remember me
</label>
<p>
<%= link_to "Forgotten Password?", new_password_reset_path %>
</p>
<%= f.button :submit "Login" %>
<% end %>
This seems to work okay until I try to log in - when I log in it is always displaying my invalid username and password message - I can't figure out where I'm going wrong here. Any help would be much appreciated!
Thanks!
In case 1, you are probably receiving params: { :email => '...', ....} and in case 2, :sessions => { :email => '...', ....}
Check params.inspect
Got it! Many thanks to Zabba for pointing me in the right direction;
My second method works;
<%= simple_form_for :sessions, :url => sessions_path, :html => { :class => 'form-vertical' } do |f| %>
<%= f.input :email, :required =>false, :label => 'Email Address',:placeholder => 'Email Address' %>
<%= f.input :password, :required =>false, :label => 'Password',:placeholder => 'Password' %>
<label class="checkbox">
<%= check_box_tag :remember_me, 1, params[:remember_me] %>
Remember me
</label>
<p>
<%= link_to "Forgotten Password?", new_password_reset_path %>
</p>
<%= f.button :submit "Login" %>
<% end %>
However I failed to update my controller so where I had;
def create
user = User.find_by_email(params[:email])
if user && user.authenticate(params[:password])
...
end
I had to update to;
def create
user = User.find_by_email(params[:sessions][:email])
if user && user.authenticate(params[:sessions][:password])
...
end
Thanks Zabba!