Delmed
Delmed

Reputation: 21

rails 4 update nested attributes does,'t work

i tried all of answer about this issue, but i still not able to resolve my probleme. i have to controllers ( and models ) : checklist and field .

class NcchecklistsController < ApplicationController
def update
    if @ncchecklist.update(ncchecklist_params)
      redirect_to ncchecklists_url, notice: 'Check-list sauvgardée.'
    else
      render :index
    end
  end
private
    # Use callbacks to share common setup or constraints between actions.
    def set_ncchecklist
      @ncchecklist = Ncchecklist.find(params[:id])
    end

    # Only allow a trusted parameter "white list" through.
    def ncchecklist_params
      params.require(:ncchecklist).permit(:name, ncfield_params: [:value, :id])
    end

fields controller

class NcfieldsController < ApplicationController
private
    # Use callbacks to share common setup or constraints between actions.
    def set_ncfield
      @ncfield = Ncfield.find(params[:id])
    end

    # Only allow a trusted parameter "white list" through.
    def ncfield_params
      params.require(:ncfield).permit(:name, :value, :ncchecklist_id)
    end

checklist model

class Ncchecklist < ActiveRecord::Base
  has_many :ncfields, dependent: :destroy
  attr_accessible :ncfields
  accepts_nested_attributes_for :ncfields
end

field model

class Ncfield < ActiveRecord::Base
  belongs_to :ncchecklist

end

the view :

<h1> Vos check-list </h1>
<% @ncchecklists.each do |ncchecklist| %>
  <%= form_for(ncchecklist) do |ncchecklist_form| %>
    <h3><%= ncchecklist.name %></h3>
    <% ncchecklist.ncfields.each do |field| %>
      <%= ncchecklist_form.fields_for :ncfields, field do |ncfield_form| %>
        <p>
          <%= ncfield_form.label :name, 'Tâche : ' %>
          <%= field.name %> : 
          <%= ncfield_form.label 'value_false', 'Non' %>
          <%= ncfield_form.radio_button :value, false %>
          <%= ncfield_form.label 'value_true', 'Oui' %>
      <%= ncfield_form.radio_button :value, true %>
        </p>
      <% end %>
    <% end %>
    <%= ncchecklist_form.submit 'Sauvegarder '+ncchecklist.name %>
  <% end %>
<% end %>

the view is like: view screen-shot

the probleme is when i change the value of radio button and i click to save, nothing happen. i have find this in log:

Started PATCH "/redmine/ncchecklists/2" for ::1 at 2016-12-21 15:58:48 +0100
Processing by NcchecklistsController#update as HTML
  Parameters: {"utf8"=>"✓", "authenticity_token"=>"XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX", "ncchecklist"=>{"ncfields_attributes"=>{"0"=>{"value"=>"false", "id"=>"2"}, "1"=>{"value"=>"false", "id"=>"3"}}}, "commit"=>"Sauvegarder Chef de projet", "id"=>"2"}
  Current user: anonymous
Redirected to http://localhost/redmine/ncchecklists

thanks fo help

Upvotes: 2

Views: 232

Answers (1)

Jared
Jared

Reputation: 1184

The strong params for your controller are just a bit off. They should be ${model}_attributes instead of ${model}_params

def ncchecklist_params
  params.require(:ncchecklist).permit(:name, ncfields_attributes: [:value, :id])
end

You can see it being sent in your raw log at the end of your question but the strong params are filtering it out.

Upvotes: 1

Related Questions