gem client_side_validations and fields_for nested form - ruby-on-rails

I am trying to use the gem on a nested form
https://github.com/bcardarella/client_side_validations
My code is
<%= form_for Question.new, remote: true, validate: true do |f| %>
<%= f.text_area :text, cols: 30, rows: 8 %>
<%= fields_for UnrosteredWork.new, :validate => true do |work_fields| %>
<%= work_fields.text_field :minutes"%>
<% end %>
<%= f.submit "Reply" %>
<% end %>
The gem works fine with my :text field.
I am trying to get the gem to work with the :minutes field
Right now, no client side validation is performed.
If break this field out into a separate form
<%= form_for UnrosteredWork.new, :validate => true do |work_fields| %>
<%= work_fields.text_field :minutes%>
<% end %>
Client side validation works fine on the :minutes field.
NOTE I was using version 3.2 of the gem. I am running rails 3.2 app.
I did try upgrade the gem to
gem 'client_side_validations', github: "bcardarella/client_side_validations", :branch => "4-0-beta"
However that didn't help.
Anyone had any success with nested forms before?

Related

Trouble dynamically adding fields with rails

I'm dynamically generating nested form fields using the cocoon gem as follows:
<%= simple_form_for #incorporation do |f| %>
<%= f.simple_fields_for :company do |company| %>
<%= link_to_add_association 'Add ID', company, :persons, class: "btn btn-default add-button" %>
<%= company.simple_fields_for :persons do |person|%>
<%= render 'person_fields', f: person %>
<% end =%>
<% end =%>
<% end =%>
And _person_fields is (currently) as follows:
<div>
<div class="col-md-6"><%= f.input :fname, input_html: {class: 'form-input form-control'}, label: "First Name" %></div>
</div>
The link_to_add_association should, through the cocoon gem and javascript, add another row of _person_fields to the form
The problem is that the button in fact adds nothing. Rather, it seems to just bring me to the top of the page. I know that javascript is installed (and working) via therubyracer gem. I know javascript is working because I've got bootstrap running on the site.
I know cocoon works and I've used it on a few apps. Does anyone see something that I might be leaving out?
Turns out I needed to RFTM I was missing the line:
//= require cocoon
in my application.js
I figure I'll keep this question up in case anyone else needs the reminder as well :-)

Why is the semantic_nested_form_for method missing in my production environment?

