Rails: how to get fields of rejected submit form? - ruby-on-rails

I got one big form with many fields. There are multiple select_tag fields:
-(0..2).to_a.each do |id|
= select_tag('product[category_ids][]', options_from_collection_for_select(Category.all, :id, :name), :prompt=> '-- Select a Category --')
Problem: If the form is rejected upon submit, the :new action does not remember what is the previous selected values of select_tags.
Every other field reappears after submit (like :title, :description), but categories_id are lost.
Probable solution: We must add default selection to the select_tag in the options_from_collection_for_select method. How to get that category_id value? How can we access the fields of the previous form?
options_from_collection_for_select(Category.ordered, :id, :name, category_id)

You could store this data in the session and load it in the new action if it exists and clear that from the session.
That way anytime the fields reject the one that is not remembered loads into the session and is loaded into the select on the new action. Otherwise nothing is loaded when this session variable is blank.
Hope this helps.

In my form
#product.categories = []
but
#product.category_ids = [1,42,57]
So I added one messy if statement:
-if #product.category_ids.count == 0 #create new product
-(0..2).to_a.each do |id|
= select_tag('product[category_ids][]', options_from_collection_for_select(Category.all, :id, :name), :prompt=> '-- Select a Category --')
-else #edit old product, or fixing errors to previous submit
-#product.category_ids.each do |category_id|
= select_tag('product[category_ids][]', options_from_collection_for_select(Category.all, :id, :name, category_id), :prompt=> '-- Select a Category --')

Related

Populate two database values from one dropdown select

I have a dropdown menu:
<%= f.collection_select :price, Print.all, :printprice, :sizetotal, {prompt: "Pick A Size & Medium"} %>
It currently populates the :price database column with the value :printprice, and shows the value :sizetotal in the actual dropdown selection.
I want to be able to populate two database columns, one of which is the :price column which is already working, but I also want to populate another column called :size with the value :printsize. I want to do this using the same dropdown select menu.
Something like:
<%= f.collection_select :price, :size, Print.all, :printprice, :printsize, :sizetotal, {prompt: "Pick A Size & Medium"} %>
But obviously the above doesnt work
Is this possible?
Following my comment, here is the ID alternative. Basically you want to send only the ID of the print, and find again the whole object on the backend during the #create or #update actions
print_for_size_and_price = Print.find(params[:print_id_for_size_and_price])
So on the frontend I'd use something like this
select_tag(
:print_id_for_size_and_price,
options_from_collection_for_select(Print.all, :id, :sizetotal)
)
And in the backend I'd do somewhere
my_instance.price = print_for_size_and_price.printprice
my_instance.size = print_for_size_and_price.sizetotal

options_from_collection_for_select in rails 4

I'm building a simple form to let users of my rails 4 site message each other.
In the form, the user can select the recipient of the message with a select list of all the users in the system (it's a small user base).
Right now the form control looks like this, and works fine:
<%= f.select(:to_id, options_from_collection_for_select(User.all, :id, :first_name), {}, {:class => 'form-control'}) %>
However, there's a "nickname" field in the Users table as well-- I'd like to have the form show the nickname (if it exists) as well as the user's first / last name-- so the value of the select would be:
<option>Snoop Dogg - Calvin Broadus</option>
rather than just
<option>Calvin</option>
Is this possible?
Make a display_name on the Users model
def display_name
nickname || first_name
end
Then update your form to reference it
<%= f.select(:to_id, options_from_collection_for_select(User.all, :id, :display_name), {}, {:class => 'form-control'}) %>

Have a select box filled with names

In my view I have a form with a text_field_tag that takes an int, and returns only the timesheets whose timesheet.user_id match that int
<%= text_field_tag("user_id", params[:user_id]) %>
And then I have a bunch of users, these users have a name user.name and an id user.id.
I have a method in the controller called pending_approvals that when given the user id, narrows my list down to a specific users objects.
if params[:user_id].present?
#time_sheets = #time_sheets.joins(:user).where("users.id IN (?)", params[:user_id])
end
So what I am having an issue with, is I am not sure how to populate my select box with a list of users names. And then when submitted I need it to give the users id to that method. What is the best way of doing this?
If I understand correctly is you want a collection dropdown list to detail all users and thier id. You can do:
<%= collection_select :user, :user_id, #users, :name, :id, :prompt => true %>
Where :text_method is method which called on #users members will return text that you'd like to appear in dropdown.
The ApiDocs breaks it down, and this is how it is set up:
object = User
method = user_id
collection #users
value_method = id
text_method = name
option = prompt => true

Form_for select field, concatenating data into the display string

I am trying to use a form_for collection_select to display some select field options of account types.
Its occurred to me that it would be easier for the user if they could see the price of the type in each select option
this is my currently not-working code:
<%= a.collection_select :account_type, AccountType.all, :id, (:name+" - "+number_to_currency(:price)) %>
how can i concatenate the values so that (:name+" - "+number_to_currency(:price)) will actually work and not throw an error?
See the documentation:
http://api.rubyonrails.org/classes/ActionView/Helpers/FormOptionsHelper.html#method-i-collection_select
You can use the :text_method option to set the displayed text in the select dropdown.
In your AccountType model, define a method like this:
def name_with_price
"#{name} - $#{price}"
end
Then, in your view, you can use:
<%= a.collection_select :account_type, nil, AccountType.all, :id, :name_with_price %>

Rails Form_For Select With Dual Purpose

I have form for adding a new job. On my form I have a select drop-down list. I need to associate the new job to a customer. The following works great.
<%= f.collection_select :customer_id, Customer.all, :id, :business_name %>
But, what if I want to also be able to send in a customer_id to the new form? Can I have the form's select drop-down show all the possible customers, as above, but have it auto select the customer_id I pass into the form, if a customer_id is passed in?
url = ...jobs/new
OR
url = ...jobs/new?customer_id=5
I apologize if I did not explain this well enough.
Thanks in advance.
--jc
I think you do what you're trying to achieve this by populating the customer_id field on the job you're creating in your controller if customer_id is present in the request params. This should make that particular customer be the initially selected option in the form.
http://api.rubyonrails.org/classes/ActionView/Helpers/FormOptionsHelper.html#method-i-collection_select
e.g. Something like.
if params[:customer_id].present?
job.customer_id = params[:customer_id]
end
If you declaring the instance variable #customer in your controller action then you can use selected option as:
<%= f.collection_select :customer_id, Customer.all, :id, :business_name, {:selected => #customer.id} %>

Resources