0

I have on change ajax event, that reloads a partial, to build nested attributes, based on the selected item.

The reloaded partial looks like this(I have cut most of it since it is not important):

      = f.fields_for :skill_of_objects, @character.skill_of_objects.build do |ff|
        %strong
          = school_skill.skill.name_pl
        = ff.hidden_field :skill_id, value: school_skill.skill.id
        = ff.label :value

The problem is the f builder.

I have done some stack research, and based on the last answer from >> this question I have ended in something like this:

'<%= form_for [current_user, @character] do |f| %>'
  $('#school_skills').html("<%= j render( partial: 'school_skills', locals: {f: f}) %>");
'<% end %>'

But I still get error like this:

ActionView::Template::Error (undefined local variable or method `f' for #<#<Class:0xca1eb70>:0xd0811c0>):

Any suggestions would be appreciated :)

Community
  • 1
  • 1
Kazik
  • 705
  • 1
  • 8
  • 20

2 Answers2

1

My error was in the js.file

$('#clan_select').html("<%= j render 'character_form_clan' %>");
$('#family_select').html("<%= j render 'character_form_families' %>");
$('#school_select').html("<%= j render 'character_form_schools' %>");
$('#character_honour').html("<%= j render 'character_honour' %>");
$('#character_outfit').html("<%= j render 'character_outfit' %>");
$('#school_skills').html("<%= j render 'school_skills' %>");
$('#character_<%= @old_school_bonus %>').val('2');
$('#character_school_bonus').val('<%= @selected_school.bonus_attr %>');
$('#character_<%= @old_family_bonus %>').val('2');
$('#character_family_bonus').val('<%= @selected_family.bonus_attr %>');
$('#character_<%= @selected_family.bonus_attr %>').val('<%= @increase_val %>');
$('#character_<%= @selected_school.bonus_attr %>').val('<%= @increase_val %>');
'<%= form_for [current_user, @character] do |f| %>'
  $('#school_skills').html("<%= j render( partial: 'school_skills', locals: {f: f}) %>");
'<% end %>'

I have rendered school_skills twice and the second time without |f| builder. After I have removed not second render everything works great.

'<%= form_for [current_user, @character] do |f| %>'
  $('#school_skills').html("<%= j render( partial: 'school_skills', locals: {f: f}) %>");
'<% end %>'

This part work great and it is the best way I have find so far.

Kazik
  • 705
  • 1
  • 8
  • 20
0

You can also pass object as @object to the partial and use fields_for like :

= fields_for @object do |ff|
        %strong
          = school_skill.skill.name_pl
        = ff.hidden_field :skill_id, value: school_skill.skill.id
        = ff.label :value
Muhammad Ali
  • 2,173
  • 15
  • 20