0

I am using the wicked gem for a wizard form. In one of the steps of my form I have fields to upload attachments. I'm using active storage and Rails 6.

In my view

<%= f.file_field :plan %>
<%= f.file_field :appraisal %>
<%= f.file_field :flow %>

In my sale_steps controller

      def show
        @sale = current_user.sales.find(params[:sale_id])
        render_wizard
      end

      def update
        @sale = current_user.sales.find(params[:sale_id])
        params[:sale][:status] = step.to_s **(the error is on this line)**
        @sale.update(sale_params)
        render_wizard @sale
      end
    
      def sale_params
          params.require(:sale).permit(.... :plan, :appraisal, :flow)
      end

In my model

    has_one_attached :plan
    has_one_attached :appraisal
    has_one_attached :flow

    validates :location,  presence: true, if: -> { status?(:second_step) }

    def status?(step_key)
      status == step_key.to_s  (this is to allow validations on each step - I have no validations defined for my attachments)
    end
    

In my form when I reach the last step of the wizard to upload attachments it works fine so long as at least one of the attachments are present. However, if no attachments are present I get the following no method error on update click:

    undefined method `[]=' for nil:NilClass
    {"_method"=>"put", "authenticity_token"=>"[FILTERED]", "commit"=>"Continue", "sale_id"=>"37", "id"=>"fourth_step"}

I'm just wondering why this might be (is it something to do with the wicked gem or something else?) and is there a solution I can try? ty

DaveC
  • 59
  • 1
  • 8
  • The error occurs because you're invoking []= in `params[:sale][:status]`, but `params[:sale]` is nil, as I see it has nothing to do with any gem, but how you're modifying the values from `params` ActionController::Parameters object. – Sebastián Palma Apr 04 '21 at 16:37

2 Answers2

0

Based on the error, the value of params[:sale] is nil when no attachment is uploaded. So when you try to set params[:sale][:status], it gives undefined method []=' for nil:NilClas error. You should add a presence check on params[:sale] before assigning a value to params[:sale][:status].

Sachin Singh
  • 993
  • 8
  • 16
  • Thanks. Understand! I'm reading the Wicked wiki and it mentions "case step if...." here https://github.com/zombocom/wicked. But I don't know how the syntax would look here for a presence check on my three uploads? Thanks again. Very helpful. – DaveC Apr 04 '21 at 17:05
  • Try- `params[:sale][:status] = step.to_s if params[:sale].present?` – Sachin Singh Apr 04 '21 at 17:08
  • error param is missing or the value is empty: sale after that (though the other steps still work) – DaveC Apr 04 '21 at 17:11
0

I got this working.

If anyone just happens to stumble on this issue - on my last step page of the wizard I had only file upload fields. When I put in an additional random input field now it works fine.

Thanks for guidance.

DaveC
  • 59
  • 1
  • 8