Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/ruby-on-rails-4/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ruby on rails 接触式轨道4_Ruby On Rails_Ruby On Rails 4 - Fatal编程技术网

Ruby on rails 接触式轨道4

Ruby on rails 接触式轨道4,ruby-on-rails,ruby-on-rails-4,Ruby On Rails,Ruby On Rails 4,我正在尝试为Rails 4制作一个联系表单。但是只有Rails3的教程 但我有一个错误: undefined method `name' for #<Message:0xa461fcc> 在我刚刚得到的路线中: resources :contact 这是控制器代码: class ContactController < ApplicationController def new @message = Message.new end def create

我正在尝试为Rails 4制作一个联系表单。但是只有Rails3的教程

但我有一个错误:

undefined method `name' for #<Message:0xa461fcc>
在我刚刚得到的路线中:

resources :contact
这是控制器代码:

class ContactController < ApplicationController

  def new
    @message = Message.new
  end

  def create
    @message = Message.new(contact_params)

    if @message.valid?
      NotificationsMailer.new_message(@message).deliver
      redirect_to(root_path, :notice => "Message was successfully sent.")
    else
      flash.now.alert = "Please fill all fields."
      render :new
    end
  end

  private
    # Never trust parameters from the scary internet, only allow the white list through.
    def contact_params
      params.require(:message).permit(:name, :email, :subject, :body)
    end

end
class ContactController“消息已成功发送”。)
其他的
flash.now.alert=“请填写所有字段。”
渲染:新
结束
结束
私有的
#永远不要相信来自恐怖网络的参数,只允许白名单通过。
def联系参数
参数require(:message).permit(:name,:email,:subject,:body)
结束
结束

提前谢谢

您需要将
attr\u accessor:name、:email、:subject、:and\u so\u on
添加到类中,以定义这些方法

当使用
form.label
时,Rails将尝试执行
您的消息.label
,但此方法未定义,因为您不使用活动记录,而只使用活动模型中的少量位


使用属性访问器应该可以做到这一点。

它确实有效。谢谢我认为attr_访问器仅用于Rails 3或更低版本。我想我错了。再次感谢。attr_accessor是纯ruby的,您可能会混淆了
attr_accessible
,它是rails,实际上不推荐使用rails4中的强参数。如果你完成了,请接受我的回答:)是的。我一直等到我能:)再次感谢你的解释。你说得对,我很困惑。我的错。
resources :contact
class ContactController < ApplicationController

  def new
    @message = Message.new
  end

  def create
    @message = Message.new(contact_params)

    if @message.valid?
      NotificationsMailer.new_message(@message).deliver
      redirect_to(root_path, :notice => "Message was successfully sent.")
    else
      flash.now.alert = "Please fill all fields."
      render :new
    end
  end

  private
    # Never trust parameters from the scary internet, only allow the white list through.
    def contact_params
      params.require(:message).permit(:name, :email, :subject, :body)
    end

end