0
votes

I am creating an ruby on rails 5 application. It is a todolist kind of application and using simple_form, haml-rails and cocoon gem. This is a application with nested form.

  1. Generated a scaffold for goal and created model for action Goal.rb and Action.rb

    class Goal < ApplicationRecord
      has_many :actions
      accepts_nested_attributes_for :actions, reject_if: :all_blank
    end
    
    class Action < ApplicationRecord
      belongs_to :goal
    end
    
    1. Then added the actions_attribute to permit in the params
class GoalsController < ApplicationController   before_action :set_goal, only: [:show, :edit, :update, :destroy]   def index
    @goals = Goal.all   end   def show   end   def new
    @goal = Goal.new   end   def edit   end   def create
    @goal = Goal.new(goal_params)
    respond_to do |format|
      if @goal.save
        format.html { redirect_to @goal, notice: 'Goal was successfully created.' }
        format.json { render :show, status: :created, location: @goal }
      else
        format.html { render :new }
        format.json { render json: @goal.errors, status: :unprocessable_entity }
      end
    end   end   def update
    respond_to do |format|
      if @goal.update(goal_params)
        format.html { redirect_to @goal, notice: 'Goal was successfully updated.' }
        format.json { render :show, status: :ok, location: @goal }
      else
        format.html { render :edit }
        format.json { render json: @goal.errors, status: :unprocessable_entity }
      end
    end   end   def destroy
    @goal.destroy
    respond_to do |format|
      format.html { redirect_to goals_url, notice: 'Goal was successfully destroyed.' }
      format.json { head :no_content }
    end   end   private
    def set_goal
      @goal = Goal.find(params[:id])
    end
    def goal_params
      params.require(:goal).permit(:name, :purpose, :deadline, actions_attributes: [:step])
    end

end

  1. Forms for both form.html and action.html

_form.html.haml

= simple_form_for(@goal) do |f|
  = f.error_notification

  .form-inputs
    = f.input :name
    = f.input :purpose
    = f.input :deadline
    %h3 Actions
    #tasks
      = f.simple_fields_for :actions do |action|
        = render 'action_fields', f: action
      .links
        = link_to_add_association 'Add', f, :actions

  .form-actions
    = f.button :submit

// action.html.haml
.nested-fields = f.input :step = f.input :done, as: :boolean = link_to_remove_association "remove task", f

log in the console when i submit form

Processing by GoalsController#create as HTML
  Parameters: {"utf8"=>"✓", "authenticity_token"=>"jel0g+oCkNaspe7T9dz7suDczIOdoKJqPqPDK9ta0WLPbwaSPBwshrDD2BrNAFeLoDx+0/soe11MWaZaH8cQoA==", "goal"=>{"name"=>"ja", "purpose"=>"asdfd", "deadline"=>"asdfasd", "actions_attributes"=>{"0"=>{"step"=>"ARasd", "done"=>"1", "_destroy"=>"false"}, "1475080334356"=>{"step"=>"", "done"=>"0", "_destroy"=>"false"}}}, "commit"=>"Create Goal"}
Unpermitted parameter: _destroy
Unpermitted parameter: _destroy
   (0.5ms)  BEGIN
   (0.5ms)  ROLLBACK
  Rendering goals/new.html.haml within layouts/application
  Rendered goals/_action_fields.html.haml (18.5ms)
  Rendered goals/_action_fields.html.haml (26.5ms)
  Rendered goals/_action_fields.html.haml (19.0ms)
  Rendered goals/_form.html.haml (252.5ms)
  Rendered goals/new.html.haml within layouts/application (309.0ms)
Completed 200 OK in 932ms (Views: 898.5ms | ActiveRecord: 1.0ms)

enter image description here

1

1 Answers

1
votes

The console log shows: Unpermitted parameter: done

This is because in your controller, only these parameters are permitted:

params.require(:goal).permit(:name, :purpose, :deadline, actions_attributes: [:step])