这很奇怪。但我的联系表格出现故障。我已经实施了相当多的这些表格,它违背了我的理由,为什么我会在这一行获得undefined method '[]' for nil:NilClass
<%= form_for(@inquiry, :html => {:"data-validate" => 'parsley'}) do |f| %>
以下是观点;
<%= form_for(@inquiry, :html => {:"data-validate" => 'parsley'}) do |f| %>
<%= f.hidden_field :nickname, :as => :hidden, :input_html => {:value => ""} %>
<div class="block clearfix" >
<%= f.label :name, "Name", class: "col-sm-2 control-label no-padding-right" %>
<div class="col-xs-10">
<%= f.text_field :name, class: 'col-xs-12' %>
</div>
</div><!--//form-group-->
<div class="space-4"></div>
<div class="block clearfix" >
<%= f.label :email, "Email", class: 'col-sm-2 control-label no-padding-right' %>
<div class="col-xs-10">
<%= f.text_field :email, class: 'col-xs-12' %>
</div>
</div><!--//form-group-->
<div class="space-4"></div>
<div class="block clearfix">
<%= f.label :subject, "Subject",class: "col-sm-2 control-label no-padding-right" %>
<div class="col-xs-10">
<%= f.text_field :subject, class: "col-xs-12" %>
</div>
</div><!--//form-group-->
<div class="space-4"></div>
<div class="block clearfix">
<%= f.label :message, "Message",class: "col-sm-2 control-label no-padding-right" %>
<div class="col-xs-10">
<%= f.text_area :message, class: "col-xs-12", :"parsley-maxlength"=>"1000" %>
</div>
</div><!--//form-group-->
<%= f.hidden_field :check %>
<div class="form-actions clearfix">
<%= f.submit "Send", class: "btn btn-primary col-xs-12" %>
</div>
<% end %>
和控制器;
def new
@inquiry = Inquiry.new
respond_to do |format|
format.html # new.html.erb
format.json { render json: @inquiry }
end
end
# POST /inquiries
# POST /inquiries.json
def create
@inquiry = Inquiry.new(params[:inquiry])
respond_to do |format|
if @inquiry.deliver
format.html { redirect_to @inquiry, notice: 'We will be in touch as soon as possible. Thank you!' }
format.json { render json: @inquiry, status: :created, location: @inquiry }
else
format.html { render action: "new" }
format.json { render json: @inquiry.errors, status: :unprocessable_entity }
end
end
end
和模型;
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
答案 0 :(得分:0)
试试这个: -
<%= form_for @inquiry, :html => {:data-validate => 'parsley'} do |f| %>
OR
<%= form_for @inquiry, :html => {:"data-validate" => 'parsley'} do |f| %>
答案 1 :(得分:0)
我通过改变解决了联系表格问题;
<%= 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
非常感谢。