Active Admin, one page - two diffrent forms - ruby-on-rails

I have following two resources:
Position
JobTitle
And a position belongs_to job title.
In page Position, I create new position, with job_title select and some other selects. I need also create new job_title in this page, instead of choose select variants. So, in fact, or I choose job title in existing base, or create new, and save it for this position.
How can I make this? I looking for answer, and reading docs, but nothing!

You need to use fields_for in your view to send params for two models at a time.
Here's how I would try to solve to problem:
<%= form_for #job_title do |f| %>
<%= f.error_messages %>
<p>
<%= f.label :name %><br />
<%= f.text_field :name %>
</p>
<p>
<%= f.label :description %><br />
<%= f.text_field :description %>
</p>
<%= f.fields_for :position do |builder| %>
<p>
<%= builder.label :name %> <br />
<%= builder.text_field :name %>
</p>
<% end %>
<p><%= f.submit "Submit" %></p>
<% end %>
And then, in your controller, you need to create the association. For more information, you can check out the railscast for nested forms.

Related

Form not showing up - Ruby on Rails

I'm currently following a Ruby on Rails tutorial book and I've noticed that the newer version of Rails is quite a bit different. A couple of given commands were different than described in the book and I've had to look up a few ways on how to fix these things. Right now though, I don't know what's going wrong. I have created a database table products and I'm simply trying to use a form to display some input components etc to create a new product. The book told me to do this:
<h1>New product</h1>
<% form_for(#product) do |f| %>
<%= f.error_messages %>
<p>
<%= f.label :title %><br />
<%= f.text_field :title %>
</p>
<p>
<%= f.label :description %><br />
<%= f.text_area :description, :rows => 6 %>
</p>
<p>
<%= f.label :image_url %><br />
<%= f.text_field :image_url %>
</p>
<p>
<%= f.label :price %><br />
<%= f.text_field :price %>
</p>
<p>
<%= f.submit "Create" %>
</p>
<% end %>
<%= link_to 'Back', products_path %>
However, the view only shows me the New product header and the link to go back. I've already installed a different gem because the f.error_messages apparently wasn't used anymore either. The problem is, the entire form_for part does not show up anything. Can anybody tell me how I am supposed to change this code to get it to show up on the view for creating a new product?
Thanks!
This is what it shows:
You are missing = here <% form_for(#product) do |f| %>. It should be
<%= form_for(#product) do |f| %>

How to set and unset build_association depending on contents of a text_field in Rails?

I have a simple Rails form that uses a conditional build_association (if there's not already an associated parent) to include a text_field to edit a parent. This is useful if a new record for the parent should be added. The problem with this is that if a parent already exists, if the text_field is updated then it will be changed for the parent record affecting many children. If build_association is called regardless of whether a parent association already exists, then new, duplicate parents will constantly be created.
I am trying to understand how I can use build_association and fields_for to add a new parent to the database if the user types in an unfamiliar name, but to set the parent to be an existing record if the name matches an existing parent's name. (If I can get this part to work right, the next step will be to add autocomplete.) The code is below:
<%= form_for #sermon, :html => { :multipart => true } do |f| %>
<div class="field">
<%= f.label :title %><br />
<%= f.text_field :title %>
</div>
<div class="field">
<%= f.label :date %><br />
<%= f.text_field :date %>
</div>
<div>
<% #sermon.build_speaker unless #sermon.speaker %>
<%= f.fields_for :speaker do |g| %>
<%= g.label :name, "Speaker name:" %><br />
<%= g.text_field :name %>
<%= g.submit %>
<% end %>
</div>
<div class="actions">
<%= f.submit %>
</div>
<% end %>
I think my gem get_or_build will be useful for you. I had the same problem and didn't find any appropriate clean solution so I've decided to write this gem. Enjoy!

Sending the details of the multi form to the database

I have to create a form which includes Batches and Samples.
General idea would be Batches having (received:date, group_leader:string, contact_person:string, batch_comment:string and num_of_samples:integer) .
N.B batches have many samples.
now the Samples include ( sample_name, species, sample_type, ini_conc, ini_vol, and sample_comment).
Now as these are of one to many relationships, I have created a form after following many tutorials having a page showing batch detail entry as well as asking for how many samples it involved in each batch. After the user types in (for eg. 3), the form generates three fields having sample details (also including name, species, type, conc, vol, etc.) but I was not able to send these details to the database.
The only thing that updates the database is one sample information.
my view looks something like this :
New sample:
<p><% form_for(sample) do |f| %></p>
<p><% (1..#batch.sample_no).each do |i| -%></p>
<%= f.error_messages %>
<p>
<%= f.hidden_field :batch_id %>
</p>
<p>
<%= f.label :sample_name %><br />
<%= f.text_field :sname %>
</p>
<p>
<%= f.label :organism %><br />
<%= f.text_field :organism, :rows => 2 %>
</p>
<p>
<%= f.label :sample_type %><br />
<%= f.select(:samp_type, %w{ DNA RNA}) %>
</p>
<p>
<%= f.label :ini_conc %><br />
<%= f.text_field :ini_conc %>
</p>
<p>
<%= f.label :ini_vol %><br />
<%= f.text_field :ini_vol %>
</p>
<p>
<%= f.label :storage_space %><br />
<%= f.text_field :storage_space %>
</p>
<p>
<%= f.label :samp_comment %><br />
<%= f.text_area :samp_comment, :rows => 3 %>
</p>
<% end -%>
<p><%= f.submit 'Submit' %></p>
<% end %>
I haven't done anything much with my controller. (in fact I have no idea which controller I should add codes to) meaning I have created two scaffolds, one for batch and one for samples and I have made a partial of the samples_view_new.html.erb and saved it in the batch_view folder which gives me an opportunity to add samples in the show_html.erb of the batch.
my models of batch.rb and sample.rb look some thing like this
<p>class Batch < ActiveRecord::Base</p>
<p>has_many :samples</p>
<p>end</p>
<p>class Sample < ActiveRecord::Base </p>
<p>belongs_to :batch</p>
<p>end</p>
if I understand your requirements correctly, you're searching for nested forms as excellently described by Ryan Bates at railscasts.com
Tutorial 1: Nested Model Form Part 1
Tutorial 2: Nested Model Form Part 2
Here is some more help for you:
In your Bash model
has_many :samples
accepts_nested_attributes_for :samples
Your Bash Controller will have at least 2 actions: new and create
In your new action:
#bash = Bash.build
4.times { #bash.samples.build } # where 4 is the no. of samples, could also be param[:amount] in case you add a preliminary action with a form and an amount field, but I suggest using javascript as described in Tutorial 2 above for better user experience
In the create action:
#bash = Bash.new(params[:bash]) # you now have access to #bash.samples
if #bash.save ...
And finally the "new" view:
<%= form_for #bash do |f| %>
<%= f.text_field :group_leader %> <!-- ... -->
<%= f.fields_for :samples do |builder| %>
<%= builder.text_field :sample_name %> <!-- ... -->
<% end %>
<%= end %>
With this and the tutorials you should now be able to build a killer app for your degree's application. Good luck!
I think I have the solution to the problem...
My main idea was to send multiple rows in the database at once... so just follow this tutorial
http://www.stephenchu.com/2008/03/paramsfu-3-using-fieldsfor-and-index.html

Adding validations without knowing the fields

My example form
<% form_for #ad do |f| %>
<%= f.error_messages %>
<p>
<%= f.label :ad_type_id %><br />
<%= f.collection_select(:ad_type_id, AdType.all, :id, :name) %>
</p>
<p>
<% #ad.ad_properties.each do |property| %>
<%= property.name %>:
<% f.fields_for :ad_values do |value_field| %>
<%= value_field.text_field :ad_id, :value => #ad.id %>
<%= value_field.text_field :ad_property_id, :value => property.id %>
<%= value_field.text_field :value %>
<% end %><br /><br />
<% end %>
</p>
<p>
<%= f.label :description %><br />
<%= f.text_area :description %>
</p>
<p><%= f.submit %></p>
<% end %>
Explanation:
Ad has many properties. I can add new properties at any time (it's a normal model).
Lets say the Ad is of the type 'hotel'. Then I would add properties like 'stars' and 'breakfast_included'
Then I store each of these properties' values in a separate model.
And all this works fine with my form above.
My problem:
These fields are not validated because I can't know what their names are.
I need to add validations dynamically somehow.
My thought:
#Before the normal validations kick in
def add_validations
self.properties.each do |property|
property.add_validation :whatever #somehow :)
end
end
How could I do this?
Have you tried with polymorphic associations ? That's maybe a cleaner approach.
http://railscasts.com/episodes/154-polymorphic-association
Disclaimer: I've never done this before, so I'm not 100% sure it will work.
But as long as you can get the type of the object you are working with, you can use the Rails constantize method to get the model it references (I'm assuming that if ad can be of type Hotel, then you have a Hotel model). At that point you should probably have the appropriate validations on your Hotel model.

In Rails, how do I generate form labels without symbols that still create correct "for" attributes?

In Rails, how do I generate form labels without symbols that still create correct "for" attributes?
If I take this form:
<% form_for(#thing) do |f| %>
<%= f.error_messages %>
<p>
<%= f.label :name %><br />
<%= f.text_field :name %>
</p>
<p>
<%= f.submit 'Create' %>
</p>
<% end %>
And alter it to improve the clarity of what's expected in the field:
<% form_for(#thing) do |f| %>
<%= f.error_messages %>
<p>
<%= f.label "What would you like to call your thing?" %><br />
<%= f.text_field :name %>
</p>
<p>
<%= f.submit 'Create' %>
</p>
<% end %>
The for attribute on the label tag would read "thing_What would you like to call your thing?", which obviously destroys its relationship with the intended partner field.
So how do I alter the label text while preserving that relationship?
<%= f.label :name, "What would you like to call your thing?" %>
See label’s documentation (and on APIdock).

Resources