Inherited Resources Rails 4 Update Action

2.1k views Asked by At

My application is using Rails 4 with inherited_resources. Very oddly, the update action does not appear to work. When try to submit the form on the edit page, I get this error:

ArgumentError in Admin::FaqsController#update
wrong number of arguments (6 for 1)

There are 6 fields in the form. My controller is like this:

class Admin::FaqsController < Admin::AdminController
  inherit_resources
  respond_to :html
  actions :index, :new, :create, :edit, :update, :destroy

  private

  def resource_params
    params.require(:faq).permit(:title, :slug, :body, :publish_immediately, :published_at, :status)
  end
end

Why is this happening? Is it an incompatiblity with Rails 4, and I just need to do the update action myself?

1

There are 1 answers

0
Jonah On

Seems I figured it out. I went back through the documentation and saw that I was using the params permit incorrectly. I thought it was called resource_params with a require call, but it is as follows:

class Admin::FaqsController < Admin::AdminController
  inherit_resources
  respond_to :html
  actions :index, :new, :create, :edit, :update, :destroy

  private

  def permitted_params
    params.permit(faq: [:title, :slug, :body, :publish_immediately, :published_at, :status])
  end
end

The difference is in calling the method permitted_params, and returning the entire params array with specific fields within it permitted, instead if only the resource's fields.