ruby-on-railsstrong-parametersnested-resourcesbutton-to

Not sure why strong params isn't working - param is missing or the value is empty:


hoping someone can help. I've been searching through other "param is missing" questions, but can't seem to figure out what's wrong.

In my routes file I have this nested resource "actions"

resources :jobs do
  resources :actions
end

The associated models. Ignore "action_reference". That's something else.

class Job < ActiveRecord::Base
  has_many :actions
end

class Action < ActiveRecord::Base
  belongs_to :job
  belongs_to :action_reference
end

And I'm trying to create a new action by making a POST request using button_to

Here's the ActionsController

class ActionsController < ApplicationController
  before_action :set_job
  before_action :set_action, only: [:show, :edit, :update]

  # GET /jobs/:job_id/actions/:id
  def show
  end

  # GET /jobs/:job_id/actions/new
  def new
    @action = Action.new
  end

  # GET /jobs/:job_id/actions/:id/edit
  def edit
  end

  # POST /jobs/:job_id/actions/
  def create
    @action = @job.actions.create(action_params)
    if @action.save
      flash[:success] = "Next step successfully added."
      redirect_to jobs_path
    else
      flash[:danger] = @action.errors.full_messages.join(", ")
      redirect_to new_job_action_path
    end
  end

  # PATCH to /jobs/:job_id/actions/:id
  def update
    if @action.update(action_params)
      flash[:success] = "Next step successfully updated."
      redirect_to jobs_path
    else
      flash[:danger] = @action.errors.full_messages.join(", ")
      redirect_to edit_job_action_path
    end
  end

  private
    def set_job
      @job = Job.find(params[:job_id])
    end

    def set_action
      @action = Action.find(params[:id])
    end

    def action_params
      params.require(:action).permit(:action_reference_id, :job_id, :completed_at, :next_action_date)
    end
end

And here's my button_to

<%= button_to answer[:text], post_action_jobs_path(@job), method: "post", params: { action: { action_reference_id: answer[:action_reference_id], job_id: @job_id , completed_at: answer[:action_completed_at], next_action_date: answer[:next_action_date] } }, type: "button", class: "btn btn btn-info btn-block" %>

I know the problem has something to do with the arguments I'm passing to the post_action_jobs_path in the view or the ones I'm passing to action_params in the controller, but I can't figure it out.

When I run this I get the error:

undefined method `permit' for "create":String Did you mean? print

I saw some thread a little while ago saying something about "action" being a reserved word in Rails, so you have to use something else, but if that's true I'm not sure how to go about that.

Any help would be greatly appreciated :)


Solution

  • Yes unfortunately this is due to "action" being an existing method inside rails controllers. It is used to get the name of the action that has been called. In this case "action" will equal the string "create".

    One thing you could do would be to rename you Action model to JobAction and use params.require(:job_action)