Easy way to connect tables using form in rails - ruby-on-rails

I have two tables, works and artists. Every artist has multiple works. Is it possible to list those artists in a dropdown menu when creating a new work.
This is the way I solve it right now:
<div class="field">
<%= f.label :title %><br />
<%= f.text_field :title %>
</div>
<div class="field">
<%= f.label :photo %><br />
<%= f.file_field :photo %>
</div>
<div class="field">
<%= f.label :exhibition_id %><br />
<%= f.number_field :exhibition_id %>
</div>
<div class="field">
<%= f.label :artist_id %><br />
<%= f.number_field :artist_id %>
</div>
<div class="actions">
<%= f.submit %>
</div>

http://api.rubyonrails.org/classes/ActionView/Helpers/FormOptionsHelper.html#method-i-collection_select
The example is exactly what you're looking for.
collection_select(#work, :artist_id, Artist.all, :id, :name)

Related

Rails - Combobox Collection depend on another Combobox

I am looking for a way to change the Combobox Collection (combo_two) depends on the option choose in another combobox (combo_one). Follow bellow the Rails form:
<%= form_for(#requisite) do |f| %>
<div class="field">
<%= f.label :this_type %><br>
<%= f.text_field :this_type, :value => params["type"] %>
</div>
<div class="field">
<%= f.label :this_id %><br>
<%= f.number_field :this_id, :value => params["id"] %>
</div>
<div id="combo_one" class="field">
<%= f.label :require_type %><br>
<%= f.select :require_type, ([["Element","Element"],["Research","Research"]]) %>
</div>
<div id="combo_two" class="field">
<%= f.label :require_id %><br>
<%= f.collection_select(:require_id, #elements, :id, :name, :prompt => true) %>
</div>
<div class="field">
<%= f.label :qty %><br>
<%= f.number_field :qty %>
</div>
<div class="field">
<%= f.label :lvl %><br>
<%= f.number_field :lvl %>
</div>
<div class="actions">
<%= f.submit %>
</div>
<% end %>
I hope we resolve it with javascript (jquery), but I do not know javascript (jquery)
with jquery you have posible get a value for first combo box using a html onchange event in combo box
<%= f.select :require_type, ([["Element","Element"],["Research","Research"]]),:onchange => GetValue(this) %>
or
<select class="combo_box" onchange="GetValue(this);"></select>
function GetValue(this){
var value = this.value;
//Another code for second combobox
}

Rails 3.2 is not saving dropdown selection in form after clicking edit

I have a form with input field and dropdown boxes with data from other tables. (joins)
but now when i submit it is good, but when i want to edit the fields the dropdown fields are empty but the text fields aren't
My form code:
<%= form_for #contracten, :html => { :multipart => true } do |f| %>
<% if #contracten.errors.any? %>
<div id="error_explanation">
<h2><%= pluralize(#contracten.errors.count, "error") %> prohibited this contracten from being saved:</h2>
<ul>
<% #contracten.errors.full_messages.each do |msg| %>
<li><%= msg %></li>
<% end %>
</ul>
</div>
<% end %>
<div id="forms">
<div class="field">
<%= f.label :naam %><br />
<%= f.text_field :naam %>
</div>
<br>
<div class="field">
<%= f.label :omschrijving %><br />
<%= f.text_area :omschrijving %>
</div>
<br>
<div class="field">
<%= f.label :organisatie %><br />
<%= f.select :organisatieid, options_for_select(#organisaties.map{ |f| [f.naam, f.id] }), :include_blank => true %>
</div>
<br>
<div class="field">
<%= f.label :vestiging %><br />
<%= f.select :vestigingid, options_for_select(#vestigingens.map{ |f| [f.naam, f.id] }), :include_blank => true %>
</div>
<br>
<div class="field">
<%= f.label :Beheerder %><br />
<%= f.select :persoonid, options_for_select(#personen.map{ |f| [f.naam, f.id] }), :include_blank => true %>
</div>
<br>
<div class="field">
<%= f.label :contractsoort %><br />
<%= f.select :contractsoortid, options_for_select(#contractsoorten.map{ |f| [f.naam, f.id] }), :include_blank => true %>
</div>
<br>
<div class="field">
<%= f.label :datumingang %><br />
<%= f.datetime_select :datumingang %>
</div>
<br>
<div class="field">
<%= f.label :datumeinde %><br />
<%= f.datetime_select :datumeinde %>
</div>
<br>
<div class="field">
<%= f.label :contractduur %><br />
<%= f.number_field :contractduur, :placeholder => 'in jaren' %>
</div>
<br>
<div class="field">
<%= f.label :opzegtermijn %><br />
<%= f.number_field :opzegtermijn, :placeholder => 'In Maanden' %>
</div>
<br>
<div class="field">
<%= f.label :betalingsperiode_eenheid_id %><br>
<%= f.select :betalingsperiodeeenheidid, options_for_select(#betalingsperiodeeenhedens.map{ |f| [f.omschrijving, f.id] }), :include_blank => true %>
</div>
<br>
<div class="field">
<%= f.label :betalingstermijn %><br />
<%= f.number_field :betalingstermijn %>
</div>
<br>
<div class="field">
<%= f.label :pdf %><br />
<%= f.file_field :pdf %>
</div>
<br>
<div class="actions">
<%= f.submit "Toevoegen", :class => 'button3' %>
</div>
</div>
<% end %>
<br>
screenshot:
I hope someone know's what the problem.
Add the :selected => ... to your options_for_select parameters.
E.g:
<%= f.select :contractsoortid,
options_from_collection_for_select(#contractsoorten, 'id', 'naam',
:selected => #contracten.contractsortid),
:include_blank => true %>
The changes are actually saved, but when you render the form, the actually selected value must be specified.

How to create a HTTP post request using the form fields to the external websites API using Ruby on Rails?

I am trying to create a HTTP post request using the form fields to the external website here it is factual.com
I have created the method in the my data_controller.rb which is like
def posttofactual
uri = URI.parse("http://api.factual.com/v2/tables/Nj0JN3/input?")
# Full control
http = Net::HTTP.new(uri.host, uri.port)
request = Net::HTTP::Post.new(uri.request_uri)
request.set_form_data({"name" => "datum.name", "address" => "datum.address", "locality" => "datum.locality", "region" => "datum.region", "postcode" => "datum.postcode","category" => "datum.category","website" => "datum.website","latitude" => "datum.latitude","longitude" => "datum.longitude","&APIKey=" => "myapikey" })
# Tweak headers, removing this will default to application/x-www-form-urlencoded
request["Content-Type"] = "application/json"
response = http.request(request)
end
I have a form like
<%= form_for(#datum) do |f| %>
<% if #datum.errors.any? %>
<div id="error_explanation">
<h2><%= pluralize(#datum.errors.count, "error") %> prohibited this datum from being saved:</h2>
<ul>
<% #datum.errors.full_messages.each do |msg| %>
<li><%= msg %></li>
<% end %>
</ul>
</div>
<% end %>
<div class="field">
<%= f.label :name %><br />
<%= f.text_field :name %>
</div>
<div class="field">
<%= f.label :address %><br />
<%= f.text_field :address %>
</div>
<div class="field">
<%= f.label :locality %><br />
<%= f.text_field :locality %>
</div>
<div class="field">
<%= f.label :region %><br />
<%= f.text_field :region %>
</div>
<div class="field">
<%= f.label :postcode %><br />
<%= f.number_field :postcode %>
</div>
<div class="field">
<%= f.label :category %><br />
<%= f.text_field :category %>
</div>
<div class="field">
<%= f.label :website %><br />
<%= f.text_field :website %>
</div>
<div class="field">
<%= f.label :latitude %><br />
<%= f.text_field :latitude %>
</div>
<div class="field">
<%= f.label :longitude %><br />
<%= f.text_field :longitude %>
</div>
<div class="actions">
<%= f.submit %>
</div>
<% end %>
I want to call "posttofactual" method on this line <%= f.submit %> on the form field.
It seems like your form will post to either create or update method of the controller. There you should extract data from params and call your posttofactual method.

Rails 3 Nested Forms

I have a Person model and an Address Model:
class Person < ActiveRecord::Base
has_one :address
accepts_nested_attributes_for :address
end
class Address < ActiveRecord::Base
belongs_to :person
end
In my people controller I have #person.build_address in my new action. My forms builds correctly. The problem is that when I submit the form, a person record and an address record is created but they aren't linked via the address_id column in the Person table.
Am I missing a step in the controller?
Thanks!
New Action
UPDATE
def new
#person = Person.new
#person.build_address
respond_to do |format|
format.html # new.html.erb
format.xml { render :xml => #person }
end
end
Form Code
UPDATE
<%= form_for(#person) do |f| %>
<% if #person.errors.any? %>
<div id="error_explanation">
<h2><%= pluralize(#person.errors.count, "error") %> prohibited this person from being saved:</h2>
<ul>
<% #person.errors.full_messages.each do |msg| %>
<li><%= msg %></li>
<% end %>
</ul>
</div>
<% end %>
<div class="field">
<%= f.label :first_name %><br />
<%= f.text_field :first_name %>
</div>
<div class="field">
<%= f.label :last_name %><br />
<%= f.text_field :last_name %>
</div>
<div class="field">
<%= f.label :email %><br />
<%= f.text_field :email %>
</div>
<div class="field">
<%= f.label :telephone %><br />
<%= f.text_field :telephone %>
</div>
<div class="field">
<%= f.label :mobile_phone %><br />
<%= f.text_field :mobile_phone %>
</div>
<div class="field">
<%= f.label :date_of_birth %><br />
<%= f.date_select :date_of_birth %>
</div>
<div class="field">
<%= f.label :gender %><br />
<%= f.select(:gender, Person::GENDER_TYPES) %>
</div>
<div class="field">
<%= f.label :notes %><br />
<%= f.text_area :notes %>
</div>
<div class="field">
<%= f.label :person_type %><br />
<%= f.select(:person_type, Person::PERSON_TYPES) %>
</div>
<%= f.fields_for :address do |address_fields| %>
<div class="field">
<%= address_fields.label :street_1 %><br />
<%= address_fields.text_field :street_1 %>
</div>
<div class="field">
<%= address_fields.label :street_2 %><br />
<%= address_fields.text_field :street_2 %>
</div>
<div class="field">
<%= address_fields.label :city %><br />
<%= address_fields.text_field :city %>
</div>
<div class="field">
<%= address_fields.label :state %><br />
<%= address_fields.select(:state, Address::STATES) %>
</div>
<div class="field">
<%= address_fields.label :zip_code %><br />
<%= address_fields.text_field :zip_code %>
</div>
<% end %>
<div class="actions">
<%= f.submit %>
</div>
<% end %>
You need to have accepts_nested_attributes_for :address on your Person model for this to work nicely. In your create action you can then do this:
def create
#person = Person.new(params[:person])
...
end
Then Rails will take care of the rest.
UPDATE: if the address_id column is in the people table then it should be belongs_to :address, not has_one :address
Why is your address built in your new action, and not in the create action? You're building an address from a non saved model, without an id, so the foreign key can't be set. You should keep your #person in your new action, but put your build_address in your create action, after #person has been saved.

Can't get plain old many-to-one nested form to save

I'm on Rails 3. I have a model Client that has an address_id. In my Client form, I have nested Address fields. Here's that the Address part of the form:
<%= f.fields_for #client.address do |address_form| %>
<div class="field">
<%= address_form.label :line1 %><br />
<%= address_form.text_field :line1 %>
</div>
<div class="field">
<%= address_form.label :line2 %><br />
<%= address_form.text_field :line2 %>
</div>
<div class="field">
<%= address_form.label :city %><br />
<%= address_form.text_field :city %>
</div>
<div class="field">
<%= address_form.label :state_id %><br />
<%= select("client[address]", "state_id", State.all.collect {|s| [ s.name, s.id ] }) %>
</div>
<div class="field">
<%= address_form.label :zip %><br />
<%= address_form.text_field :zip %>
</div>
<% end %>
When I try to save my form, I get this:
Address(#23652762896420) expected, got ActiveSupport::HashWithIndifferentAccess(#23652751466220)
I don't understand exactly what that means or why I'm getting it. Here's what params.inspect looks like:
{"commit"=>"Update Client",
"authenticity_token"=>"CBw1fQcsUtXs9x6lKTL4zeoekX1cwuFUrZvZpCShHIc=",
"_method"=>"put",
"utf8"=>"\342\234\223",
"action"=>"update",
"id"=>"16",
"client"=>{"name"=>"Rosie O'Donnell",
"address"=>{"city"=>"Grand Rapids",
"line1"=>"216 Grandville Ave SW",
"zip"=>"49503",
"line2"=>"",
"state_id"=>"1"},
"phone"=>"",
"salon_id"=>"1",
"email"=>""},
"controller"=>"clients"}
Can someone please tell me what's going on?
See here: http://guides.rubyonrails.org/2_3_release_notes.html#nested-object-forms (It's a Rails 2.3 article, but it should apply to Rails 3 as well)
In your Client model you need this line:
accepts_nested_attributes_for :address
Also, change the first line of your nested form to this:
<%= f.fields_for :address do |address_form| %>
Assuming you have the following in your clients class:
accepts_nested_attributes_for :address
and
attr_accessible :address_attributes
I would modify the form as follows:
<%= f.fields_for :address do |address_form| %>
<div class="field">
<%= address_form.label :line1 %><br />
<%= address_form.text_field :line1 %>
</div>
<div class="field">
<%= address_form.label :line2 %><br />
<%= address_form.text_field :line2 %>
</div>
<div class="field">
<%= address_form.label :city %><br />
<%= address_form.text_field :city %>
</div>
<div class="field">
<%= address_form.label :state_id %><br />
<%= select("client[address]", "state_id", State.all.collect {|s| [ s.name, s.id ] }) %> </div>
<div class="field">
<%= address_form.label :zip %><br />
<%= address_form.text_field :zip %>
</div>
<% end %>

Resources