简体   繁体   中英

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

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 "

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM