I am working on a script that allows for an admin to assign multiple languages to a user.
I have my multi select working like so:
<%= fields_for :users_languages do |u| %>
<div class="field">
<%= #lang_list.inspect %>
<%= u.label :Assign_Languages %><br />
<%= select_tag :language_id, options_for_select(Language.all.collect {|lang| [lang.english, lang.id]}),:multiple => true, :prompt => 'Select Language' %>
</div>
<% end %>
But when I go in to edit the user their languages are not showing up as auto selected. How would I go about doing this?
Figured it out by doing the following:
Controller:
def edit
#user = User.find(params[:id])
#users_langs = UsersLanguage.where("user_id = ?", params[:id])
#lang_list = []
#users_langs.each do |langs|
#lang_list << langs.language_id
end
end
I created an array variable called #lang_list, which I then used in my view to tell my multi-select which fields to auto highlight.
View:
<%= fields_for :users_languages do |u| %>
<div class="field">
<%= u.label :Assign_Languages %><br />
<%= select_tag :language_id, options_for_select(Language.all.collect {|lang| [lang.english, lang.id]}, #lang_list),:multiple => true, :prompt => 'Select Language' %>
</div>
<% end %>
Hope this helps someone!
Related
I'm trying to make a select input on all my users in ruby on rails, that almost works but the thing is that the don't get the username of my user but i render somethings like this : #<User:0x007f8cc1024d60>
I created 3 users in my console, I can see them while i'm on rails console
I got this in my controller :
def new
#bug = Bug.new
#users = User.all
end
Also my params
def bug_params
params.require(:bug).permit(:owner, :title, :description)
end
And in my html :
<%= form_with model: #bug do |form| %>
<%= form.select :owner, #users %>
<%= form.text_field :title, placeholder: "title" %>
<%= form.text_area :description, placeholder: "description" %>
<%= form.submit %>
<% end %>
Could someone explain me what's wrong ?
With your select form helper, you return only a collection of User instances.
As specified in the documentation, you need to collect what attribute of User you want to pass as option value and option display.
For example:
<%= form.select :owner, #users.collect {|u| [ u.name, u.id ] } %>
#=> output
# <select name="bug[owner]">
# <option value="1">John</option>
# <option value="2">Joe</option>
# </select>
Use collection_select to create a select from a collection of records.
<%= form_with model: #bug do |form| %>
<%= form.collection_select(:owner, #users, :id, :name) %>
<%= form.text_field :title, placeholder: "title" %>
<%= form.text_area :description, placeholder: "description" %>
<%= form.submit %>
<% end %>
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, [])
Am trying to create a profile page where the user can check "male" or "female". After saving the form, whenever the user visits the page again, the gender must be set by default based on the data stored in the database.
Am using the form_for helper here.
<%= form_for #profile do |f| %>
<%= f.label :gender, "Male", :value => "m" do %>
<%= f.radio_button :gender, "m" %>
<% end %>
<%= f.label :gender, "Female", :value => "f" do %>
<%= f.radio_button :gender, "f" %>
<% end %>
<% end %>
I this you should use like this way:
<% form_for( #profile) do |f| %>
<%= f.radio_button :gender, "Male", :checked => #profile.is_male? %>
<%= f.radio_button :gender, "Female",:checked => #profile.is_female? %>
<% end %>
In your models/profile.rb:
class Profile
def is_male?
#write your code for check male
end
def is_female?
#write your code for check female
end
end
okay, if you have a habtm relationship between category and articles, now through the category index page is it possible to perform a filter, using two select box populated with the categories, which shows articles belonging to either or both category?
this is how i tried.
category controller
def index
#categories = Category.all
end
def show
#category = Category.find(params[:name][:id])
end
category index page
<%= form_for :categories, :url => {:action => :show}, :method => "get" do |f| %>
<div class="field">
<%= f.label :category %><br />
<%= collection_select(:name, :id, Category.all, :id, :name) %>
</div>
<div class="field">
<%= f.label :category %><br />
<%= collection_select(:name, :id, Category.all, :id, :name) %>
</div>
<div class="actions">
<%= f.submit "Submit" %>
</div>
<% end %>
category show page
<ul>
<% #category.articles.each do |a| %>
<li><%= a.author %></li>
<li><%= a.title%></li>
<li><%= a.pub_date%></li>
<% end %>
</ul>
at this stage it only presents articles, based on the second collection_select.
also, is habtm altogether necessary to do something like this? can this be done if there is a column instead in the articles model? which is the best way?
thanks
I'd suggest to rename the second collection_select ids and try a Where clause with OR in your controller. Something like that:
def index
#categories = Category.all
end
def show
#category = Category.where(:id => [params[:name][:id1], params[:name][:id2]])
end
and in your index page:
<%= form_for :categories, :url => {:action => :show}, :method => "get" do |f| %>
<div class="field">
<%= f.label :category %><br />
<%= collection_select(:name, :id, Category.all, :id1, :name) %>
</div>
<div class="field">
<%= f.label :category %><br />
<%= collection_select(:name, :id, Category.all, :id2, :name) %>
</div>
<div class="actions">
<%= f.submit "Submit" %>
</div>
<% end %>
I did not test it, but this is the idea.
Hope it helps
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' }) %>