创建具有多对多关系轨道的嵌套模型实例

时间:2013-06-24 13:58:04

标签: ruby-on-rails haml

我正在尝试创建一个具有多对多关系的模型。我正在努力做到这一点。

我的routes.rb:

resources :users, only: [:show] do
  resources :cals
end

模型cal.rb

has_many :cal_users
has_many :users, through: :cal_users

用户与上述内容相同,但相应地进行了修改,cal_users belongs_to

查看user/show.html.haml

= form_for(@user.cals.new, url: user_cals_path(@user)) do |f|
  = f.label :name
  = f.text_field :name
  = f.submit ("New Cal")

controller cals_controller.rb

load_and_authorize_resource
def index
  @user = User.find(params[:user_id])
  @cals = Cal.all
end
def create
  @user = User.find(params[:user_id])
  #flash[:notice] = params[:cal]
  #redirect_to root_path
  @cal = @user.cals.create(params[:cal])
  redirect_to user_cal_path(@user, @cal)

端 请注意,由于我尝试过简单的重定向到root_path

,因此我没有达到创建方法

我的错误:

undefined method `user_id=' for #<Cal id: nil, name: "asdf", created_at: nil,
updated_at: nil>

以下内容显示在我的服务器日志中:

User Load (0.3ms)  SELECT "users".* FROM "users" WHERE "users"."id" = 1 LIMIT 1
Completed 500 Internal Server Error in 15ms

我可以通过rails_admin gem成功创建cals,但它们并未链接到我的用户。

如果您需要更多信息,请询问。

修改

Started POST "/users/2/cals" for 127.0.0.1 at 2013-06-25 09:57:22 +0300
Processing by CalsController#create as HTML
  Parameters: {"utf8"=>"✓", "authenticity_token"=>"UlQ5aZSCEPdpDKfBNiqUjLIkuqoaFOVc7nNKrlZqKFY=", "cal"=>{"name"=>"asdf"}, "commit"=>"New Cal", "user_id"=>"2"}
  User Load (0.2ms)  SELECT "users".* FROM "users" WHERE "users"."id" = 2 LIMIT 1
Completed 500 Internal Server Error in 63ms

NoMethodError (undefined method `user_id=' for #<Cal id: nil, name: "asdf", created_at: nil, updated_at: nil>):
  activemodel (3.2.13) lib/active_model/attribute_methods.rb:407:in `method_missing'
  activerecord (3.2.13) lib/active_record/attribute_methods.rb:149:in `method_missing'
  cancan (1.6.10) lib/cancan/controller_resource.rb:92:in `block in assign_attributes'
  cancan (1.6.10) lib/cancan/controller_resource.rb:91:in `each'
  cancan (1.6.10) lib/cancan/controller_resource.rb:91:in `assign_attributes'
  cancan (1.6.10) lib/cancan/controller_resource.rb:86:in `build_resource'
  cancan (1.6.10) lib/cancan/controller_resource.rb:66:in `load_resource_instance'
  cancan (1.6.10) lib/cancan/controller_resource.rb:32:in `load_resource'
  cancan (1.6.10) lib/cancan/controller_resource.rb:25:in `load_and_authorize_resource'
  cancan (1.6.10) lib/cancan/controller_resource.rb:10:in `block in add_before_filter'
  activesupport (3.2.13) lib/active_support/callbacks.rb:418:in `_run__3620033272997586426__process_action__650720297067175852__callbacks'
  activesupport (3.2.13) lib/active_support/callbacks.rb:405:in `__run_callback'
  activesupport (3.2.13) lib/active_support/callbacks.rb:385:in `_run_process_action_callbacks'
  activesupport (3.2.13) lib/active_support/callbacks.rb:81:in `run_callbacks'
  actionpack (3.2.13) lib/abstract_controller/callbacks.rb:17:in `process_action'
  actionpack (3.2.13) lib/action_controller/metal/rescue.rb:29:in `process_action'
  actionpack (3.2.13) lib/action_controller/metal/instrumentation.rb:30:in `block in process_action'
  activesupport (3.2.13) lib/active_support/notifications.rb:123:in `block in instrument'
  activesupport (3.2.13) lib/active_support/notifications/instrumenter.rb:20:in `instrument'
  activesupport (3.2.13) lib/active_support/notifications.rb:123:in `instrument'
  actionpack (3.2.13) lib/action_controller/metal/instrumentation.rb:29:in `process_action'
  actionpack (3.2.13) lib/action_controller/metal/params_wrapper.rb:207:in `process_action'
  activerecord (3.2.13) lib/active_record/railties/controller_runtime.rb:18:in `process_action'
  actionpack (3.2.13) lib/abstract_controller/base.rb:121:in `process'
  actionpack (3.2.13) lib/abstract_controller/rendering.rb:45:in `process'
  actionpack (3.2.13) lib/action_controller/metal.rb:203:in `dispatch'
  actionpack (3.2.13) lib/action_controller/metal/rack_delegation.rb:14:in `dispatch'
  actionpack (3.2.13) lib/action_controller/metal.rb:246:in `block in action'
  actionpack (3.2.13) lib/action_dispatch/routing/route_set.rb:73:in `call'
  actionpack (3.2.13) lib/action_dispatch/routing/route_set.rb:73:in `dispatch'
  actionpack (3.2.13) lib/action_dispatch/routing/route_set.rb:36:in `call'
  journey (1.0.4) lib/journey/router.rb:68:in `block in call'
  journey (1.0.4) lib/journey/router.rb:56:in `each'
  journey (1.0.4) lib/journey/router.rb:56:in `call'
  actionpack (3.2.13) lib/action_dispatch/routing/route_set.rb:612:in `call'
  rack-pjax (0.7.0) lib/rack/pjax.rb:12:in `call'
  warden (1.2.1) lib/warden/manager.rb:35:in `block in call'
  warden (1.2.1) lib/warden/manager.rb:34:in `catch'
  warden (1.2.1) lib/warden/manager.rb:34:in `call'
  actionpack (3.2.13) lib/action_dispatch/middleware/best_standards_support.rb:17:in `call'
  rack (1.4.5) lib/rack/etag.rb:23:in `call'
  rack (1.4.5) lib/rack/conditionalget.rb:35:in `call'
  actionpack (3.2.13) lib/action_dispatch/middleware/head.rb:14:in `call'
  remotipart (1.0.5) lib/remotipart/middleware.rb:30:in `call'
  actionpack (3.2.13) lib/action_dispatch/middleware/params_parser.rb:21:in `call'
  actionpack (3.2.13) lib/action_dispatch/middleware/flash.rb:242:in `call'
  rack (1.4.5) lib/rack/session/abstract/id.rb:210:in `context'
  rack (1.4.5) lib/rack/session/abstract/id.rb:205:in `call'
  actionpack (3.2.13) lib/action_dispatch/middleware/cookies.rb:341:in `call'
  activerecord (3.2.13) lib/active_record/query_cache.rb:64:in `call'
  activerecord (3.2.13) lib/active_record/connection_adapters/abstract/connection_pool.rb:479:in `call'
  actionpack (3.2.13) lib/action_dispatch/middleware/callbacks.rb:28:in `block in call'
  activesupport (3.2.13) lib/active_support/callbacks.rb:405:in `_run__2837239340152142588__call__361503758903214645__callbacks'
  activesupport (3.2.13) lib/active_support/callbacks.rb:405:in `__run_callback'
  activesupport (3.2.13) lib/active_support/callbacks.rb:385:in `_run_call_callbacks'
  activesupport (3.2.13) lib/active_support/callbacks.rb:81:in `run_callbacks'
  actionpack (3.2.13) lib/action_dispatch/middleware/callbacks.rb:27:in `call'
  actionpack (3.2.13) lib/action_dispatch/middleware/reloader.rb:65:in `call'
  actionpack (3.2.13) lib/action_dispatch/middleware/remote_ip.rb:31:in `call'
  actionpack (3.2.13) lib/action_dispatch/middleware/debug_exceptions.rb:16:in `call'
  actionpack (3.2.13) lib/action_dispatch/middleware/show_exceptions.rb:56:in `call'
  railties (3.2.13) lib/rails/rack/logger.rb:32:in `call_app'
  railties (3.2.13) lib/rails/rack/logger.rb:16:in `block in call'
  activesupport (3.2.13) lib/active_support/tagged_logging.rb:22:in `tagged'
  railties (3.2.13) lib/rails/rack/logger.rb:16:in `call'
  actionpack (3.2.13) lib/action_dispatch/middleware/request_id.rb:22:in `call'
  rack (1.4.5) lib/rack/methodoverride.rb:21:in `call'
  rack (1.4.5) lib/rack/runtime.rb:17:in `call'
  activesupport (3.2.13) lib/active_support/cache/strategy/local_cache.rb:72:in `call'
  rack (1.4.5) lib/rack/lock.rb:15:in `call'
  actionpack (3.2.13) lib/action_dispatch/middleware/static.rb:63:in `call'
  railties (3.2.13) lib/rails/engine.rb:479:in `call'
  railties (3.2.13) lib/rails/application.rb:223:in `call'
  rack (1.4.5) lib/rack/content_length.rb:14:in `call'
  railties (3.2.13) lib/rails/rack/log_tailer.rb:17:in `call'
  rack (1.4.5) lib/rack/handler/webrick.rb:59:in `service'
  /home/USER/.rvm/rubies/ruby-2.0.0-p195/lib/ruby/2.0.0/webrick/httpserver.rb:138:in `service'
  /home/USER/.rvm/rubies/ruby-2.0.0-p195/lib/ruby/2.0.0/webrick/httpserver.rb:94:in `run'
  /home/USER/.rvm/rubies/ruby-2.0.0-p195/lib/ruby/2.0.0/webrick/server.rb:295:in `block in start_thread'


  Rendered /home/USER/.rvm/gems/ruby-2.0.0-p195/gems/actionpack-3.2.13/lib/action_dispatch/middleware/templates/rescues/_trace.erb (3.0ms)
  Rendered /home/USER/.rvm/gems/ruby-2.0.0-p195/gems/actionpack-3.2.13/lib/action_dispatch/middleware/templates/rescues/_request_and_response.erb (1.3ms)
  Rendered /home/USER/.rvm/gems/ruby-2.0.0-p195/gems/actionpack-3.2.13/lib/action_dispatch/middleware/templates/rescues/diagnostics.erb within rescues/layout (13.5ms)

编辑2

我发现了问题, 我的坎昆ability.rb模型错了......

can :manage, Cal, user_id: user.id
应该是:

can :manage, Cal do |cal|
  user.cals.all
end

不确定这种方法是如何RESTful的,但它确实有效。

1 个答案:

答案 0 :(得分:0)

想出了问题,我的cancan ability.rb模型错了......

can :manage, Cal, user_id: user.id
should have been:

can :manage, Cal do |cal|
  user.cals.all
end

不确定这种方法是如何RESTful的,但它确实有效。