
[英]Routing Error undefined method `<' for nil:NilClass in rails
[英]Clearly defined Rails routing problem - undefined method for Nil:NilClass
我已經在這個問題上工作了一段時間,但仍然沒有任何高興。 這是我在這個一般領域中的第二個問題,因為最后一個問題太長了,現在定義的更加明確。
問題總結:
我正在為客戶加載頁面,但出現錯誤:
undefined method 'name' for Nil:NilClass
我的密碼
#Link on views/users/show.html.erb:
<%= link_to "Customer Account", :action => "home", :controller => "customers", :id => @user.user_type_id %>
#Regular Route:
map.connect 'customers/home/:id', :controller => 'customers', :action => 'home'
#Rake Routes, first entry:
/customers/home/:id :controller=>:"customers", :action=>"home"
#Customers Controller:
def home
render :layout => 'home'
@customer = Customer.find(params[:id])
@user = @current_user_session.user
flash[:error] = "Customer not found" and return unless @customer
@jobs = @customer.jobs
end
#views/customers/home.html.erb:
<%= @customer.name %>
我完全不知道為什么這種看似清晰的事件序列會導致NilClass。 在控制台中搜索Customer.find(2)返回正確的客戶。 這個菜鳥缺少什么? 非常感謝你。
您在設置@customer
之前渲染視圖,所以它為nil。 請嘗試以下操作:
def home
@customer = Customer.find(params[:id])
@user = @current_user_session.user
flash[:error] = "Customer not found" and return unless @customer
@jobs = @customer.jobs
render :layout => 'home'
end
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.