Nested property fields do not show up using the Reform gem - ruby-on-rails

I'm using the Reform gem to make a form object in my current project but the nested fields don't show up in the form. Here's my code:
Shipment Model:
class Shipment < ApplicationRecord
has_one :shipment_detail
end
ShipmentDetail Model:
class ShipmentDetail < ApplicationRecord
belongs_to :shipment
end
Reform Class
class ShipmentForm < Reform::Form
property :shipment_type
property :measure
property :shipment_detail do
property :po_number
property :job_no
end
end
Controller
class ShipmentsController < ApplicationController
def new
#shipment = ShipmentForm.new(Shipment.new)
end
end
Template
<%= form_for #shipment, url: shipments_path, method: :post do |f| %>
<%= f.label :shipment_type %><br />
<%= f.text_field :shipment_type %><br /><br />
<%= f.label :measure %><br />
<%= f.text_field :measure %><br /><br />
<%= f.fields_for :shipment_detail do |d| %>
<%= d.label :po_number %><br />
<%= d.text_field :po_number %><br /><br />
<%= d.label :job_no %>
<%= d.text_field :job_no %><br /><br />
<% end %>
<% end %>
Only fields shipment_type and measure are visible on the form, po_number and job_no are not. What should I do to make them visible?

In Reform you need to use a prepopulator to create a new/blank :shipment_detail section to appear on the form.
http://trailblazer.to/gems/reform/prepopulator.html
prepopulators is when you want to fill out fields (aka. defaults) or add nested forms before rendering.
populators is them code that is run just before validation.
Here is what I used in my code you can get the idea for yours from it:
collection :side_panels, form: SidePanelForm,
prepopulator: ->(options) {
if side_panels.count == 0
self.side_panels << SidePanel.new(sales_order_id: sales_order_id, collection: sales_order.collection)
end
}
Prepopulation must be invoked manually.
Controller#new
#shipment_form = ShipmentForm.new(Shipment.new)
#shipment_form.shipment_detail #=> nil
#shipment_form.prepopulate!
#shipment_form.shipment_detail #=> <nested ShipmentDetailForm #model=<ShipmentDetail ..>>
RE: The edit form
If you create a ShipmentForm in the new action and leave the details section blank and later you want to have these fields appear on the edit action you need to run the prepopulators again on that action too. Just like the new action.
In my code above I have if side_panels.count == 0 line will add in the missing lines on the editing form if there is none there currently.

Related

How to use nested forms in Rails if the fields have the same name?

