How would I create a drop down list which displays the employees name as well as badge number?
At the moment I have:
<%= f.collection_select(:employee_id, Employee.all, :id, :name) %>
And I want to do something like:
<%= f.collection_select(:employee_id, Employee.all, :id, :badge_number + :name) %>
In your Employee model put a method:
def badge_name
"#{badge_number} - #{name}"
end
And use it in your view:
<%= f.collection_select(:employee_id, Employee.all, :id, :badge_name) %>
Related
I'm working on some app and I have 2 models. Categories in which I create category names and Question. Category has_many questions and Question belongs_to Category.
I've added category_id to Question model.
Now I need to take all Category_names and display them in form where I create Question so User can choose in which category_name will save question.
I've tried something like this in firs line of code but not working.
<%= f.input :category_id, Category.all.map(&:name) %>
<%= f.input :question_name, wrapper: :vertical_text_input, as: :text %>
<%= link_to "Markdown help", "http://assemble.io/docs/Cheatsheet-Markdown.html", target: "_blank", class: "right" %>
<%= f.input :answer %>
<%= f.input :image, as: :attachinary %>
QUESTION: How to display all Category names in form where I create new Questions?
you may wanna use a select box and use the collection select rails helper :
f.collection_select(:category_id, Category.all, :id, :name)
Also you can use this way.
<%= f.select :category_id, Category.all.map(&:name), {prompt:"Choose Category"}%>
#Jhon suggestion.
<%= f.select :category_id, Category.pluck(:name), {prompt:"Choose Category"}%>
I'm trying to make a collection select which shows two attributes from two different models.
I want to select an account. The account has a name and an owner. The owner is a model which also has the attribute name.
When using the collection select I want it to show: account.name + owner.name. This is currently the collection_select I have which only shows the account.name
<div class="field">
<%= f.label :to_account_id %>
<%= f.collection_select :to_account_id, Account.all, :id, :name %>
</div>
ex: A account has name Main account and the owner of the account is Stan, when selecting it should show Stan - Main account.
worked with:
<%= f.collection_select :to_account_id, Account.all.map{|a| ["#{a.owner.name} - #{a.name}", a.id] },:second,:first %>
Try following code
<%= f.collection_select :to_account_id, Account.all.map{|a| ["#{a.name} - #{a.owner.name}", a.id] } %>
class Classification < ApplicationRecord
def class_detail
"#{definition} #{description}"
end
end
<%= form.collection_select(:classification_id, Classification.order(:classification), :id, :class_detail, prompt: true)%>
In my rails project I have two models, Car Make & Car Model, with a 1:M relationship (i.e. one Audi has many Audi models).
In my Views page, I want a form with two input fields for car make & car model. Ideally, I will be able to input a car make (i.e. Audi) and the second input field will have a drop down menu with all the models available for the make (2016 Audi A6, 2017 Audi A7).
I've set up all the relations and in the models I have saved a foreign key of the make.
currently in _form.html.erb I have
<div class="field">
<%= f.label :make_id, "Make:"%><br>
<%#= f.number_field :make_id %>
<%= f.collection_select :make_id, Make.all,
:id,:makes_info, {:include_blank => 'Please Select'} %>
</div>
<div class="field">
<%= f.label :model_id, "Model:" %><br>
<%= f.collection_select :model_id, Model.all,
:id,:model_info, {:include_blank => 'Please Select'} %>
</div>
If you want it to truly be dynamic, you would need to use an AJAX request to update the second select after the first is picked. You'd also need to use the options_for_select method inside of the select tag
Some more info to accompany what was already provided.
It's known as dynamic select boxes:
#config/routes.rb
resources :makes do
get :models, on: :collection #-> url.com/makes/models
end
#app/controllers/makes_controller.rb
class MakesController < ApplicationController
def models
#make = Make.find(params[:make][:make_id])
respond_to do |format|
format.js
end
end
end
#app/views/makes/models.js.erb
$select = $("select#models");
$select.empty();
<% #make.models.each do |model| %>
$select.append($('<option>').text(<%=j model.name %>).attr('value', <%= model.id %>));
<% end %>
#views
<%= f.collection_select :make_id, Make.all, :id, :makes_info, {include_blank: 'Please Select'}, { data: { remote: true, url: make_models_path }} %>
<%= f.collection_select :model_id, Model.all, :id,:model_info, {include_blank: 'Please Select'}, { id: "models" } %>
I have a model like:
class User < ActiveRecord::Base
enum :status [:banned, :registered, :trial, :pending]
end
On my edit page, I want to show a list of 4 radio buttons and pre-select the radio button that is currently set for a user.
How can I do this?
<%= form_for #user do |f| %>
<%= f.collection_radio_buttons :status, User.statuses, :first, :first %>
<%= f.submit %>
<% end %>
Ref
Rails creates a class method using the pluralized attribute name when you use enum. The method returns a key value pair of strings you've defined and what integers they map to. So, you could do something like this:
<% User.status.keys.each do |status| %>
<%= f.radio_button :status, status %>
<%= f.label status.to_sym %>
<% end %>
I'm trying to use a collection select and from there go to my user_show page, but I can't seem to figure out how to send the selected variable through to be displayed.
Here is my current code:
<%= form_tag user_path(:id), :method => :post do %>
<%= collection_select(:user, :id, User.all, :id, :name) %>
<button type="submit">Sign In</button>
<% end %>
This is the closest I've gotten, but it is reading the :id as id. What is the correct way to do this?
Your collection_select should be like this (with explanation of each field):
collection_select(
:user, # field namespace
:user_id, # field name
# result of these two params will be: <select name="user[user_id]">
# then you should specify some collection or array of rows.
# In your example it is:
User.all,
# then you should specify methods for generating options
:id, # this is name of method that will be called for every row, result will be set as key
:name, # this is name of method that will be called for every row, result will be set as value
# as a result, every option will be generated by the following rule:
# 'user' is an element in the collection or array
)
So, change your collection_select to this:
<%= collection_select(:user, :user_id, User.all, :id, :name) %>
You probably need:
<%= form_tag user_path(:id), :method => :post do %>
<%= collection_select(:user, :user_id, User.all, :id, :name) %>
<button type="submit">Sign In</button>
<% end %>