Forms on partial doesn't render properly - ruby-on-rails-3

I have a Rails app that is running fine on Rails 3.x and ActiveAdmin 0.6.6. However I want to upgrade it to Rails 5 and ActiveAdmin 1.x. I started the upgrade process first upgrading from 3.x to 4.x and then to 5x, and with ActiveAdmin I tested with 1.0.0 and now I'm using the master from Github.
Everything worked fine with the App in both Rails versions and ActiveAdmin but the ActiveAdmin forms that are in a partial.
The problem is the following:
I have a file app/admin/menu.rb whose has a partial views/admin/menus/_form.html.erb.
The partial contains the form of the Menu.
This is the contents of the partial (views/admin/menus/_form.html.erb):
<%= semantic_form_for [:admin, #menu], builder: ActiveAdmin::FormBuilder do |f| %>
<% f.inputs 'Campos Menu' do %>
<% f.input :project if current_admin_user.admin? %>
<% f.input :title %>
<% f.input :title_en %>
<% f.input :item %>
<% f.input :icon, as: :file %>
<% end %>
<% f.inputs 'Submenus' do %>
<% f.has_many :submenus, heading: '' do |fa| %>
<% fa.input :title, as: :string %>
<% fa.input :title_en, as: :string %>
<% fa.input :kind, as: :select, collection: Submenu.kind_collection %>
<% fa.input :items_as, as: :select, collection: Submenu.items_as_collection %>
<% end %>
<% end %>
<% f.actions do %>
<% f.action :submit %>
<% end %>
<% end %>
The render result of this file is only the 'actions' buttons, in this case the 'submit' button. The interesting thing is that if I remove the <% f.actions do %>... from the partial then the <% f.inputs 'Submenus' do %> gets rendered and the same if I remove the later. In other others is being rendered on the last block that contains an end.
The same behaviour is being observed on Rails 4.x and 5.x (except Rails 5.1.x which I didn't tested).
If I move the form from the partial to the app/admin/menu.rb it gets rendered properly. This could be a solution for me however I have others forms that make use of JQuery for fields manipulation and other stuff on the front end.
As I said before, all these forms partials were working properly on Rails 3.x and ActiveAdmin 0.6.6.
Anyone knows what's the problem?
DISCLAIMER: I'm not a Rails or ActiveAdmin expert, so bear with me if is a silly mistake related to this issue.

Yes, it's activeadmin#3486 I'm glad you figured out the workaround. I'm curious if this Arbre branch fixes it for you. Glad to see people still upgrading, I'll do what I can to help.

The solution that I found was to put a = for the <% f.input(s) %> on the partial. Having something like this <%= f.inputs ... %> instead of this <% f.inputs ... %> solves the problem.

Related

Is there a way to display the already added pictures when editing a post using Paperclip?

I am developing a web app where people can post new questions. Each question contains many sub-questions, and each sub-question can have many images. To post new questions, I use a Rails plug-in called 'nested-form', and to add pictures I use Paperclip.
Here is my question. When I add new questions, everything works fine. But when I edit the question, the fields for the pictures are still there but the picture directories in the fields are gone. If I submit the form with the blank fields, all the pictures would be gone. Therefore, I have to upload all the pictures again when I edit a question, even if I don't want to do any change to the pictures.
Here is my code:
new_question.html.erb:
<%= nested_form_for #question, :url => {:action => 'create_question'} do |f| %>
<%= f.label :name # some fields for #question %>
<%= f.text_area :name %>
<%= f.fields_for :subquestions do |builder| %>
<%= render 'subquestion_fields', :f => builder %>
<%= builder.link_to_remove 'Remove Sub-question' %>
_subquestion_fields.html.erb:
<%= f.hidden_field :_destroy %><br>
<%= f.link_to_add 'Add an image', :qimages %><br>
<%= f.fields_for :qimages, :html => { :multipart => true } do |builder| %>
<%= render 'qimage_fields', :f => builder %>
<%= builder.link_to_remove 'Remove this image' %><br>
<% end %>
_qimage_fields.html.erb:
<%= f.file_field :image %>
edit_question.html.erb uses the same partials above, but the picture that are already added to the question are not displayed(leaving blank 'qimage' fields). Is there a way to display all the images when editing the question?
Thank you!
Sorry. I just found out that there is a solution on Ryanb's Nested Form page. It is possible to track the event that generates new fields by listening to the nested:fieldAdded event, and the generated elements can be found by via event.field.

undefined method `model_name' for NilClass:Class in edit form

I have a rails app where I can create and edit records. I've created a form to enter data which works fine when I use the new/create actions. It will create a record no problem. But when I hit the edit action it gives me an undefined method 'model_name' for NilClass:Class.
I'm not sure what this means. Can someone give me a hand?
Form:
<%= form_for(#patient) do |f| %>
<%= f.label :Patient_Last_Name %>
<%= f.text_field :patient_last %>
<%= f.label :Patient_First_Name %>
<%= f.text_field :patient_first %>
<%= f.label :Patient_DOB %>
<%= f.date_select :patient_dob %>
<%= f.label :Primary_Diagnosis %>
<%= f.collection_select(:diagnosis_id, Diagnosis.all, :id, :diagnosis_name)%></br>
<%= f.label :Primary_Physician %>
<%= f.collection_select(:physician_id, Physician.all, :id, :physician_name)%></br>
<%= f.button :submit %>
<% end %>
View Code:
<td><%= link_to 'Edit', edit_patient_path(patient), :class => 'btn btn-close btn-mini'%></td>
Controller Code:
def edit
#patient = Patient.find(params[:id])
end
Edit view:
<%= render 'form' %>
When I remove the partial render from the form, the URL will go to the correct route/url. But I keep getting that error when the form partial is rendered.
There was an extra end statement in my controller which was cutting off half of the class which included the edit action. This was not allowing me to use the edit action. Once this typo was fixed things started working normally.
Sorry for the confusion.

Rails 3 ActiveRecord::UnknownAttributeError "unknown attribute: _destroy" for nested records

Let's say I have a schema in which an apple crate contains zero or more apples. While editing the apple crate in a form, I want to list the apples and provide a checkbox next to each apple, for deleting it when the form is submitted.
There is nothing going wrong that I can see. In my model I say
class AppleCrate < ActiveRecord::Base
has_many :apples
accepts_nested_attributes_for :apples, :allow_destroy => true
...
end
I have the form working, so far as I can tell. The checkboxes appear in the form html and when the form is processed by the controller each apple in the list has an attribute called "_destroy" which is set to either "1" or "0" depending on whether or not I checked the box before submitting.
According to the Rails API, when I set _destroy to 1 and save, the apple should be deleted. But when I submit the form I get
ActiveRecord::UnknownAttributeError in AppleCrateController#update
unknown attribute: _destroy
...
"apple_crate"=>{"id"=>"10101", "apples"=>{"1"=>{"id"=>"1",
"variety"=>"granny smith",
"apple_crate_id"=>"10101",
"_destroy"=>"1"},
"2"=>{"id"=>"2",
"variety"=>"fuji",
"apple_crate_id"=>"10101",
"_destroy"=>"1"},
"3"=>{"id"=>"3",
"variety"=>"macintosh",
"apple_crate_id"=>"10101",
"_destroy"=>"0"},
...
and so on.
I must be missing something obvious but after several days of futzing around I can't figure it out. I can successfully do everything else -- update, edit, index, etc -- so long as I leave out the :_destroy attribute. Any ideas?
(For what it's worth, I'm running rails 3.2.2 on Windows.)
Updated:
This is what I'm looking at in the documentation. (See the subsection "One-to-many".)
Updated:
As requested in comments, here is the view:
<%= form_for #apple_crate do |f| %>
<% #apples = #apple_crate.apples %>
<% #apples.each do |apple| %>
<%= fields_for "apples[]", apple do |apple_fields| %>
<%= apple_fields.text_field :variety %>
<%= apple_fields.hidden_field :apple_crate_id %>
<%= apple_fields.hidden_field :id %>
<%= apple_fields.check_box :_destroy %>
<% end %>
<% end %>
<%= f.submit "Save" %>
<% end %>
You should generate nested forms and forms with rails helpers, don't do it by your hands. So I think that's where your error at.
Try:
<%= form_for #apple_crate do |f| %>
<%= f.fields_for :apples do |apple_fields| %>
<%= apple_fields.text_field :variety %>
<%= apple_fields.hidden_field :apple_crate_id %>
<%= apple_fields.hidden_field :id %>
<%= apple_fields.check_box :_destroy %>
<% end %>
<% end %>
something like this, did not check if it's correct, but idea should be clear enough

Ruby on Rails simple_form_for all.each do

I am using Rails 3.0, Ruby 1.9.2 and the Plataformatec simple_form gem. This code works with a form_for but not simple_form_for:
<%= simple_form_for(#provider) do |f| %>
<% Car.all.each do |c| %>
<div>
<%= check_box_tag :car_ids, c.id, #store.cars.include?(c), :name => 'store[car_ids][]' %>
$<%= c.cost %> | <%= c.description %>
</div>
<% end %>
<div class="actions">
<%= f.submit "New" %>
</div>
<% end %>
How do I get it to work with simple_form_for?
Thanks in advance!
You can't use simple_form right the same way as form_for.
For example ther is no any check_box_tag method in simple_form gem. There is ONLY inuput fields that you can specify with :as option. So your check_box_tag will be converted to
f.input car_ids, ..., :as => :check_box
Checkout Usage, Rdoc and other useful stuff https://github.com/plataformatec/simple_form
The problem was in the controller code.
In the "new" controller action I can't simply perform:
#provider = Provider.new(params[:provider])
as one would normally.
Instead I have to process each parameter separately:
#provider.location = params[:provider][:location]
etc...
For the Car check boxes, I add each car_id from the car_ids parameter to the "has_many" cars model association one at a time:
car_ids = params[:provider][:car_ids]
car_ids.each do |cid|
#provider.cars << Car.find(cid)
end
Then I can call:
#provider.save!
And it saves correctly (my initial problem was that it wasn't saving the selected Cars).
For some reason, I was able to figure this out only after posting the question here. Funny how that works.
Thanks all for your replies!

Can't use the nested_form_for gem

I am using the nested_form_for gem for the first time. I don't know if this is an issue or if I am using it wrong, but I am getting an "undefined method nested_form_for" error.
I have a pretty regular form as you can see:
<%= nested_form_for #user do |f| %>
<%= f.fields_for :godfathers do |godfather_form| %>
<%= godfather_form.label :name %> <br/>
<%= godfather_form.text_field :name %> <br/>
<%= godfather_form.label :description %> <br/>
<%= godfather_form.text_field :description %> <br/>
<%= godfather_form.link_to_remove "Remove this godfather" %>
<% end %>
<%= f.link_to_add "Add a godfather", :godfathers %>
<% end %>
By the way, I installed the gem and ran the: rails generate nested_form:install command to generate the nested_form.js file that I included in my layout after the jquery inclusion(<%= javascript_include_tag :default, 'nested_form' %>).
Anyone using this gem as well?
Thanks!
Everything looks right and if the generator ran the gem should be in place. Have you restarted your server since adding the plugin to your Gemfile?