I have two models, Dog and Owner, and I want their names to be the same, and would be redundant if I asked to fill out the fields twice (once for the dog and another time for the owner). I'm wondering if there's a simpler way to update the two databases with one input.
<h1>Create a new Dog:</h1>
<%= form_for(#dog) do |f|%>
<div>
<%= f.label :name%>
<%= f.text_field :name%>
</div><br>
<div>
<%= f.label :breed%>
<%= f.text_field :breed%>
</div><br>
<div>
<%= f.label :age%>
<%= f.text_field :age%>
</div><br>
<div>
<h3>create a new owner:</h3>
<%= f.fields_for :owner, Owner.new do |owner_attributes|%>
<%= owner_attributes.label :name, "Owner Name:" %>
<%= owner_attributes.text_field :name %>
<% end %>
</div>
<%= f.submit %>
<% end %>
First of all, not sure why you want to keep the name of the owner and the dog same.
However, there can be many ways to achieve what you want:
You can simply omit the owner name from the form.
So you no longer need: <%= owner_attributes.label :name, "Owner Name:" %>
OR you no longer need:
<div>
<%= f.label :name%>
<%= f.text_field :name%>
</div><br>
And in the Owner/Dog model, you can pass the name of the dog/owner in a callback - maybe after_initialize or before_save or before_validation depending on your validation requirements.
class Dog
belongs_to :owner
before_validation :set_name
private
def set_name
self.name = owner&.name
end
end
You can make the owner name as a hidden field instead and can write some javascript to update the hidden field with the dog name before submitting the form or onblur event. I would prefer the first approach since it's simpler and more secure than only JS solution to maintain database consistency
If dogs belongs_to and owner, you don't really need to store the owner's name separately. You can just call dog.owner.name anywhere you have a Dog instance. Having said that, it is relatively straightforward to append attributes on top of the POSTed form values in your controller using .merge():
def create
#dog = Dog.new(dog_params.merge(owner: params[:dog][:owner])[:name])
if #dog.save
...
end
end
def dog_params
params.require(:dog).permit(:name, :breed, :age, owner: %i[name])
end

Rails 5 fields_for nesting under a collection

So I have a model with this structure:
document {
structure {
id,
...
},
fields {
field {
id,
...
},
value {
...
}
}
}
And I'm trying to put together a form for the fields, where the fields would look like:
<input type="hidden" name="document[fields][0][field][id]" />
<input type="text" name="document[fields][0][value]" />
<input type="hidden" name="document[fields][1][field][id]" />
<input type="text" name="document[fields][1][value]" />
...
This is what I'm currently doing:
<%= document.fields.each_with_index do |df, i| %>
<%= f.fields_for "fields[]", df do |builder| %>
<p>
<%= builder.label df.field.name %>
<%= builder.fields_for :field do |field_builder| %>
<%= field_builder.hidden_field :id, value: df.field.id %>
<% end %>
<%= builder.fields_for :value do |value_builder| %>
<%= render df.field.edit_view, field: df.field, builder: value_builder %>
<% end %>
</p>
<% end %>
<% end %>
But the resulting field names are:
document[fields][field][id]
document[fields][value][value]
In other words, the indices are missing.
Using <%= builder.hidden_field :id, value: df.field.id %> results in the correct format but no index (because the field object has no id yet): document[fields][][id] but that is not an option for value, as there could be multiple fields involved.
Is there a way to do this using the form helpers, or is what I'm doing just too wonky?
Edit: Here are the models involved:
The idea is to have a configurable set of fields for document. The structure determines fields available, and a document_field is a relation between a document and a field with a value contained.
class Field < ApplicationRecord
belongs_to :structure
end
class Document < ApplicationRecord
belongs_to :structure
has_many :document_fields
class DocumentField < ApplicationRecord
belongs_to :document
belongs_to :field
class Structure < ApplicationRecord
has_many :fields
You should configure a nested attributes setup on your models, build those nested relations on the controller, and instantiate an object in you form_for block instead of passing a referencing symbol.

RoR : Mongoid and form create hash

Simple question for Rails gurus. Why I do have to use the following statement to insert a new Mongoid document : params[:video][:description] in the following create method of my VideosController? Why I can't use the params[:description] from the POST form? If I use it, the value becomes nil.
def create
#video = Video.new(
:title => params[:video][:title],
:description => params[:video][:description]
)
if #video.save
render 'success'
else
render 'error'
end
end
Here is the Video.rb class :
class Video
include Mongoid::Document
field :title, type: String
field :description, type: String
validates_presence_of :title
validates_presence_of :description
acts_as_url :title
end
And finaly the form view :
<%= form_for #video do |f| %>
<%= f.label :title %>
<%= f.text_field :title %>
<p/>
<%= f.label :description %>
<%= f.text_field :description %>
<%= submit_tag("Enqueue video") %>
<% end %>
I don't quite get why the form input are video[description] and not just description as expected :
<label for="video_title">Title</label>
<input id="video_title" name="video[title]" type="text" />
<p/>
<label for="video_description">Description</label>
<input id="video_description" name="video[description]" type="text" />
When you are using form_for:
Creates a form that allows the user to create or update the attributes
of a specific model object.
In your case, Video model. To understand Rails convention:
<%= form_for #video do |f| %>
...
<%= f.text_field :description %>
...
Which results in an html tag whose name attribute is video[description]. This means that when the form is submitted, the value entered by the user will be available in the controller as params[:video][:description].
The params variable is an instace of ActiveSupport::HashWithIndifferentAccess, like a Hash with a small difference, according to documentation:
This class has dubious semantics and we only have it so that people
can write params[:key] instead of params[‘key’] and they get the same
value for both keys.
Your params is something like:
{"utf8"=>"✓",
"_method"=>"post",
"authenticity_token"=>"xxx",
"video"=>
{"description"=>"Video desc"}
}
Where "video" or :video is one of the keys of the Hash. So, params[:video] is equivalent to params.fetch("video") which value is {"description"=>"Video desc"}. As you can see the value is another Hash. Finally to get the value of the description you have to params[:video][:description] (The Rails way) or params.fetch("video").fetch("description"), which value is "Video desc".
fetch is a Ruby method of Hash: "Returns a value from the hash for the given key."
Taking this into account:
Video.new(params[:video]) = Video.new(:description => "Video desc") = Video.new(:description => params[:video][:description])
It's easier to use conventions, but for sure you can have params[:description] (just in case):
<%= form_for #video do |f| %>
...
<%= text_field_tag :description %>
...
Note that I'm using text_field_tag instead of f.text_field. In this case the html tag name will be description in the params Hash you will receive { "description" => 'Video desc" }...
Take a look to Rails API documentation to understand different helpers, etc. And also review your server's log.
If you want to use video[:description]. Create your form like this
<%= form_for #video do |f| %>
....
<p/>
<%= f.label :description %>
<%= f.text_field :description, :name => "description" %>
....
<% end %>
Rails form_for helper name the input elements such that it becomes easy to push them into model attributes in one go like this
#video = Video.new(params[:video])
so that you don't have to do it like the way you have done
#video = Video.new(
:title => params[:video][:title],
:description => params[:video][:description]
)

