我試圖在我的模式中獲取由簡單窗體生成的窗體,但是在加載頁面時我一直遇到以下錯誤。嘗試在Rails中使用簡單窗體時未定義的方法'model_name'
undefined method 'model_name' for NilClass:Class
下面是我用嘗試生成表單
_header.html.erb(在view_pages_controller下)
<%= simple_form_for @update do |f| %>
<%= f.input :lang %>
<%= f.input :book %> #temp, just for testing simpform
<%= f.button :submit %>
<% end %>
簡單的代碼,我敢肯定問題謊言與我的控制器代碼
updates_controller.rb
class UpdatesController < ApplicationController
before_filter :signed_in_user, only: [:create, :destroy]
def create
@update = current_user.updates.build(params[:update])
if @update.save
flash[:success] = "Update successful"
redirect_to root_path
else
flash[:error] = "Failed to update, please try again"
redirect_to root_path
end
end
end
update.rb
class Update < ActiveRecord::Base
attr_accessible :book, :user_id, :lang, :round_id
belongs_to :user
end
任何幫助/提示將不勝感激。我知道我的代碼糟透了。
該模式目前在標題中,直到我可以重構它,因此視圖由ViewPages控制器生成。只是嘗試添加@update = current_user.updates.build到ViewPages主頁功能,但我只是得到「未定義的方法」更新爲零:NilClass「 – SlowBucket
這個錯誤是因爲你的'current_user'爲零。哪個控制器和哪個動作呈現視圖?通常如何獲取Update對象? – Matzi
我在sessionhelper中改變了一些東西,最終讓它工作。如果我從一開始就有正確的答案,那麼您的解決方案將立即奏效。 – SlowBucket