Keil Miller
Keil Miller

Reputation: 71

Rails 4 Strong Params Error

I am using rails 4. I have a model that uses validation, but does not store any records. It is only used for a web contact form that sends an email.

I am trying to use strong parameters with this controller/model. Currently I am getting a nomethoderror on my new action. Any ideas? I think it is something to do with my model not being a full blown model.?

Code slimmed down for easy viewing.

model:

class Message
  include ActiveModel::Model
  include ActiveModel::ForbiddenAttributesProtection
end

controller:

class MessagesController < ApplicationController
  def new
    @message = Message.new
  end

  private

    def project_params
      params.require(:message).permit(:name, :email, :content)
    end
end

Upvotes: 0

Views: 2337

Answers (1)

vee
vee

Reputation: 38645

Your project_params needs to be renamed to message_params since you want to allow message in your MessagesController and not project.

Please try:

class MessagesController < ApplicationController
  def new
    @message = Message.new
  end

  private

    def message_params
      params.require(:message).permit(:name, :email, :content)
    end
end

Update:

Also, although you've mentioned "code slimmed down for easy viewing", I should add that you also need to define att_accessor for those permitted attributes as follows:

class Message
  include ActiveModel::Model
  include ActiveModel::ForbiddenAttributesProtection

  attr_accessor :name, :email, :content
end

Please see Louis XIV's answer in this question: "Rails Model without a table"

Upvotes: 2

Related Questions