mercredi 28 septembre 2016

What to do with error 'Please review the problems below:' using cocoon gem?

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

Started POST "/goals" for ::1 at 2016-09-28 21:21:48 +0530
Processing by GoalsController#create as HTML
  Parameters: {"utf8"=>"✓", "authenticity_token"=>"GEjMM1ntPTj29+5rBPvnQ9EKnx1umS+KjyQ1DwK0attazr4ij/OBaOqR2KI8J0t6keotTQgR9r393lB+ximrGQ==", "goal"=>{"name"=>"Raja", "purpose"=>"asdfasd", "deadline"=>"asdfasdf", "actions_attributes"=>{"0"=>{"step"=>"Rajasdfsdfsdwead", "done"=>"0", "_destroy"=>"false"}, "1"=>{"step"=>"ewerqsdfd", "done"=>"0", "_destroy"=>"false"}}}, "commit"=>"Create Goal"}
Unpermitted parameter: done
Unpermitted parameter: done
   (3.0ms)  BEGIN
   (1.0ms)  ROLLBACK
  Rendering goals/new.html.haml within layouts/application
  Rendered goals/_action_fields.html.haml (30.5ms)
  Rendered goals/_action_fields.html.haml (27.0ms)
  Rendered goals/_action_fields.html.haml (30.5ms)
  Rendered goals/_form.html.haml (371.8ms)
  Rendered goals/new.html.haml within layouts/application (450.3ms)
Completed 200 OK in 1597ms (Views: 1431.2ms | ActiveRecord: 4.0ms)

Aucun commentaire:

Enregistrer un commentaire