2014-05-11 25 views
0

這很奇怪。但是我的聯繫表單發生故障。我已經實現了相當數量的這些形式和這違揹我的推理爲什麼我在這一行未定義方法`[]'爲零:NilClass在聯繫表格

<%= form_for(@inquiry, :html => {:"data-validate" => 'parsley'}) do |f| %> 

及彼undefined method '[]' for nil:NilClass是視圖;

 <%= 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\-][email protected](?:[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

只是好奇。爲什麼你有'@ inquiry.deliver'?不應該是'@ inquiry.save'? – Pavan

+0

什麼是你的參數設置爲失敗時?它看起來像params [:inquiry]爲空。 –

+0

@Pavan,提供發送郵件。 – Acacia

回答

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 

非常感謝。

0

試試這個: -

<%= form_for @inquiry, :html => {:data-validate => 'parsley'} do |f| %> 
OR 
<%= form_for @inquiry, :html => {:"data-validate" => 'parsley'} do |f| %> 
+0

真的沒有幫助。問題出現在與控制器和模型有關的@ query中 – Acacia

相關問題