活动模型禁止属性错误

时间:2014-09-15 10:06:29

标签: ruby-on-rails ruby activerecord ruby-on-rails-4

有人能够帮助我理解这个错误。我正在尝试按照构建Web应用程序教程在rails中创建联系表单。我按照步骤生成了一个消息支架。然后我修改了我的路线。接下来它说要把它放到消息控制器show动作中。

if @message.save
  flash[:notice] = 'Thanks for Your Message'
  format.html { redirect_to root_path }

我已经这样做了,我收到以下错误 MessagesController #create中的ActiveModel :: ForbiddenAttributesError ::加载ActiveModel ForbiddenAttributesError

这是我的消息控制器文件

class MessagesController < InheritedResources::Base 
  def show
     if @message.save
      flash[:notice] = 'Thanks for Your Message'
      format.html { redirect_to root_path }
     end
  end
end

我的路线文件如下

# devise_for :users
资源:产品做   资源:仅限订单:[:new,:create]   #tells rails需要产品ID号 端

  # get 'pages/payment'

  get 'home/about'

  get 'messages/new'

  get 'seller' => "products#seller"

  get 'sales' => "orders#sales"

  get 'static_pages/productlanding'

  get "content/veg"

  get "content/fruit"

  get "content/mix"

  get 'subscriptions/new'

  root 'static_pages#home'

3 个答案:

答案 0 :(得分:12)

为什么保存 show

-

<强> PARAMS

ForbiddenAttributes错误源于Rails的strong_params功能。

保存数据时,您需要通过strong_params方法将参数传递给模型。这通常通过以下设置实现:

#app/controllers/messages_controller.rb
class MessagesController < ApplicationController
   def show
      @message = Message.find(params[:id])
   end

   def new
      @message = Message.new
   end

   def create
      @message = Message.new(message_params)
      @message.save
   end

   private

   def message_params
      params.require(:message).permit(:your, :message, :params)
   end
end

这就是你的controller应该如何构建的方式。我相信,您的错误是由于缺少参数传递给模型中的属性(因此您调用@save导致尝试使用非数据填充模型)。

答案 1 :(得分:1)

奇怪。您可以在&#34; show&#34;中执行保存方法。控制器的方法,负责在单独的页面上显示内容。

您应该替换如下:

def create
  if @message.save
   flash[:notice] = 'Thanks for Your Message'
   format.html { redirect_to root_path }
  end
end

答案 2 :(得分:1)

我设法用以下方法对此进行排序!感谢所有帮助

class MessagesController < ApplicationController
  before_action :set_message, only: [:show, :edit, :update, :destroy]

  # GET /messages
  # GET /messages.json
  def index
    @messages = Message.all
  end

  # GET /messages/1
  # GET /messages/1.json
  def show
  end

  # GET /messages/new
  def new
    @message = Message.new
  end

  # GET /messages/1/edit
  def edit
  end

  # POST /messages
  # POST /messages.json
  def create
    @message = Message.new(message_params)

    respond_to do |format|
      if @message.save
        flash.now[:notice] = 'Thank you for your message!'
        format.html { redirect_to root_path }
        format.json { render :show, status: :created, location: @message }
      else
        format.html { render :new }
        format.json { render json: @message.errors, status: :unprocessable_entity }
      end
    end
  end

  # PATCH/PUT /messages/1
  # PATCH/PUT /messages/1.json
  def update
    respond_to do |format|
      if @message.update(message_params)
        format.html { redirect_to @message, notice: 'Message was successfully updated.' }
        format.json { render :show, status: :ok, location: @message }
      else
        format.html { render :edit }
        format.json { render json: @message.errors, status: :unprocessable_entity }
      end
    end
  end

  # DELETE /messages/1
  # DELETE /messages/1.json
  def destroy
    @message.destroy
    respond_to do |format|
      format.html { redirect_to messages_url, notice: 'Message was successfully destroyed.' }
      format.json { head :no_content }
    end
  end

  private
    # Use callbacks to share common setup or constraints between actions.
    def set_message
      @message = Message.find(params[:id])
    end

    .
    def message_params
      params.require(:message).permit(:name, :email, :company, :phone, :subject, :body)
    end
end