Rails 3 - Create View to Insert Multiple Records

I have what seems like a simple query. I need to create a view that will accept multiple records based on a single model. In my case the model is Project, which has 1 foreign key (person) and 2 fields time, role. I need to create a view (form) to insert 5 roles.
<%= form_for(#project) do |f| %>
<% 5.times do |index|%>
<div class="field">
<%= f.label :position %><br />
<%= f.text_field "fields[#{index}][stime]" %>
</div>
<% end %>
<div class="actions">
<%= f.submit %>
</div>
<% end %>
I get an error message: undefined method `fields[0][stime]'
I do not think the railscasts for nested models is what I need.
How would I go about creating this?
EDIT: The Project model code is below:
class Project < ActiveRecord::Base
belongs_to :person
attr_accessible :role, :stime
end
The Projects_Controller code for the new method is below:
def new
#project = Project.new
end
I see you're planning to make some 1-to-many relationship (Product has_many :roles).
Here's some advices.
First, take a look at the accepts_nested_attributes_for method. You need to add it to your model to be able to perform mass-create.
Second, fields_for is what you need to design nested forms.
I'll give you some example of mass-creating for a simple Product has_many :line_items case:
<%= form_for #product do |f| %>
<%= f.fields_for :line_items, [LineItem.new]*5 do |li_fields| %>
<%= li_fields.text_field :quantity %>
<%= li_fields.text_field :price %>
<br>
<% end %>
<%= f.submit "Create line items" %>
<% end %>
All you need is to write in you controller something like:
#product.update_attributes params[:product]
and 5 line_items will be created at once.
Don't forget to white-list association_attributes (see params in your logs to see it). But I think if you get the mass-assignment error you'll do it anyway :)
I hope it helps.

Ruby on Rails: Multiple Input Fields in The Same Form - Change ID/Value

Have a page where there are multiple input fields of the same thing, Posts. Right now, when a user enters in a question for, let's say 3 fields, the only one that saves to the database is the last one. Whereas, it should save all three and give them each it's own post_id. Also; if the user doesn't enter anything in for the other fields, it should not save in the database either.
new_step_4_html.erb
<%= form_for(#post) do |f| %>
<%= f.text_field :content %>
<%= f.text_field :content %>
<%= f.text_field :content %>
<% end %>
projects_controller.rb
def new_step_4
#post = Post.new
end
Right now, all it does is submit one :content field, obviously because they all share the same id/value. Unfortunately, the Railscasts #197 applies for nested forms, so the javascript and helper stuff he does all applies for nested. I would think this is something simple. Person from IRC mentioned I could do some sort of '3.times' code into the view file or something?
First of all you will probably have to edit the model of you post.
post.rb
has_many :contents, :dependent => :destroy
accepts_nested_attributes_for :contents
You will need another model to store the content fields.
so first generate a model
rails g model content post_id:integer body:text
the model
content.rb
belongs_to :post
Now, in stead of doing <%= f.text_field :content %> a few times, let rails create them, because now you basically let them overwrite each other.
3.times do
content = #post.content.build
end
the form view will be something like this:
<%= form_for #post do |f| %>
<%= f.fields_for :contents do |builder| %>
<%= builder.label :body, "Question" %><br />
<%= builder.text_area :body, :rows => 3 %><br />
<%= end %>
<p><%= f.submit "Submit" %></p>
<% end %>
I did not test this code, but the idea should be correct. Let me know if you need more info.

Resources