collection_select issue ruby on rails - ruby-on-rails

i'm new with ruby on rails and i've an issue.
In my app, an user can publish an idea and ideas belongs to activities.
So user can choose with a select which activity they want.
Here's my code for my form, i used collection_select
<div class="field">
<%= collection_select(:idee, :id, Activite.all, :id, :nom, prompt: true) %></div>
The select input work, i've all my activities in the select input but when i select activity and publish my idea, the idea don't take the value and stay empty.
Even when i edit idea, the idea don't take the value of the activity.
How can i resolve this ?

Given that you have an Idea and an Activity model and that you are building a form for an idea, I think you should write the collection_select as:
<%= collection_select(:idea, :activity_id, Activity.all, :id, :nom, prompt: true) %>
I suggest you to use form_for syntax if you are not already doing it.
check apidock for reference:
http://apidock.com/rails/ActionView/Helpers/FormOptionsHelper/collection_select
http://apidock.com/rails/ActionView/Helpers/FormHelper/form_for

Related

Multiple value form tag for ruby

Here is the problem I'm having, and I have tried thinking around, but still can't figure out the solution. So I have two models
User
Data
and Experience belongs to user, and accepts nested attributes
Now here comes the problem ! I have page/form where I would like to update or insert.
so in the Data model
flavor, body
So How do I add form tag where I can specify my flavor but let user decide the body so for example, currently I have
<%= f.text_field :body, placeholder: "...." %>
So how do I do something like (wrong syntax)
<%= f.text_field :body, :flavor => "someflav" , placeholder: "...." %>
<%= f.text_field :body, :flavor => "Otherflav" , placeholder: "...." %>
and so on...
How does one achieve this ? I have looked around rails api, and but couldn't figure out how to achieve my issue.
Thanks for your consideration and time.
You need to use fields_for
Rails constructs input names that help it determine exactly what attribute goes where.
For instance:
user[datas_attributes][0][body]
Since (if I am interpreting you correctly) User has many Datas, it would look something like this:
<%= fields_for :datas do |data_fields| %>
<%= data_fields.text_field :body %>
<% end %>
There are a few things you need to do to make this work.
In your model, you need to add the following two lines:
accepts_nested_attributes_for :datas
attr_accessible :datas_attributes

Rails + Ransack - Drop Down List Collection?

I am loving the ransack gem for its flexibility, however I am unable to get the standard collection_select to function properly. Perhaps someone can assist.
Example:
<%= collection_select(:expense, :project_id, Project.all,
:id, :name, { prompt: 'Select Project'}, { class: 'span4' }) %>
in this case, this code is from an expense entry screen, so the first parameter is the expense object. What should it be in the ransack form? Also, I know I need to get the suffix in there. In this example, I would like project_id_eq to be the search pattern.
Also, my form is on a controller and view called "reports", I am not putting this search in the default controllers.
Thanks!
Seems that this will work.
<%= f.select :project_id_eq, options_from_collection_for_select(Project.all, "id", "name", #search.project_id_eq) %>
If anyone has another suggestion, would love to know it too.
To do this with an include_blank, put it outside of the parentheses:
ex:
<%= f.select :languages_id_eq, options_from_collection_for_select(Language.all, "id", "name"), include_blank: true %>
== UPDATE ==
better yet, use f.collection_select. This way after the user searches for something using the drop down, that option is preselected on the following page:
<%= form.collection_select :vendor_id_eq, Vendor.all, :id, :name, include_blank: true %>

Posting to database from dropdown menu Rails

I have created a dropdown menu that is pulling Cities and Countries from two database tables (named citie and country).
I am using the following collection_select tag:
<section class="field">
<%= f.label :city %>
<%= f.collection_select(:id, Citie.all, :id, :city) %>
</section>
But when I submit my form nothing is being posted into my jobs table (the form is to generate a new job).
I have searched to find a solution for this and am sure I am just missing a small part but can't seem to figure out what it is and why it's not working.
Any advice and a solution would be much appreciated! Thanks
I'm not terribly familliar with collection_select but shouldn't you give the association name as the first argument? e.g.
<%= f.collection_select(:city, Citie.all, :id, :city) %>

Ruby on Rails form select

Hey guys I'm a rails newbie and I have a question about select boxes in forms. Here is my code right now.
<%= form_for #message do |f| %>
<%= f.text_field :name %>
<%= f.select :topic ['Test1', 'test2']
<% end %>
Now, this code works for submitting the information in the text boxes, but for some reason not the information in the select box. However my main question is, Is there a way that I can make a select box and rails automatically include: "Please select an option" and that way when I do validates_presence_of :topic on the select box it will return false if it hasn't been changed?
Also, do any of you think you know why the select box isn't submitting info to my database?
Thanks in advance!
Yes, what you want is easy to do. Just add a pair of values, your placeholder text and a blank value, to the beginning of your select options. Try this:
<%= f.select :topic, [['Please select an option', nil], 'Test1', 'test2'] %>

Rails 3 - collection_select - Understanding PROMPT?

I'm building a form to allow a user to CRUD a project permission.
....
<% roles = Role.all %>
<%= f.collection_select :role_id, roles, :id, :name, :prompt => true %>
Problems with the above, while it renders:
If a value matches, it shows that in the dropdown as selected, which is good. Problem, is if a user is set as ADMIN. It's easy to use the dropdown to change the permission to something else, but not to CLEAR the permission...
Example... Select Drop Down:
- Please Select
- Admin
- Member
- Guest
If Admin is selected, Please Select never shows up.... How can I make an option show up to allow the user to remove the setting?
Any ideas? thx
I believe you want:
<%= f.collection_select(:role_id, roles, :id, :name, {:include_blank => 'Please Select'} %>
See the FormOptionsHelper docs for more information
<% roles = Role.all %>
<%= f.collection_select :role_id, roles, :id, :name, :prompt => (#user.admin? ? true : false) %>
does that help you?
you must be having a way to check if a user is admin / not.. use that condition in ternary operation to set the value of :prompt..
lemme know how it goes :)

Resources