嗨,大家好我是Ruby on Rails的新手,當我嘗試添加新頁面時出現錯誤。當我點擊創建這是什麼出現頁面中的NoMethodError#create
當你沒有想到它時,你有一個零對象!您可能需要 Array的實例。而評估 nil.collect
class PagesController < ApplicationController
layout 'admin'#apply the admin.html layout
before_filter :confirm_logged_in #confirms if the user is logged in before entering the page
before_filter :find_subject
def index
list
render('list')#runs pages/list.html
end
def list
@pages = Page.where(:subject_id => @subject.id)#for the list view it finds the products sorted,all of them
end
def show
@page = Page.find(params[:id])#it finds the information of the product by its id and shows it in the show.html
end
def new
@page = Page.new(:subject_id => @subject.id)#it creates a new page(product) in the corresponding subject(category)
@page_count = @subject.pages.size + 1 #the quantity of total (pages)products all of them
@subjects = Subject.order('position ASC')# shows the subjects ordered ascending
end
def create
#new_position = params[:page].delete(:position)
@page = Page.new(params[:page]) #instantiate a new object using form parameters
puts "Im here"
if @page.save# save the object
#@page.move_to_position(new_position)
flash[:notice] = "Product created."
redirect_to(:action => 'list', :subject_id => @page.subject_id)# if save succeeds, redirect to the list action
else
@page_count = @subject.pages.size + 1 # products quantity is updated
@subjects = Subject.order('position ASC')
render('new') # if save fails, redisplay the form so user can fix problems
end
end
def edit
@page = Page.find(params[:id])# it find the page to be edited by its id
@page_count = @subject.pages.size
@subjects = Subject.order('position ASC')
end
def update
new_position = params[:page].delete(:position)
@page = Page.find(params[:id])# find object using parameters
if @page.update_attributes(params[:page])# update the object
@page.move_to_position(new_position)
flash[:notice] = "Page updated."
redirect_to(:action => 'show', :id => @page.id, :subject_id => @page.subject_id)# if update succeeds, redirect to the list action
else
@page_count = @subject.pages.size
@subjects = Subject.order('position ASC')
render('edit')# if save fails, redisplay the form so user can fix problems
end
end
def delete
@page = Page.find(params[:id])#it finds the page by the id to be deleted
end
def destroy
page = Page.find(params[:id])#it finds the page by the id to be destroyed
page.move_to_position(nil)
page.destroy
flash[:notice] = "Product destroyed."
redirect_to(:action => 'list', :subject_id => @subject.id)# redirect the user to the pages/list.html according to the corresponding subject(category)
end
private
def find_subject
if params[:subject_id]
@subject = Subject.find_by_id(params[:subject_id])
end
end
end
我可以在任何沒有問題添加主題和我可以毫不問題,問題是網頁添加管理員用戶發生錯誤。
class SubjectsController < ApplicationController
layout 'admin'#apply the admin.html layout
before_filter :confirm_logged_in #confirms if the user is logged in before entering the page
def index
list
render('list')#runs subjects/list.html all list.html are different
end
def list
@subjects = Subject.order("subjects.position ASC")#for the list view it finds the categories sorted,all of them
end
def show
@subject = Subject.find(params[:id])
end
def new
@subject = Subject.new # creates a new subject(category)
@subject_count = Subject.count + 1
end
def create
new_position = params[:subject].delete(:position)
@subject = Subject.new(params[:subject]) # instantiate a new object using form parameters
if @subject.save #save the object
@subject.move_to_position(new_position)
flash[:notice] = "Category created."
redirect_to(:action => 'list')# if save succeeds, redirect to list.html
else
@subject_count = Subject.count + 1
render('new')#If save fails, redisplay the form so user can fix problems
end
end
def edit
@subject = Subject.find(params[:id])# it finds the subject to be edited
@subject_count = Subject.count
end
def update
new_position = params[:subject].delete(:position)#if updated it delete the current position
@subject = Subject.find(params[:id])# find object using form parameters
if @subject.update_attributes(params[:subject])#update the object(subject)
@subject.move_to_position(new_position)#new position assigned
flash[:notice] = "Subject updated."
redirect_to(:action => 'show', :id => @subject.id)# if update succeeds, redirect to the list action
else
@subject_count = Subject.count
render('edit')# if save fails, redisplay the form so user can fix problems
end
end
def delete
@subject = Subject.find(params[:id])#it finds thesubjectby the id to be deleted
end
def destroy
subject = Subject.find(params[:id])#it finds the Subject(category)by the id to be destroyed
subject.move_to_position(nil)
subject.destroy
flash[:notice] = "Subject destroyed."
redirect_to(:action => 'list')# redirected to the list.html
end
end
這是假定誤差根據所述框架 提取的源被happenign的_form.html.erb(圍繞線#6):
3: <table summary="Products form fields">
4: <tr>
5: <th><%= f.label(:subject_id, "Category") %></th>
6: <td><%= f.select(:subject_id, @subjects.collect {|s| [s.name, s.id]}) %></td> </tr>
<%= error_messages_for(@pages) %>
<table summary="Products form fields">
<tr>
<th><%= f.label(:subject_id, "Category") %></th>
<td><%= f.select(:subject_id, @subjects.collect {|s| [s.name, s.id]}) %></td> </tr>
<tr>
<th><%= f.label(:product_name) %></th>
<td><%= f.text_field(:product_name) %></td>
</tr>
<tr>
<th><%= f.label(:description) %></th>
<td><%= f.text_area(:description) %></td>
</tr>
<tr>
<th><%= f.label(:min_description) %></th>
<td><%= f.text_area(:min_description) %></td>
</tr>
<tr>
<th><%= f.label(:brand) %></th>
<td><%= f.text_field(:brand) %></td>
<tr>
<th><%= f.label(:quantity) %></th>
<td><%= f.text_field(:quantity) %></td>
</tr>
<tr>
<th><%= f.label(:price) %></th>
<td><%= f.text_field(:price) %></td>
</tr>
<tr>
<th><%= f.label(:size) %></th>
<td><%= f.text_field(:size) %></td>
</tr>
<tr>
<th><%= f.label(:head_size) %></th>
<td><%= f.text_field(:head_size) %></td>
</tr>
<tr>
<th><%= f.label(:racquets_weight) %></th>
<td><%= f.text_field(:racquets_weight) %></td>
</tr>
<tr>
<th><%= f.label(:category) %></th>
<td><%= f.text_field(:category) %></td>
</tr>
<tr>
<th><%= f.label(:image_url) %></th>
<td><%= f.text_field(:image_url) %></td>
</tr>
</table>
框架跟蹤
actionpack(3.0.9)lib/action_view/template.rb:135:
send' actionpack (3.0.9) lib/action_view/template.rb:135:in
render' activesupport(3.0.9)lib/active_support/notifications.rb:54:instrument' actionpack (3.0.9) lib/action_view/template.rb:127:in
渲染 'ActionPack的(3.0.9)LIB/ACTION_VIEW /渲染/ partials.rb:333:在render_partial' actionpack (3.0.9) lib/action_view/render/partials.rb:262:in
渲染' 的ActiveSupport (3.0.9)LIB/active_support/notifications.rb:52:在instrument' activesupport (3.0.9) lib/active_support/notifications/instrumenter.rb:21:in
儀器」 的ActiveSupport (3.0.9)lib/active_support/notifications.rb:52:ininstrument' actionpack (3.0.9) lib/action_view/render/partials.rb:260:in
render'actionpack(3.0.9) lib/action_view/render/partials.rb:378:in_render_partial' actionpack (3.0.9) lib/action_view/render/rendering.rb:22:in
render'actionpack (3.0.9) lib/action_view/helpers/capture_helper.rb:40:在capture' actionpack (3.0.9) lib/action_view/helpers/form_helper.rb:545:in
fields_for' actionpack(3.0.9)lib/action_view/40:在capture' actionpack (3.0.9) lib/action_view/helpers/capture_helper.rb:172:in
with_output_buffer'actionpack(3.0.9) lib/action_view/helpers/capture_helper.rb:幫手/ form_helper.rb:320:在0123中(3.0.9)lib/action_view/template.rb:127:render' actionpack (3.0.9) lib/action_view/render/rendering.rb:59:in
_render_template'activesupport(3.0.9)lib/action_view/template.rb:135:render' activesupport (3.0.9) lib/active_support/notifications.rb:54:in
) lib/active_support/notifications.rb:52:ininstrument' activesupport (3.0.9) lib/active_support/notifications/instrumenter.rb:21:in
instrument'activesupport(3.0.9) lib/active_support/notifications.rb:52:ininstrument' actionpack (3.0.9) lib/action_view/render/rendering.rb:56:in
_render_template' actionpack(3.0.9)lib/action_view/render/render.rb:26:render' actionpack (3.0.9) lib/abstract_controller/rendering.rb:115:in
_render_template'actionpack(3.0.9) lib/abstract_controller/rendering.rb:109:render_to_body' actionpack (3.0.9) lib/action_controller/metal/renderers.rb:47:in
render_to_body'actionpack(3.0.9) lib/action_controller/metal/compatibility.rb:55:在render_to_body' actionpack (3.0.9) lib/abstract_controller/rendering.rb:102:in
render_to_strin g'actionpack(3.0.9) lib/abstract_controller/rendering.rb:93:inrender' actionpack (3.0.9) lib/action_controller/metal/rendering.rb:17:in
render'actionpack (3.0.9)lib/action_controller/metal/instrumentation.rb:40:inrender' activesupport (3.0.9) lib/active_support/core_ext/benchmark.rb:5:in
ms'c:/ RailsInstaller /Ruby1.8.7/lib/ruby/1.8/benchmark.rb:308:inrealtime' activesupport (3.0.9) lib/active_support/core_ext/benchmark.rb:5:in
ms'actionpack(3.0.9) lib/action_controller/metal/instrumentation.rb:40:inrender' actionpack (3.0.9) lib/action_controller/metal/instrumentation.rb:78:in
cleanup_view_runtime'activerecord(3.0.9 ) LIB/active_record/railties/controller_runtime.rb:15:在cleanup_view_runtime' actionpack (3.0.9) lib/action_controller/metal/instrumentation.rb:39:in
渲染 ' ActionPack的(3.0.9)LIB/action_controller /金屬/ implicit_render.rb:4:send_action' actionpack (3.0.9) lib/action_controller/metal/implicit_render.rb:4:in
send_action' ActionPack的(3.0。9)LIB/abstract_controller/base.rb:150:在process_action' actionpack (3.0.9) lib/action_controller/metal/rendering.rb:11:in
process_action」 ActionPack的(3.0.9)LIB/abstract_controller/callbacks.rb:18:在process_action' activesupport (3.0.9) lib/active_support/callbacks.rb:446:in
運行 _367793087__process_action_ _callbacks'的ActiveSupport (3.0 .9)lib/active_support/callbacks.rb:410:send' activesupport (3.0.9) lib/active_support/callbacks.rb:410:in
_run_process_action_callbacks'activesupport(3.0.9) lib/active_support/callbacks.rb:94:insend' activesupport (3.0.9) lib/active_support/callbacks.rb:94:in
run_callbacks'actionpack (3.0.9)lib/abstract_controller/callbacks .rb:17:process_action' actionpack (3.0.9) lib/action_controller/metal/instrumentation.rb:30:in
process_action' activesupport(3.0.9)lib/active_support/notifications.rb:52:ininstrument' activesupport (3.0.9) lib/active_support/notifications/instrumenter.rb:21:in
儀器 ' 的ActiveSupport(3.0.9)LIB/active_support/notifications.rb:52:在instrument' actionpack (3.0.9) lib/action_controller/metal/instrumentation.rb:29:in
process_action' ActionPack的(3.0.9)LIB/action_controller /金屬/ rescue.rb:17:在process_action' actionpack (3.0.9) lib/abstract_controller/base.rb:119:in
過程ActionPack的(3.0.9) lib/abstract_controller/rendering.rb:41:inprocess' actionpack (3.0.9) lib/action_controller/metal.rb:138:in
dispatch'actionpack (3.0.9)lib/action_controller/metal/rack_delegation.rb:14:indispatch' actionpack (3.0.9) lib/action_controller/metal.rb:178:in
action'actionpack(3.0.9) lib/action_dispatch/routing/route_set.rb:62:call' actionpack (3.0.9) lib/action_dispatch/routing/route_set.rb:62:in
dispatch' actionpack(3.0.9)lib/action_dispatch/routing/route_set.rb:27:incall' rack-mount (0.6.14) lib/rack/mount/route_set.rb:148:in
call' 機架安裝(0.6.14)lib/rack/mount/code_generation.rb:93:在recognize' rack-mount (0.6.14) lib/rack/mount/code_generation.rb:75:in
optimized_each'機架安裝 (0.6.14)lib/rack/mount/code_generation.rb:92:在recognize' rack-mount (0.6.14) lib/rack/mount/route_set.rb:139:in
調用' ActionPack的(3.0.9)LIB/action_dispatch /路由/ route_set.rb:493:在call' actionpack (3.0.9) lib/action_dispatch/middleware/best_standards_support.rb:17:in
呼叫' ActionPack的(3.0.9)LIB/action_dispatch /中間件/ head.rb:14:call' rack (1.2.8) lib/rack/methodoverride.rb:24:in
呼叫」 ActionPack的 (3.0.9)lib/action_dispatch/middleware/params_parser.rb:21:在call' actionpack (3.0.9) lib/action_dispatch/middleware/flash.rb:182:in
調用'actionpack(3.0.9) lib/action_dispatch/middleware/session/abstract_store.rb:149:在call' actionpack (3.0.9) lib/action_dispatch/middleware/cookies.rb:302:in
調用'activerecord(3.0。 9)lib/active_record/query_cache.rb:32:在call' activerecord (3.0.9) lib/active_record/connection_adapters/abstract/query_cache.rb:28:in
cache'active記錄(3.0.9)LIB/active_record/query_cache.rb:12:cache' activerecord (3.0.9) lib/active_record/query_cache.rb:31:in
呼叫的ActiveRecord(3.0.9) LIB/active_record/connection_adapters /抽象/ connection_pool.rb:354:在call' actionpack (3.0.9) lib/action_dispatch/middleware/callbacks.rb:46:in
呼叫'的ActiveSupport ( (1.2.8)lib/rack/sendfile.rb:106:在call' actionpack (3.0.9) lib/action_dispatch/middleware/remote_ip.rb:48:in
中調用' actionpack(3.0.9) lib/action_dispatch/(1.2.8)lib/rack/runtime.rb:17:在call' activesupport (3.0.9) lib/active_support/cache/strategy/local_cache.rb:72:in
中調用'rack (1.2.8)lib/rack/lock.rb:在call' rack (1.2.8) lib/rack/lock.rb:13:in
同步'rack(1.2.8) lib/rack/lock.rb:13:在(3.0.9) lib/rails/application.rb:168:在call' railties (3.0.9) lib/rails/application.rb:77:in
發送'railties ) LIB /齒條/ content_length.rb:13:在call' rack (1.2.8) lib/rack/handler/webrick.rb:52:in
服務 ' C:/RailsInstaller/Ruby1.8.7/lib/ruby/1.8/webrick/httpserver.rb:104:在service' c:/RailsInstaller/Ruby1.8.7/lib/ruby/1.8/webrick/httpserver.rb:65:in
運行' C:/ RailsInstaller /Ruby1.8.7/lib/ruby/1.8/webrick/server.rb:173:instart_thread' c:/RailsInstaller/Ruby1.8.7/lib/ruby/1.8/webrick/server.rb:162:in
開始' c:/RailsInstaller/Ruby1.8.7/lib/ruby/1.8/webrick/server.rb:162:在start_thread' c:/RailsInstaller/Ruby1.8.7/lib/ruby/1.8/webrick/server.rb:95:in
開始' c:/RailsInstaller/Ruby1.8.7/lib/ruby/1.8/webrick/server。rb:92:ineach' c:/RailsInstaller/Ruby1.8.7/lib/ruby/1.8/webrick/server.rb:92:in
start' c:/RailsInstaller/Ruby1.8.7/lib/ruby/1.8/webrick/server.rb:23:instart' c:/RailsInstaller/Ruby1.8.7/lib/ruby/1.8/webrick/server.rb:82:in
start'rack(1.2.8)lib/rack/handler/webrick.rb:13:在run' rack (1.2.8) lib/rack/server.rb:217:in
開始'railties(3.0.9) lib/rails/commands/server.rb:65:instart' railties (3.0.9) lib/rails/commands.rb:30 railties (3.0.9) lib/rails/commands.rb:27:in
tap'railties(3.0.9)lib/rails/commands.rb:27 script /軌道:6:在 `要求'腳本/導軌:6
任何形式的幫助讚賞。
建議:輸入「rails g scaffold page title:string content:text」並將您的代碼與腳手架的代碼進行比較。它會幫助你理解軌道。 – emrahbasman