I'm trying to switch a site to use the nested_form gem in combination with formtastic, which I'm already using, but I'm getting a strange error. Everything works perfectly fine in development, and tests pass. This error only occurs in production.
Visiting the page where semantic_nested_form_for is used causes the following error:
ActionView::Template::Error (undefined method `semantic_nested_form_for' for #<#<Class:0x0000000524cca0>:0x00000004e46a70>):
1: <%= semantic_nested_form_for(#volume) do |f| %>
2: <%= f.inputs do %>
3: <%= f.input :number, :label => 'Volume #', :input_html => {:style => 'width:100px;', :autofocus => true}, :hint => raw('Choose the volume number.') %>
4: <% end %>
app/views/volumes/_form.html.erb:1:in `_app_views_volumes__form_html_erb__2632693694855646779_43838320'
app/views/volumes/new.html.erb:3:in `_app_views_volumes_new_html_erb___2327298489284463705_41053660'
The gem is in my Gemfile and seems to be installed correctly:
[~/application/current]$ bundle show nested_form
/home/deployer/application/shared/bundle/ruby/1.9.1/gems/nested_form-0.3.1
I can even successfully call the semantic_nested_form_for in the production console:
[~/application/current]$ rails c production
Loading production environment (Rails 3.2.12)
irb(main):001:0> helper.semantic_nested_form_for(Volume.new, url: '/volumes' do |f|
irb(main):002:2* f.inputs
irb(main):003:2> f.actions
irb(main):004:2> end
irb(main):005:1>
Additionally, semantic_form_for works fine by itself, but nested_form_for (without the semantic_ part) does NOT work, leading me to think this is related directly to nested_form.
I don't think the erb code is really relevant to this issue, but here it is anyway:
<%= semantic_nested_form_for(#volume) do |f| %>
<%= f.inputs do %>
<%= f.input :number, label: 'Volume #', input_html: {autofocus: true}, hint: 'Choose the volume number.' %>
<% end %>
<div id="issues_fields">
<%= f.semantic_fields_for :issues %>
<%= f.link_to_add 'Add an Issue', :issues %>
</div>
<%= f.actions do %>
<%= f.action :submit, label: 'Save' %>
<% end %>
<% end %>
Well I'm a little dumbfounded. Capistrano restarts unicorn when I deploy, and before I asked this question I had tried manually restarting nginx and unicorn just to rule that out. After I asked the question I rebooted the entire production server (which I hate doing) just to see, and now it's working fine.
If anyone can tell me a good reason why simply rebooting the unicorn process wasn't enough, I'll mark their answer as the accepted answer.

rendering a custom form helper (ckeditor_rails) in an activeadmin form:

I've got two gems that i use and enjoy
gem 'activeadmin'
and
gem "ckeditor"
I'd like for my 'content' field to use ckeditor.
In my past apps, I render ckeditor in a form like this:
<%= form_for #resource do |f| %>
<div class="field">
<%= f.label :content %>
<br />
<%= cktext_area_tag("page_part[content]", #page_part.content) %>
</div>
...
<% end %>
Now i just added activeadmin to my stack and like what i see so far. So, I read that you can customize the form like so by editing the app/admin/#{resource}.rb file:
ActiveAdmin.register NewsItem do
form :html => { :enctype => "multipart/form-data" } do |f|
f.inputs "NewsItem", :multipart => true do
f.input :title
f.input :content
f.input :photo
#NOT WORKING
cktext_area_tag("news_item[content]", #news_item.content)
#NOT WORKING
end
f.buttons
end
end
How can i get this form helper to work in active_admin, and what would i put in place of #news_item.content. #news_item is null... So right now I'm a bit confused.
When I try even witout reference to #news_item like so:
cktext_area_tag("news_item[content]", 'i cant be edited properly')
I still get:
undefined method `cktext_area_tag' for #<ActiveAdmin::DSL:0x00000007e02250>
Any help would be appreciated!
Ok,
Answer was pretty simple.
Taken from active admin's own documentation page: http://activeadmin.info/docs/5-forms.html
ActiveAdmin.register Post do
form :partial => "form"
end
Then I was able to use any form helper tags I wanted to:
<%= javascript_include_tag "/javascripts/ckeditor/ckeditor.js" %>
<%= semantic_form_for [:admin, #news_item], :multipart => true do |f| %>
<%= f.inputs :title, :photo %>
<%= cktext_area_tag("news_item[content]", #news_item.content) %>
<% end %>
You can try
f.template.some_view_method

Error with nested_form gem: wrong number of arguments (4 for 3)

I have been struggling with this one for days and can't seem to figure out what's wrong. I'm attempting to allow polymorphic file attachments to a model Item, which belongs to model Location. My routes are defined as:
resources :locations do
resources :items
post :sort
end
resources :items do
resources :assets #model for attachments
end
I followed a tutorial about doing exactly this with carrierwave and nested_form. After setting everything up, however, I get the following error when requesting the New action for the Item model: wrong number of arguments (4 for 3). It tells me the error is occurring at line 7 of this view:
<%= nested_form_for [#location, #item], :html => { :multipart => true } do |f| %>
<p>
<%= f.label :name %><br />
<%= f.text_field :name %>
</p>
<%= f.fields_for :assets do |a_form| %> ### LINE 7 ####
<p>
<%= a_form.label :file %><br />
<%= a_form.file_field :file %>
<%= a_form.hidden_field :file_cache %>
</p>
<%= a_form.link_to_remove "Remove this attachment" %>
<% end %>
<%= f.link_to_add "Add attachment", :assets %>
<p><%= f.submit %></p>
<% end %>
If I don't use the nested_form gem and start out the view with a normal form_for, I get no errors and am able to successfully attach a single file to the Item. I can try and proceed without the gem, but (as far as I understand) nested_form will automate some of the functionality like removing the files and generating ajax to add new attachments.
I was just wondering if anyone has run into this error or knows what mistake I'm making that's causing problems with nested_form? I understand what the error means, just not sure where/why the extra argument is being thrown in. I greatly appreciate any insight you can provide!
FYI my dev setup: rails (3.1.0, 3.0.10), nested_form (0.1.1), carrierwave (0.5.7)
In order to get nested_form working with rails 3.1, I had to pull the latest from github rather than using what's in the gem. In my Gemfile:
gem "nested_form", :git => "git://github.com/ryanb/nested_form.git"

Change rails text_field form builder type

I've been looking at the new options available in HTML5 forms, such as declaring input types as "email", "url", and "number", as described here.
How can I use these in conjunction with the rails form builders? I have tried
<% form_for #user do |f| %>
<%= f.email :email, {:placeholder => 'user#domain.com'} %>
<% end %>
But that does not work. I've also tried
<% form_for #user do |f| %>
<%= f.text_field :email, {:placeholder => 'user#domain.com', :type => :email} %>
<% end %>
But the type is still "text" and not overridden. Is it possible, or is this something that will need to be addressed in Rails itself?
Looks like there is currently an open ticket and patch for adding the HTML5 form input types. If you can't wait until the patch is accepted, you could apply the patch locally by freezing the actionpack gems and applying the patch or making an initializer that add the extra methods.
Of course the other option is adding the fields manually without a form helper:
<% form_for #user do |f| %>
<%= tag(:input, {:type => :email, :value => f.object.email} %>
<% end %>
There is now an email_field tag.

Resources