2016-01-25 55 views
1

這是我的第一個ror應用程序。 我有主頁:home.html.erb 我有形式那裏。形式中的第一個參數不能包含零或爲空ror

<%= form_for(@lead ,:html => {:class => 'check_form'}) do |f| %> 

     <%= f.text_field :phone, placeholder: 'phone' %> 
    <%= f.submit "Check car status", class: "btn btn-large btn-primary" %> 
    <% end %> 

背景故事:一個客戶(我叫他主管可以輸入自己的電話號碼,並檢查它現在正在修理自己的汽車的狀態)

眼下這種觀點home.html.erbstatic_pages_controller

服務
class StaticPagesController < ApplicationController 
    def home 
    end 

    def help 
    end 
    def about 
    end 
    def contact 
    end 
end 

我也LeadsController

class LeadsController < ApplicationController 
    #before_action :signed_in_user, only: [:index, :edit, :update] 
    #before_action :correct_user, only: [:edit, :update] 
    before_action :admin_user,  only: [:index, :edit, :update, :destroy] 


    def index 
     @leads = Lead.paginate(page: params[:page]) 
    end 

    def destroy 
    Lead.find(params[:id]).destroy 
    flash[:success] = "record deleted." 
    redirect_to leads_url 
    end 

    def show 
    @lead = Lead.find(params[:id]) 
    end 
    def new 
    @lead = Lead.new 
    end 
    def create 
    @lead = Lead.new(lead_params) 
    if @lead.save 
     #sign_in @lead 
     flash[:success] = "Request successfully created!" 
     redirect_to @lead 
    else 
     render 'new' 
    end 
    end 

    def edit 
    @lead = Lead.find(params[:id]) 
    end 

    def update 
    @lead = Lead.find(params[:id]) 
    if @lead.update_attributes(status: params[:status]) 
     flash[:success] = "Information updated" 
     redirect_to leads_url 
    else 
     render 'edit' 
    end 
    end 



    private 

    def lead_params 
     params.require(:lead).permit(:name, :email, :phone, :car_type, 
     :car_year, :car_manufacturer, :car_model, :photo1, :photo2, :coords) 
    end 

    # Before filters 

    def signed_in_user 
     unless signed_in? 
     store_location 
     redirect_to signin_url, notice: "Please sign in." 
     end 
    end 

    def correct_user 
     @lead = Lead.find(params[:id]) 
     redirect_to(root_url) unless current_user?(@user) 
    end 
    def admin_user 
     redirect_to(root_url) unless current_user.admin? 
    end 

end 

當用戶輸入他的電話號碼查找帶有相同電話號碼的數據庫中的電話號碼並向用戶顯示修復狀態時,我想要執行的操作。 但它會產生一個錯誤: First argument in form cannot contain nil or be empty 所以我得到那form_for(@lead..這裏@lead是空的。 但是我應該怎麼做才能擺脫這個錯誤。

+0

哪裏是'@ lead'初始化初始化@lead? – Makoto

+0

@Makoto我不確定它在哪裏初始化。我爲我的leadController添加了代碼。我想現在我不初始化它。 – user2950593

回答

4

First argument in form cannot contain nil or be empty

由於您的視圖(home.html.erb)由static_pages#home服務,你應該在static_pages#home行動

def home 
    @lead = Lead.new 
end 
+0

第二個問題:我知道如何通過電話找到我的領導,就像這個'Lead.find(params [:phone])',但是我可以放置我的代碼?我應該在評論中提出這個問題還是添加另一個問題? – user2950593

+0

@ user2950593你應該把它放在'lead#create'動作:) – Pavan

+0

你的意思是在控制器中的'def create ...... end'方法嗎? – user2950593

相關問題