我是 Rails 编程的新手,我正在尝试对对象进行基本保存。我的控制器中的代码如下:
def create
@subject = Subject.new
@subject.resource_id = params[:subject][:resource_id]
@subject.student_id = params[:subject][:student_id]
@subject.baseid = params[:subject][:baseid]
@subject = params[:subject]
respond_to do |format| # line 50
if @subject.save # line 51
format.html { redirect_to @subject, :notice => 'Subject was successfully created.' }
format.json { render :json => @subject, :status => :created, :location => @subject }
else
format.html { render :action => "new" }
format.json { render :json => @subject.errors, :status => :unprocessable_entity }
end
end
end
我的视图代码如下所示:
<%= form_for(@subject) do |f| %>
<% if @subject.errors.any? %>
<div id="error_explanation">
<h2><%= pluralize(@subject.errors.count, "error") %> prohibited this subject from being saved:</h2>
<ul>
<% @subject.errors.full_messages.each do |msg| %>
<li><%= msg %></li>
<% end %>
</ul>
</div>
<% end %>
<div class="field">
<%= f.label :Subject %><br />
<%= f.text_field :name %>
</div>
<div class="field">
<%= f.label :baseid %><br />
<%= f.number_field :baseid %>
</div>
<div class="field">
<%= f.label :Resource %><br />
<%= select(:subject, :resource_id, Resource.all.collect { |r| [ r.title, r.id ] }, { :prompt => true }) %>
</div>
<div class="field">
<%= f.label :Student %><br />
<%= select(:subject, :student_id, Student.all.collect { |s| [s.name, s.id ] }, { :include_blank => true }) %>
</div>
<div class="field">
<%= f.label :Graded? %><br />
<%= f.check_box :graded %>
</div>
<div class="field">
<%= f.label :Student_scored %><br />
<%= f.check_box :studentupdate %>
</div>
<div class="field">
<%= f.label :Monday %><br />
<%= f.check_box :AssignOnMonday %>
</div>
<div class="field">
<%= f.label :Tuesday %><br />
<%= f.check_box :AssignOnTuesday %>
</div>
<div class="field">
<%= f.label :Wednesday %><br />
<%= f.check_box :AssignOnWednesday %>
</div>
<div class="field">
<%= f.label :Thursday %><br />
<%= f.check_box :AssignOnThursday %>
</div>
<div class="field">
<%= f.label :Friday %><br />
<%= f.check_box :AssignOnFriday %>
</div>
<div class="actions">
<%= f.submit %>
</div>
<% end %>
当我尝试保存时,“如果@subject.save”行出现错误,指出“保存”是一个未定义的方法。我保存任何其他对象都没有问题。它必须是我的控制器和我的视图之间的交互,但是我想不通。
生成的 HTML 如下所示:
{"utf8"=>"✓",
"authenticity_token"=>"6BNo+WhSXreJekQvRo10DSp46LBNPkxskBREZr41q+Y=",
"subject"=>{"name"=>"ttesting",
"baseid"=>"11",
"resource_id"=>"1",
"student_id"=>"1",
"graded"=>"1",
"studentupdate"=>"0",
"AssignOnMonday"=>"1",
"AssignOnTuesday"=>"1",
"AssignOnWednesday"=>"1",
"AssignOnThursday"=>"0",
"AssignOnFriday"=>"0"},
"commit"=>"Create Subject"}
和跟踪转储:
app/controllers/subjects_controller.rb:51:in `block in create'
actionpack (3.2.2) lib/action_controller/metal/mime_responds.rb:278:in `call'
actionpack (3.2.2) lib/action_controller/metal/mime_responds.rb:278:in `retrieve_collector_from_mimes'
actionpack (3.2.2) lib/action_controller/metal/mime_responds.rb:194:in `respond_to'
app/controllers/subjects_controller.rb:50:in `create'
actionpack (3.2.2) lib/action_controller/metal/implicit_render.rb:4:in `send_action'
actionpack (3.2.2) lib/abstract_controller/base.rb:167:in `process_action'
actionpack (3.2.2) lib/action_controller/metal/rendering.rb:10:in `process_action'
actionpack (3.2.2) lib/abstract_controller/callbacks.rb:18:in `block in process_action'
activesupport (3.2.2) lib/active_support/callbacks.rb:414:in `_run__4186951750656382860__process_action__4279552811083519369__callbacks'
activesupport (3.2.2) lib/active_support/callbacks.rb:405:in `__run_callback'
activesupport (3.2.2) lib/active_support/callbacks.rb:385:in `_run_process_action_callbacks'
activesupport (3.2.2) lib/active_support/callbacks.rb:81:in `run_callbacks'
actionpack (3.2.2) lib/abstract_controller/callbacks.rb:17:in `process_action'
actionpack (3.2.2) lib/action_controller/metal/rescue.rb:29:in `process_action'
actionpack (3.2.2) lib/action_controller/metal/instrumentation.rb:30:in `block in process_action'
activesupport (3.2.2) lib/active_support/notifications.rb:123:in `block in instrument'
activesupport (3.2.2) lib/active_support/notifications/instrumenter.rb:20:in `instrument'
activesupport (3.2.2) lib/active_support/notifications.rb:123:in `instrument'
actionpack (3.2.2) lib/action_controller/metal/instrumentation.rb:29:in `process_action'
actionpack (3.2.2) lib/action_controller/metal/params_wrapper.rb:205:in `process_action'
activerecord (3.2.2) lib/active_record/railties/controller_runtime.rb:18:in `process_action'
actionpack (3.2.2) lib/abstract_controller/base.rb:121:in `process'
actionpack (3.2.2) lib/abstract_controller/rendering.rb:45:in `process'
actionpack (3.2.2) lib/action_controller/metal.rb:203:in `dispatch'
actionpack (3.2.2) lib/action_controller/metal/rack_delegation.rb:14:in `dispatch'
actionpack (3.2.2) lib/action_controller/metal.rb:246:in `block in action'
actionpack (3.2.2) lib/action_dispatch/routing/route_set.rb:67:in `call'
actionpack (3.2.2) lib/action_dispatch/routing/route_set.rb:67:in `dispatch'
actionpack (3.2.2) lib/action_dispatch/routing/route_set.rb:30:in `call'
journey (1.0.3) lib/journey/router.rb:68:in `block in call'
journey (1.0.3) lib/journey/router.rb:56:in `each'
journey (1.0.3) lib/journey/router.rb:56:in `call'
actionpack (3.2.2) lib/action_dispatch/routing/route_set.rb:594:in `call'
actionpack (3.2.2) lib/action_dispatch/middleware/best_standards_support.rb:17:in `call'
rack (1.4.1) lib/rack/etag.rb:23:in `call'
rack (1.4.1) lib/rack/conditionalget.rb:35:in `call'
actionpack (3.2.2) lib/action_dispatch/middleware/head.rb:14:in `call'
actionpack (3.2.2) lib/action_dispatch/middleware/params_parser.rb:21:in `call'
actionpack (3.2.2) lib/action_dispatch/middleware/flash.rb:242:in `call'
rack (1.4.1) lib/rack/session/abstract/id.rb:205:in `context'
rack (1.4.1) lib/rack/session/abstract/id.rb:200:in `call'
actionpack (3.2.2) lib/action_dispatch/middleware/cookies.rb:338:in `call'
activerecord (3.2.2) lib/active_record/query_cache.rb:64:in `call'
activerecord (3.2.2) lib/active_record/connection_adapters/abstract/connection_pool.rb:443:in `call'
actionpack (3.2.2) lib/action_dispatch/middleware/callbacks.rb:28:in `block in call'
activesupport (3.2.2) lib/active_support/callbacks.rb:405:in `_run__665871749096871788__call__3768009419737595332__callbacks'
activesupport (3.2.2) lib/active_support/callbacks.rb:405:in `__run_callback'
activesupport (3.2.2) lib/active_support/callbacks.rb:385:in `_run_call_callbacks'
activesupport (3.2.2) lib/active_support/callbacks.rb:81:in `run_callbacks'
actionpack (3.2.2) lib/action_dispatch/middleware/callbacks.rb:27:in `call'
actionpack (3.2.2) lib/action_dispatch/middleware/reloader.rb:65:in `call'
actionpack (3.2.2) lib/action_dispatch/middleware/remote_ip.rb:31:in `call'
actionpack (3.2.2) lib/action_dispatch/middleware/debug_exceptions.rb:16:in `call'
actionpack (3.2.2) lib/action_dispatch/middleware/show_exceptions.rb:56:in `call'
railties (3.2.2) lib/rails/rack/logger.rb:26:in `call_app'
railties (3.2.2) lib/rails/rack/logger.rb:16:in `call'
actionpack (3.2.2) lib/action_dispatch/middleware/request_id.rb:22:in `call'
rack (1.4.1) lib/rack/methodoverride.rb:21:in `call'
rack (1.4.1) lib/rack/runtime.rb:17:in `call'
activesupport (3.2.2) lib/active_support/cache/strategy/local_cache.rb:72:in `call'
rack (1.4.1) lib/rack/lock.rb:15:in `call'
actionpack (3.2.2) lib/action_dispatch/middleware/static.rb:61:in `call'
railties (3.2.2) lib/rails/engine.rb:479:in `call'
railties (3.2.2) lib/rails/application.rb:220:in `call'
rack (1.4.1) lib/rack/content_length.rb:14:in `call'
railties (3.2.2) lib/rails/rack/log_tailer.rb:14:in `call'
rack (1.4.1) lib/rack/handler/webrick.rb:59:in `service'
/Users/Lon/.rbenv/versions/1.9.3-p125/lib/ruby/1.9.1/webrick/httpserver.rb:138:in `service'
/Users/Lon/.rbenv/versions/1.9.3-p125/lib/ruby/1.9.1/webrick/httpserver.rb:94:in `run'
/Users/Lon/.rbenv/versions/1.9.3-p125/lib/ruby/1.9.1/webrick/server.rb:191:in `block in start_thread'
我将不胜感激任何建议。
谢谢,
朗