Posting to database from dropdown menu Rails - ruby-on-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) %>

Related

collection_select issue 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

How do you allow a null date entry with ruby on rails?

I'm brand new to ROR. I'm having a pretty simple problem that I can't find the solution to anywhere. In one of my projects I have a field to enter the date. The problem is that for lots of the data that I gather the date is missing. I don't know how to make that null. My form.html looks like this.
<div class="field">
<%= f.label :date %><br />
<%= f.date_select :date %>
</div>
I imagine that just need to add something to this to allow there to be no date entered. Does anyone know how to allow null entries in a date field?
Thanks
You can use the following option to allow blanks if that's what you mean:
<%= f.date_select :date, :include_blank => true %>
You can then leave it blank, so it becomes NULL in your DB.

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

Populate Dropdown based on another dropdown in ruby on rails

I have a two different fields in my form like below.
<div class="field">
<%= f.label :Master_Survey %><br/>
<%= f.select :master_survey, Condition::MasterSurvey.all.map{|e| [e.Master_Survey_Code]}, { :prompt => 'Please Select' } %>
</div>
<div class="field">
<%= f.label :Element_Code %><br/>
<%= f.select :Element_Code, Condition::Element.all.map{|e| [e.Element, e.Element_Code]}, { :prompt => 'Please Select' } %>
</div>
I want the second field should disabled unless the first got selected. And The Second field Element code should change the value depend upon the First field Master Survey selected. I have a Master Survey Code in the Elements Table.
If these two models are related via has_many/belongs_to or something similar, the best bet is going to use the grouped_collection_select method for your drop downs. This will organize your drop down into a tabbed-list (using optgroup), but then doing some jQuery magic to make the 2 drop down selections dynamic and chained!
Ryan Bates has a RailsCast that explains in great detail exactly how to do this. If you don't have a pro-subscription to RailsCasts, I highly recommend getting one so you can easily find out how to do things like this :)
Hope this points you in the right direction!

Rails: searching filter model attribute

I need to add filter search feature in my search form.
It's something like a social network. My app has societies, and a societies have categories (fashion, press, factory, ecc). I need my search to filter those categories.
Since I saw there are a plenty of search gems (solr, thinking sphinx, elasticsearch, sunspot) and look through all of them is a bit overwhelming have anybody accomplished such a task?
Any suggestion?
Things the search form should perform:
1) Search for a Society Name (And I can handle this with Sunspot)
2) Filter all Society by category chosen by user in a select tag (something like this)
3) And if both the text field and the select are filled, search for the name only in the category chosen
A great gem that I found recently is Ransack, which Ryan Bates has a RailsCasts for.
Here is an example of how it's used.
<%= search_form_for #query do |f| %>
<!-- filter: `society_name` contains text -->
<%= f.label :society_name_cont %>
<%= f.text_field :society_name_cont %>
<!-- filter: `category_id` equals -->
<%= f.label :category_id_eq %>
<%= f.collection_select :category_id_eq, Category.all, :id, :name %>
<% end %>
If you're unfamiliar with collection_select the Rails Guides will help <http://guides.rubyonrails.org/form_helpers.html#making-select-boxes-with-ease>.

Resources