Ruby on rails 未定义的方法“[]';对于nil:NilClass在联系人表单上
这很奇怪。但是我的联系方式有问题。我已经实现了很多这样的表单,这与我的推理不符,为什么我在这一行为nil:NilClass获取Ruby on rails 未定义的方法“[]';对于nil:NilClass在联系人表单上,ruby-on-rails,ruby,Ruby On Rails,Ruby,这很奇怪。但是我的联系方式有问题。我已经实现了很多这样的表单,这与我的推理不符,为什么我在这一行为nil:NilClass获取未定义的方法“[]” <%= form_for(@inquiry, :html => {:"data-validate" => 'parsley'}) do |f| %> 以及模型 class Inquiry < ActiveRecord::Base # attr_accessible :title, :body extend Ac
未定义的方法“[]”
<%= form_for(@inquiry, :html => {:"data-validate" => 'parsley'}) do |f| %>
以及模型
class Inquiry < ActiveRecord::Base
# attr_accessible :title, :body
extend ActiveModel::Naming
include ActiveModel::Conversion
include ActiveModel::Validations
include ActionView::Helpers::TextHelper
attr_accessor :name, :email, :phone_number, :message, :nickname, :subject, :check
validates :name, presence: true
validates :email, presence: true, format: { with: /\b[A-Z0-9._%a-z\-]+@(?:[A-Z0-9a-z\-]+\.)+[A-Za-z]{2,4}\z/ }
validates :message, presence: true, length: { minimum: 10, maximum: 1000 }
validates :nickname, format: { with: /^$/ }
validates :subject, presence: true, length: {maximum: 25}
validates :check, presence: false
def initialize(attributes = {})
attributes.each do |name, value|
send("#{name}=", value)
end
end
def deliver
ContactUs.contact_us(name, email, subject, message).deliver
end
def persisted?
false
end
end
类查询
试试这个:-
<%= form_for @inquiry, :html => {:data-validate => 'parsley'} do |f| %>
OR
<%= form_for @inquiry, :html => {:"data-validate" => 'parsley'} do |f| %>
{:data validate=>'parsley'}do | f |%>
或
{:“数据验证”=>“欧芹”}do | f |%>
我通过更改解决了联系方式问题
<%= form_for(@inquiry, :html => {:"data-validate" => 'parsley'}) do |f| %>
{:“数据验证”=>“欧芹”})do | f |%>
到
{:“数据验证”=>“欧芹”}do | f |%>
并将控制器更改为
class InquiriesController < ApplicationController
def new
@inquiry = Inquiry.new
end
def create
@inquiry = Inquiry.new(params[:inquiry])
if @inquiry.deliver
flash[:notice] = "Your inquiry has been sent and we will be in touch as soon as possible. Thank you!"
redirect_to action: :new
else
flash[:notice] = "Your inquiry has not been sent Thank you!"
render :new
end
end
end
类查询控制器
非常感谢。没有真正的帮助。问题在于与控制器和模型相关的@inquiry
。只是好奇而已。为什么要@inquiry.deliver
?不应该是@inquiry.save
?失败时,您的参数设置为什么?看起来params[:inquiry]为空。@Pavan,deliver用于发送邮件。
<%= form_for(@inquiry, :html => {:"data-validate" => 'parsley'}) do |f| %>
<%= form_for :inquiry, url: inquiries_path, :html => {:"data-validate" => 'parsley'} do |f| %>
class InquiriesController < ApplicationController
def new
@inquiry = Inquiry.new
end
def create
@inquiry = Inquiry.new(params[:inquiry])
if @inquiry.deliver
flash[:notice] = "Your inquiry has been sent and we will be in touch as soon as possible. Thank you!"
redirect_to action: :new
else
flash[:notice] = "Your inquiry has not been sent Thank you!"
render :new
end
end
end