嗨,我几乎是新来的,遇到了问题。
我制作了一个自定义方法来处理索引控制器,因此我可以触发创建,但作为回报,无论属性是什么,我总是为 nil 类获得 nil,即使在实例变量中返回值也是如此。
我确实尝试使用 restful rails 'create' 更改方法,但仍然返回相同的内容。
最奇怪的是……有时它会起作用。
我应该怎么办?
提前致谢。
这是我的代码:
setup_controller
skip_before_filter :authorize
def index
@countries = Country.all(:order => 'iso_code')
@users = User.all
@company = Company.new
end
def create_company
@company = Company.new(company_params)
if @company.save!
@company.create_uoms
@company.create_product_statuses
redirect_to login_path #default_uom_setup_path(company: @company)
flash[:notice] = "Thank you. Please login to proceed"
else
flash[:notice] = "Error creating company. Please try again."
redirect_to :controller => 'setup', :action => 'index'
end
end
index.html.erb
<h3>Please Enter the New Company Details</h3>
<%= form_for :company, :url => {:controller => 'setup', :action => 'create_company', :method => 'post'}, :html=>{:multipart=>true} do |form| %>
<table>
<tr><td>Name </td> <td><%= form.text_field :name %> </td></tr>
<tr><td>Address 1 </td> <td><%= form.text_field :address_1 %> </td></tr>
<tr><td>Address 2 </td> <td><%= form.text_field :address_2 %> </td></tr>
<tr><td>City </td> <td><%= form.text_field :city %> </td></tr>
<tr><td>Country </td> <td><%= form.select :country_id, @countries.collect {|c| [c.iso_code, c.id] } %> </td></tr>
<tr><td>Logo File </td> <td><%= form.file_field :logo_filename %> </td></tr>
</table>
<%= submit_tag "Create Companies" %>
<% end %>
路线
resources :setup do
collection do
post 'create_company'
get '/:company/default_uom_setup', to: 'setup#default_uom_setup', as: "default_uom"
get '/:company/create_warehouses', to: 'setup#create_warehouses', as: "create_warehouses"
end
end
公司.erb
class Company < ActiveRecord::Base
belongs_to :country
has_many :warehouses
has_many :products
has_many :customers
has_many :customer_types
has_many :purchase_order_types
has_many :purchase_orders
has_many :receipt_types
has_many :orders
has_many :order_types
has_many :product_statuses
has_many :product_categories
has_many :suppliers
has_many :uoms
has_many :assignment_types
has_many :product_subcategories
has_many :volume_uoms
has_many :quantity_uoms
has_many :weight_uoms
has_many :dimension_uoms
validates :name, uniqueness: true
validates :name, :address_1, :city, :country_id, presence: true
def default_quantity_uom
@default_quantity_uom = QuantityUom.find(self.default_quantity_uom_id)
end
def default_weight_uom
@default_weight_uom = WeightUom.find(self.default_weight_uom_id)
end
def default_length_uom
@default_length_uom = DimensionUom.find(self.default_length_uom_id)
end
def create_uoms
quantity_uoms = QuantityUom.create([ {code: "EACH", description: "Each", name: "Each", discrete: true, company_id: self.id},
{code: "CASE", description: "Case", name: "Case", discrete: true, company_id: self.id},
{code: "PALT", description: "Pallet", name: "Pallet", discrete: true, company_id: self.id},
{code: "UNIT", description: "Unit", name: "Unit", discrete: true, company_id: self.id}])
dimension_uoms = DimensionUom.create([{code: "M", description: "Meter", name: "Meter", discrete: true, company_id: self.id},
{code: "FT", description: "Foot", name: "Foot", discrete: true, company_id: self.id},
{code: "IN", description: "Inch", name: "Inch", discrete: true, company_id: self.id}])
weight_uoms = WeightUom.create([{code: "LP", description: "Pound", name: "Pound", discrete: true, company_id: self.id},
{code: "KG", description: "Kilogram", name: "Kilogram", discrete: true, company_id: self.id}])
volume_uoms = VolumeUom.create([ {code: "M3", description: "Cubic Meter", name: "Cu M", discrete: true, company_id: self.id},
{code: "FT3", description: "Cubic Foot", name: "Cu Ft", discrete: true, company_id: self.id},
{code: "I3", description: "Cubic Inch", name: "Cu In", discrete: true, company_id: self.id}])
end
def create_product_statuses
product_statuses = ProductStatus.create([
{code: "AV", description: "Available", allocatable: true, company_id: self.id },
{code: "DM", description: "Damaged", allocatable: false, company_id: self.id },
{code: "HD", description: "On-Hold", allocatable: false, company_id: self.id },
{code: "QA", description: "Quality Audit", allocatable: false, company_id: self.id }])
end
end
错误
Started POST "/setup/create_company" for 127.0.0.1 at 2013-08-14 17:23:23 +0700
Processing by SetupController#create_company as HTML
Parameters: {"utf8"=>"✓", "authenticity_token"=>"23batLaNe7Z1Xa17PsrDS/av26J/zkLnw3QN15/y6ic=", "company"=>{"name"=>"fdsfsdf", "address_1"=>"asdfasdfasdf", "address_2"=>"asdfasdfasdfadsfasd", "city"=>"asdfasdfadfasdfasdfasdfdas", "country_id"=>"1", "logo_filename"=>#<ActionDispatch::Http::UploadedFile:0xb3c14838 @tempfile=#<Tempfile:/tmp/RackMultipart20130814-11385-18dve10>, @original_filename="Domikado.png", @content_type="image/png", @headers="Content-Disposition: form-data; name=\"company[logo_filename]\"; filename=\"Domikado.png\"\r\nContent-Type: image/png\r\n">}, "commit"=>"Create Companies"}
User Load (0.8ms) SELECT `users`.* FROM `users` WHERE `users`.`id` = 1 LIMIT 1
(0.3ms) BEGIN
Company Exists (0.6ms) SELECT 1 AS one FROM `companies` WHERE `companies`.`name` = BINARY 'fdsfsdf' LIMIT 1
(0.4ms) ROLLBACK
Completed 500 Internal Server Error in 43ms
NoMethodError - undefined method `name' for nil:NilClass:
/home/googs/.rbenv/versions/2.0.0-p0/lib/ruby/2.0.0/psych/visitors/yaml_tree.rb:50:in `block in initialize'
/home/googs/.rbenv/versions/2.0.0-p0/lib/ruby/2.0.0/psych/visitors/yaml_tree.rb:52:in `block in initialize'
/home/googs/.rbenv/versions/2.0.0-p0/lib/ruby/2.0.0/psych/visitors/yaml_tree.rb:52:in `block in initialize'
/home/googs/.rbenv/versions/2.0.0-p0/lib/ruby/2.0.0/psych/visitors/yaml_tree.rb:52:in `block in initialize'
/home/googs/.rbenv/versions/2.0.0-p0/lib/ruby/2.0.0/psych/visitors/yaml_tree.rb:52:in `block in initialize'
/home/googs/.rbenv/versions/2.0.0-p0/lib/ruby/2.0.0/psych/visitors/yaml_tree.rb:128:in `accept'
/home/googs/.rbenv/versions/2.0.0-p0/lib/ruby/2.0.0/psych/visitors/yaml_tree.rb:480:in `block in dump_ivars'
/home/googs/.rbenv/versions/2.0.0-p0/lib/ruby/2.0.0/psych/visitors/yaml_tree.rb:478:in `dump_ivars'
/home/googs/.rbenv/versions/2.0.0-p0/lib/ruby/2.0.0/psych/visitors/yaml_tree.rb:150:in `visit_Object'
/home/googs/.rbenv/versions/2.0.0-p0/lib/ruby/2.0.0/psych/visitors/yaml_tree.rb:128:in `accept'
/home/googs/.rbenv/versions/2.0.0-p0/lib/ruby/2.0.0/psych/visitors/yaml_tree.rb:92:in `push'
/home/googs/.rbenv/versions/2.0.0-p0/lib/ruby/2.0.0/psych.rb:244:in `dump'
(gem) activerecord-4.0.0/lib/active_record/connection_adapters/abstract/quoting.rb:39:in `quote'
(gem) activerecord-4.0.0/lib/active_record/connection_adapters/abstract_mysql_adapter.rb:246:in `quote'
(gem) activerecord-4.0.0/lib/active_record/connection_adapters/abstract/database_statements.rb:14:in `block in to_sql'
(gem) arel-4.0.0/lib/arel/visitors/bind_visitor.rb:26:in `visit_Arel_Nodes_BindParam'
(gem) arel-4.0.0/lib/arel/visitors/visitor.rb:19:in `visit'
(gem) arel-4.0.0/lib/arel/visitors/to_sql.rb:160:in `block in visit_Arel_Nodes_Values'
(gem) arel-4.0.0/lib/arel/visitors/to_sql.rb:158:in `visit_Arel_Nodes_Values'
(gem) arel-4.0.0/lib/arel/visitors/visitor.rb:19:in `visit'
(gem) arel-4.0.0/lib/arel/visitors/to_sql.rb:123:in `visit_Arel_Nodes_InsertStatement'
(gem) arel-4.0.0/lib/arel/visitors/visitor.rb:19:in `visit'
(gem) arel-4.0.0/lib/arel/visitors/visitor.rb:5:in `accept'
(gem) arel-4.0.0/lib/arel/visitors/to_sql.rb:68:in `accept'
(gem) arel-4.0.0/lib/arel/visitors/bind_visitor.rb:11:in `accept'
(gem) activerecord-4.0.0/lib/active_record/connection_adapters/abstract/database_statements.rb:13:in `to_sql'
(gem) activerecord-4.0.0/lib/active_record/connection_adapters/abstract/database_statements.rb:95:in `insert'
(gem) activerecord-4.0.0/lib/active_record/connection_adapters/abstract/query_cache.rb:14:in `insert'
(gem) activerecord-4.0.0/lib/active_record/relation.rb:76:in `insert'
(gem) activerecord-4.0.0/lib/active_record/persistence.rb:498:in `create_record'
(gem) activerecord-4.0.0/lib/active_record/attribute_methods/dirty.rb:78:in `create_record'
(gem) activerecord-4.0.0/lib/active_record/callbacks.rb:303:in `block in create_record'
(gem) activesupport-4.0.0/lib/active_support/callbacks.rb:373:in `_run__594700878__create__callbacks'
(gem) activesupport-4.0.0/lib/active_support/callbacks.rb:80:in `run_callbacks'
(gem) activerecord-4.0.0/lib/active_record/callbacks.rb:303:in `create_record'
(gem) activerecord-4.0.0/lib/active_record/timestamp.rb:57:in `create_record'
(gem) activerecord-4.0.0/lib/active_record/persistence.rb:466:in `create_or_update'
(gem) activerecord-4.0.0/lib/active_record/callbacks.rb:299:in `block in create_or_update'
(gem) activesupport-4.0.0/lib/active_support/callbacks.rb:393:in `_run__594700878__save__callbacks'
(gem) activesupport-4.0.0/lib/active_support/callbacks.rb:80:in `run_callbacks'
(gem) activerecord-4.0.0/lib/active_record/callbacks.rb:299:in `create_or_update'
(gem) activerecord-4.0.0/lib/active_record/persistence.rb:128:in `save!'
(gem) activerecord-4.0.0/lib/active_record/validations.rb:57:in `save!'
(gem) activerecord-4.0.0/lib/active_record/attribute_methods/dirty.rb:41:in `save!'
(gem) activerecord-4.0.0/lib/active_record/transactions.rb:275:in `block in save!'
(gem) activerecord-4.0.0/lib/active_record/transactions.rb:326:in `block in with_transaction_returning_status'
(gem) activerecord-4.0.0/lib/active_record/connection_adapters/abstract/database_statements.rb:202:in `block in transaction'
(gem) activerecord-4.0.0/lib/active_record/connection_adapters/abstract/database_statements.rb:210:in `within_new_transaction'
(gem) activerecord-4.0.0/lib/active_record/connection_adapters/abstract/database_statements.rb:202:in `transaction'
(gem) activerecord-4.0.0/lib/active_record/transactions.rb:209:in `transaction'
(gem) activerecord-4.0.0/lib/active_record/transactions.rb:323:in `with_transaction_returning_status'
(gem) activerecord-4.0.0/lib/active_record/transactions.rb:275:in `save!'
app/controllers/setup_controller.rb:13:in `create_company'
(gem) actionpack-4.0.0/lib/action_controller/metal/implicit_render.rb:4:in `send_action'
(gem) actionpack-4.0.0/lib/abstract_controller/base.rb:189:in `process_action'
(gem) actionpack-4.0.0/lib/action_controller/metal/rendering.rb:10:in `process_action'
(gem) actionpack-4.0.0/lib/abstract_controller/callbacks.rb:18:in `block in process_action'
(gem) activesupport-4.0.0/lib/active_support/callbacks.rb:413:in `_run__788419911__process_action__callbacks'
(gem) activesupport-4.0.0/lib/active_support/callbacks.rb:80:in `run_callbacks'
(gem) actionpack-4.0.0/lib/abstract_controller/callbacks.rb:17:in `process_action'
(gem) actionpack-4.0.0/lib/action_controller/metal/rescue.rb:29:in `process_action'
(gem) actionpack-4.0.0/lib/action_controller/metal/instrumentation.rb:31:in `block in process_action'
(gem) activesupport-4.0.0/lib/active_support/notifications.rb:159:in `block in instrument'
(gem) activesupport-4.0.0/lib/active_support/notifications/instrumenter.rb:20:in `instrument'
(gem) activesupport-4.0.0/lib/active_support/notifications.rb:159:in `instrument'
(gem) actionpack-4.0.0/lib/action_controller/metal/instrumentation.rb:30:in `process_action'
(gem) actionpack-4.0.0/lib/action_controller/metal/params_wrapper.rb:245:in `process_action'
(gem) activerecord-4.0.0/lib/active_record/railties/controller_runtime.rb:18:in `process_action'
(gem) actionpack-4.0.0/lib/abstract_controller/base.rb:136:in `process'
(gem) actionpack-4.0.0/lib/abstract_controller/rendering.rb:44:in `process'
(gem) actionpack-4.0.0/lib/action_controller/metal.rb:195:in `dispatch'
(gem) actionpack-4.0.0/lib/action_controller/metal/rack_delegation.rb:13:in `dispatch'
(gem) actionpack-4.0.0/lib/action_controller/metal.rb:231:in `block in action'
(gem) actionpack-4.0.0/lib/action_dispatch/routing/route_set.rb:80:in `dispatch'
(gem) actionpack-4.0.0/lib/action_dispatch/routing/route_set.rb:48:in `call'
(gem) actionpack-4.0.0/lib/action_dispatch/journey/router.rb:71:in `block in call'
(gem) actionpack-4.0.0/lib/action_dispatch/journey/router.rb:59:in `call'
(gem) actionpack-4.0.0/lib/action_dispatch/routing/route_set.rb:655:in `call'
(gem) rack-1.5.2/lib/rack/etag.rb:23:in `call'
(gem) rack-1.5.2/lib/rack/conditionalget.rb:35:in `call'
(gem) rack-1.5.2/lib/rack/head.rb:11:in `call'
(gem) actionpack-4.0.0/lib/action_dispatch/middleware/params_parser.rb:27:in `call'
(gem) actionpack-4.0.0/lib/action_dispatch/middleware/flash.rb:241:in `call'
(gem) rack-1.5.2/lib/rack/session/abstract/id.rb:225:in `context'
(gem) rack-1.5.2/lib/rack/session/abstract/id.rb:220:in `call'
(gem) actionpack-4.0.0/lib/action_dispatch/middleware/cookies.rb:486:in `call'
(gem) activerecord-4.0.0/lib/active_record/query_cache.rb:36:in `call'
(gem) activerecord-4.0.0/lib/active_record/connection_adapters/abstract/connection_pool.rb:626:in `call'
(gem) actionpack-4.0.0/lib/action_dispatch/middleware/callbacks.rb:29:in `block in call'
(gem) activesupport-4.0.0/lib/active_support/callbacks.rb:373:in `_run__56629178__call__callbacks'
(gem) activesupport-4.0.0/lib/active_support/callbacks.rb:80:in `run_callbacks'
(gem) actionpack-4.0.0/lib/action_dispatch/middleware/callbacks.rb:27:in `call'
(gem) actionpack-4.0.0/lib/action_dispatch/middleware/reloader.rb:64:in `call'
(gem) actionpack-4.0.0/lib/action_dispatch/middleware/remote_ip.rb:76:in `call'
(gem) better_errors-0.8.0/lib/better_errors/middleware.rb:84:in `protected_app_call'
(gem) better_errors-0.8.0/lib/better_errors/middleware.rb:79:in `better_errors_call'
(gem) better_errors-0.8.0/lib/better_errors/middleware.rb:56:in `call'
(gem) actionpack-4.0.0/lib/action_dispatch/middleware/debug_exceptions.rb:17:in `call'
(gem) actionpack-4.0.0/lib/action_dispatch/middleware/show_exceptions.rb:30:in `call'
(gem) railties-4.0.0/lib/rails/rack/logger.rb:38:in `call_app'
(gem) railties-4.0.0/lib/rails/rack/logger.rb:21:in `block in call'
(gem) activesupport-4.0.0/lib/active_support/tagged_logging.rb:67:in `block in tagged'
(gem) activesupport-4.0.0/lib/active_support/tagged_logging.rb:25:in `tagged'
(gem) activesupport-4.0.0/lib/active_support/tagged_logging.rb:67:in `tagged'
(gem) railties-4.0.0/lib/rails/rack/logger.rb:21:in `call'
(gem) actionpack-4.0.0/lib/action_dispatch/middleware/request_id.rb:21:in `call'
(gem) rack-1.5.2/lib/rack/methodoverride.rb:21:in `call'
(gem) rack-1.5.2/lib/rack/runtime.rb:17:in `call'
(gem) activesupport-4.0.0/lib/active_support/cache/strategy/local_cache.rb:83:in `call'
(gem) rack-1.5.2/lib/rack/lock.rb:17:in `call'
(gem) actionpack-4.0.0/lib/action_dispatch/middleware/static.rb:64:in `call'
(gem) railties-4.0.0/lib/rails/engine.rb:511:in `call'
(gem) railties-4.0.0/lib/rails/application.rb:97:in `call'
(gem) rack-1.5.2/lib/rack/lock.rb:17:in `call'
(gem) rack-1.5.2/lib/rack/content_length.rb:14:in `call'
(gem) rack-1.5.2/lib/rack/handler/webrick.rb:60:in `service'
/home/googs/.rbenv/versions/2.0.0-p0/lib/ruby/2.0.0/webrick/httpserver.rb:138:in `service'
/home/googs/.rbenv/versions/2.0.0-p0/lib/ruby/2.0.0/webrick/httpserver.rb:94:in `run'
/home/googs/.rbenv/versions/2.0.0-p0/lib/ruby/2.0.0/webrick/server.rb:295:in `block in start_thread'