Rails参与!错误(无法批量分配受保护的属性:用户)

时间:2013-01-20 02:52:02

标签: sql ruby-on-rails ruby ruby-on-rails-3 rubygems

我正在安装Engage!

我按照此处的安装说明进行操作:http://engagecsm.com/readme

我做的安装步骤:

  1. 添加Engage!应用程序的Gemfile:gem'intembesm'。
  2. 安装它:bundle install。
  3. 运行生成器:rails generate engage user。
  4. 运行迁移:rake db:migrate。
  5. 现在,我可以去localhost:3000 /搞

    Picture of the results of doing "rake routes". It displays "engage    /engage     Engage::Engine

    这就是显示的内容:

    This is a picture of the Engage! forum, where I have entered data to be used in created a new topic. THere is a "Create Topic" submit button in the bottom right that I click to get the error in the rails server console.

    现在,当我点击“创建主题”时,网站上没有任何反应。我看看我的rails服务器控制台,我看到了:(更容易在这里查看http://i.stack.imgur.com/xKBER.png,在底部找到完整的堆栈跟踪)

    Started POST "/engage/topics" for 127.0.0.1 at 2013-01-19 19:48:26 -0700 Processing by Engage::TopicsController#create as JS  Parameters: {"utf8"=>"✓", "authenticity_token"=>"aRKQAwMsnc1DJJ62S6bb7EkhFkzuCDtNL0kV756LIP4=", "topic"=>{"style"=>"question", "title"=>"ww", "message"=>"www", "follow"=>"1"}, "commit"=>"Create Topic"}  User Load (0.3ms)  SELECT "users".* FROM "users" WHERE "users"."id" = 1 LIMIT 1  Engage::UserProfile Load (0.4ms)  SELECT "engage_user_profiles".* FROM "engage_user_profiles" WHERE "engage_user_profiles"."user_id" = 1 LIMIT 1   (0.1ms)  begin transaction  Engage::UserProfile Load (0.3ms)  SELECT "engage_user_profiles".* FROM "engage_user_profiles" INNER JOIN "users" ON "engage_user_profiles"."user_id" = "users"."id" WHERE "users"."id" = 1 LIMIT 1  SQL (1.2ms)  INSERT INTO "engage_topics" ("comments_count", "created_at", "followers_count", "message", "private", "status", "string", "style", "title", "updated_at", "user_id", "votes_count") VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)  [["comments_count", 0], ["created_at", Sun, 20 Jan 2013 02:48:26 UTC +00:00], ["followers_count", 0], ["message", "www"], ["private", false], ["status", "pending"], ["string", nil], ["style", "question"], ["title", "ww"], ["updated_at", Sun, 20 Jan 2013 02:48:26 UTC +00:00], ["user_id", 1], ["votes_count", 0]]  Engage::Following Load (0.3ms)  SELECT "engage_followings".* FROM "engage_followings" WHERE "engage_followings"."topic_id" = 7 AND "engage_followings"."user_id" = 1 LIMIT 1  User Load (0.2ms)  SELECT "users".* FROM "users" WHERE "users"."id" = 1 LIMIT 1 Binary data inserted for <code>string</code> type on column <code>token</code>  SQL (0.9ms)  INSERT INTO "engage_followings" ("created_at", "token", "topic_id", "updated_at", "user_id") VALUES (?, ?, ?, ?, ?)  [["created_at", Sun, 20 Jan 2013 02:48:26 UTC +00:00], ["token", "5813db34aaedfa6226cf77b7a17d5dfbcf8d4560"], ["topic_id", 7], ["updated_at", Sun, 20 Jan 2013 02:48:26 UTC +00:00], ["user_id", 1]]  Engage::Topic Load (0.2ms)  SELECT "engage_topics".* FROM "engage_topics" WHERE "engage_topics"."id" = 7 ORDER BY created_at DESC LIMIT 1  SQL (0.8ms)  UPDATE "engage_topics" SET "followers_count" = COALESCE("followers_count", 0) + 1 WHERE "engage_topics"."id" IN (SELECT "engage_topics"."id" FROM "engage_topics" WHERE "engage_topics"."id" = 7 ORDER BY created_at DESC)   (3.8ms)  commit transaction   (0.1ms)  begin transaction   (0.1ms)  rollback transaction Completed 500 Internal Server Error in 71msActiveModel::MassAssignmentSecurity::Error (Can't mass-assign protected attributes: user):  activemodel (3.2.11) lib/active_model/mass_assignment_security/sanitizer.rb:48:in <code>process_removed_attributes'  activemodel (3.2.11) lib/active_model/mass_assignment_security/sanitizer.rb:20:in</code>debug_protected_attribute_removal'  activemodel (3.2.11) lib/active_model/mass_assignment_security/sanitizer.rb:12:in `sanitize'

    所以我开始谷歌搜索,我看到这样的事情:

    我已将attr_accessible:user添加到我的用户模型中,但这不起作用。

    这是我的用户模型:

    class User < ActiveRecord::Base  include Engage::Extensions::User  has_many :resources  has_many :resource_views, :class_name => 'UserResourceView'  has_many :viewed_resources, :through => :resource_views, :source => :resource    has_many :evaluations, class_name: "RSEvaluation", as: :source  has_reputation :votes, source: {reputation: :votes, of: :resources}, aggregated_by: :sum  has_reputation :karma,      :source => { :reputation => :votes, :of => :resources }  def voted_for?(resource)    evaluations.where(target_type: resource.class, target_id: resource.id).present?  end    def recently_viewed_resources    viewed_resources.order('user_resource_views.created_at DESC')  end  # Include default devise modules. Others available are:  # :token_authenticatable, :confirmable,  # :lockable, :timeoutable and :omniauthable  devise :database_authenticatable, :registerable,         :recoverable, :rememberable, :trackable, :validatable  # Setup accessible (or protected) attributes for your model  attr_accessible :user_id, :email, :password, :password_confirmation, :remember_me  # attr_accessible :title, :body end

    另外,我看不到我如何访问Engage模型等,请看这里是我的目录:

    List of my directory: Controllers, Helpers, Models and Views. None of which is Engage specific. I couldn't find where Engage's controllers and such was. It is in the gem.

    有谁知道如何解决这个问题?如果您需要任何其他信息,请告诉我,我将提供截图。

    非常感谢任何帮助。谢谢!

    编辑#1 - 添加应用程序控制器图片和完整堆栈跟踪

    这是我唯一能看到Engage的地方!在控制器中(我目前没有用户控制器)

    application_controller.rb file. class ApplicationController < ActionController::Base include Engage::Extensions::Helpers  protect_from_forgery  end

    这是完整的堆栈跟踪:

    Started POST "/engage/topics" for 127.0.0.1 at 2013-01-19 19:48:26 -0700 Processing by Engage::TopicsController#create as JS  Parameters: {"utf8"=>"✓", "authenticity_token"=>"aRKQAwMsnc1DJJ62S6bb7EkhFkzuCDtNL0kV756LIP4=", "topic"=>{"style"=>"question", "title"=>"ww", "message"=>"www", "follow"=>"1"}, "commit"=>"Create Topic"}  User Load (0.3ms)  SELECT "users".* FROM "users" WHERE "users"."id" = 1 LIMIT 1  Engage::UserProfile Load (0.4ms)  SELECT "engage_user_profiles".* FROM "engage_user_profiles" WHERE "engage_user_profiles"."user_id" = 1 LIMIT 1   (0.1ms)  begin transaction  Engage::UserProfile Load (0.3ms)  SELECT "engage_user_profiles".* FROM "engage_user_profiles" INNER JOIN "users" ON "engage_user_profiles"."user_id" = "users"."id" WHERE "users"."id" = 1 LIMIT 1  SQL (1.2ms)  INSERT INTO "engage_topics" ("comments_count", "created_at", "followers_count", "message", "private", "status", "string", "style", "title", "updated_at", "user_id", "votes_count") VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)  [["comments_count", 0], ["created_at", Sun, 20 Jan 2013 02:48:26 UTC +00:00], ["followers_count", 0], ["message", "www"], ["private", false], ["status", "pending"], ["string", nil], ["style", "question"], ["title", "ww"], ["updated_at", Sun, 20 Jan 2013 02:48:26 UTC +00:00], ["user_id", 1], ["votes_count", 0]]  Engage::Following Load (0.3ms)  SELECT "engage_followings".* FROM "engage_followings" WHERE "engage_followings"."topic_id" = 7 AND "engage_followings"."user_id" = 1 LIMIT 1  User Load (0.2ms)  SELECT "users".* FROM "users" WHERE "users"."id" = 1 LIMIT 1 Binary data inserted for <code>string</code> type on column <code>token</code>  SQL (0.9ms)  INSERT INTO "engage_followings" ("created_at", "token", "topic_id", "updated_at", "user_id") VALUES (?, ?, ?, ?, ?)  [["created_at", Sun, 20 Jan 2013 02:48:26 UTC +00:00], ["token", "5813db34aaedfa6226cf77b7a17d5dfbcf8d4560"], ["topic_id", 7], ["updated_at", Sun, 20 Jan 2013 02:48:26 UTC +00:00], ["user_id", 1]]  Engage::Topic Load (0.2ms)  SELECT "engage_topics".* FROM "engage_topics" WHERE "engage_topics"."id" = 7 ORDER BY created_at DESC LIMIT 1  SQL (0.8ms)  UPDATE "engage_topics" SET "followers_count" = COALESCE("followers_count", 0) + 1 WHERE "engage_topics"."id" IN (SELECT "engage_topics"."id" FROM "engage_topics" WHERE "engage_topics"."id" = 7 ORDER BY created_at DESC)   (3.8ms)  commit transaction   (0.1ms)  begin transaction   (0.1ms)  rollback transaction Completed 500 Internal Server Error in 71ms SQL (0.9ms)  INSERT INTO "engage_followings" ("created_at", "token", "topic_id", "updated_at", "user_id") VALUES (?, ?, ?, ?, ?)  [["created_at", Sun, 20 Jan 2013 02:48:26 UTC +00:00], ["token", "5813db34aaedfa6226cf77b7a17d5dfbcf8d4560"], ["topic_id", 7], ["updated_at", Sun, 20 Jan 2013 02:48:26 UTC +00:00], ["user_id", 1]]  Engage::Topic Load (0.2ms)  SELECT "engage_topics".* FROM "engage_topics" WHERE "engage_topics"."id" = 7 ORDER BY created_at DESC LIMIT 1  SQL (0.8ms)  UPDATE "engage_topics" SET "followers_count" = COALESCE("followers_count", 0) + 1 WHERE "engage_topics"."id" IN (SELECT "engage_topics"."id" FROM "engage_topics" WHERE "engage_topics"."id" = 7 ORDER BY created_at DESC)   (3.8ms)  commit transaction   (0.1ms)  begin transaction   (0.1ms)  rollback transaction Completed 500 Internal Server Error in 71ms ActiveModel::MassAssignmentSecurity::Error (Can't mass-assign protected attributes: user):  activemodel (3.2.11) lib/active_model/mass_assignment_security/sanitizer.rb:48:in <code>process_removed_attributes'  activemodel (3.2.11) lib/active_model/mass_assignment_security/sanitizer.rb:20:in</code>debug_protected_attribute_removal'  activemodel (3.2.11) lib/active_model/mass_assignment_security/sanitizer.rb:12:in <code>sanitize'  activemodel (3.2.11) lib/active_model/mass_assignment_security.rb:230:in</code>sanitize_for_mass_assignment'  activerecord (3.2.11) lib/active_record/attribute_assignment.rb:75:in <code>assign_attributes'  activerecord (3.2.11) lib/active_record/base.rb:497:in</code>initialize'  activerecord (3.2.11) lib/active_record/reflection.rb:183:in <code>new'  activerecord (3.2.11) lib/active_record/reflection.rb:183:in</code>build_association'  activerecord (3.2.11) lib/active_record/associations/association.rb:233:in <code>build_record'  activerecord (3.2.11) lib/active_record/associations/collection_association.rb:434:in</code>block in create_record'  activerecord (3.2.11) lib/active_record/associations/collection_association.rb:149:in <code>block in transaction'  activerecord (3.2.11) lib/active_record/connection_adapters/abstract/database_statements.rb:192:in</code>transaction'  activerecord (3.2.11) lib/active_record/transactions.rb:208:in <code>transaction'  activerecord (3.2.11) lib/active_record/associations/collection_association.rb:148:in</code>transaction'  activerecord (3.2.11) lib/active_record/associations/collection_association.rb:433:in <code>create_record'  activerecord (3.2.11) lib/active_record/associations/collection_association.rb:119:in</code>create'  activerecord (3.2.11) lib/active_record/associations/collection_proxy.rb:46:in <code>create'  engagecsm (1.0.8) app/controllers/engage/topics_controller.rb:46:in</code>create'  actionpack (3.2.11) lib/action_controller/metal/implicit_render.rb:4:in <code>send_action'  actionpack (3.2.11) lib/abstract_controller/base.rb:167:in</code>process_action'  actionpack (3.2.11) lib/action_controller/metal/rendering.rb:10:in <code>process_action'  actionpack (3.2.11) lib/abstract_controller/callbacks.rb:18:in</code>block in process_action'  activesupport (3.2.11) lib/active_support/callbacks.rb:426:in `block in _run__2010401100414174750__process_action__2035169522325419882__callbacks'

    activesupport (3.2.11) lib/active_support/callbacks.rb:215:in <code>block in _conditional_callback_around_4085'  activesupport (3.2.11) lib/active_support/callbacks.rb:326:in</code>around'  activesupport (3.2.11) lib/active_support/callbacks.rb:310:in <code>_callback_around_127'  activesupport (3.2.11) lib/active_support/callbacks.rb:214:in</code>_conditional_callback_around_4085'  activesupport (3.2.11) lib/active_support/callbacks.rb:414:in <code>_run__2010401100414174750__process_action__2035169522325419882__callbacks'  activesupport (3.2.11) lib/active_support/callbacks.rb:405:in</code>__run_callback'  activesupport (3.2.11) lib/active_support/callbacks.rb:385:in <code>_run_process_action_callbacks'  activesupport (3.2.11) lib/active_support/callbacks.rb:81:in</code>run_callbacks'  actionpack (3.2.11) lib/abstract_controller/callbacks.rb:17:in <code>process_action'  actionpack (3.2.11) lib/action_controller/metal/rescue.rb:29:in</code>process_action'  actionpack (3.2.11) lib/action_controller/metal/instrumentation.rb:30:in <code>block in process_action'  activesupport (3.2.11) lib/active_support/notifications.rb:123:in</code>block in instrument'  activesupport (3.2.11) lib/active_support/notifications/instrumenter.rb:20:in <code>instrument'  activesupport (3.2.11) lib/active_support/notifications.rb:123:in</code>instrument'  actionpack (3.2.11) lib/action_controller/metal/instrumentation.rb:29:in <code>process_action'  actionpack (3.2.11) lib/action_controller/metal/params_wrapper.rb:207:in</code>process_action'  activerecord (3.2.11) lib/active_record/railties/controller_runtime.rb:18:in <code>process_action'  actionpack (3.2.11) lib/abstract_controller/base.rb:121:in</code>process'  actionpack (3.2.11) lib/abstract_controller/rendering.rb:45:in <code>process'  actionpack (3.2.11) lib/action_controller/metal.rb:203:in</code>dispatch'  actionpack (3.2.11) lib/action_controller/metal/rack_delegation.rb:14:in <code>dispatch'  actionpack (3.2.11) lib/action_controller/metal.rb:246:in</code>block in action'  actionpack (3.2.11) lib/action_dispatch/routing/route_set.rb:73:in <code>call'  actionpack (3.2.11) lib/action_dispatch/routing/route_set.rb:73:in</code>dispatch'  actionpack (3.2.11) lib/action_dispatch/routing/route_set.rb:36:in <code>call'  journey (1.0.4) lib/journey/router.rb:68:in</code>block in call'  journey (1.0.4) lib/journey/router.rb:56:in <code>each'  journey (1.0.4) lib/journey/router.rb:56:in</code>call'  actionpack (3.2.11) lib/action_dispatch/routing/route_set.rb:601:in <code>call'  railties (3.2.11) lib/rails/engine.rb:479:in</code>call'  railties (3.2.11) lib/rails/railtie/configurable.rb:30:in <code>method_missing'  journey (1.0.4) lib/journey/router.rb:68:in</code>block in call'  journey (1.0.4) lib/journey/router.rb:56:in <code>each'  journey (1.0.4) lib/journey/router.rb:56:in</code>call'actionpack (3.2.11) lib/action_dispatch/routing/route_set.rb:601:in <code>call'  warden (1.2.1) lib/warden/manager.rb:35:in</code>block in call'  warden (1.2.1) lib/warden/manager.rb:34:in <code>catch'  warden (1.2.1) lib/warden/manager.rb:34:in</code>call'  actionpack (3.2.11) lib/action_dispatch/middleware/best_standards_support.rb:17:in <code>call'  rack (1.4.3) lib/rack/etag.rb:23:in</code>call'  rack (1.4.3) lib/rack/conditionalget.rb:35:in <code>call'  actionpack (3.2.11) lib/action_dispatch/middleware/head.rb:14:in</code>call'  actionpack (3.2.11) lib/action_dispatch/middleware/params_parser.rb:21:in `call'

    actionpack (3.2.11) lib/action_dispatch/middleware/flash.rb:242:in <code>call'  rack (1.4.3) lib/rack/session/abstract/id.rb:210:in</code>context'  rack (1.4.3) lib/rack/session/abstract/id.rb:205:in <code>call'  actionpack (3.2.11) lib/action_dispatch/middleware/cookies.rb:341:in</code>call'  activerecord (3.2.11) lib/active_record/query_cache.rb:64:in <code>call'  activerecord (3.2.11) lib/active_record/connection_adapters/abstract/connection_pool.rb:479:in</code>call'  actionpack (3.2.11) lib/action_dispatch/middleware/callbacks.rb:28:in <code>block in call'  activesupport (3.2.11) lib/active_support/callbacks.rb:405:in</code>_run__4377122496359715927__call__4128007891844716680__callbacks'  activesupport (3.2.11) lib/active_support/callbacks.rb:405:in <code>__run_callback'  activesupport (3.2.11) lib/active_support/callbacks.rb:385:in</code>_run_call_callbacks'  activesupport (3.2.11) lib/active_support/callbacks.rb:81:in <code>run_callbacks'  actionpack (3.2.11) lib/action_dispatch/middleware/callbacks.rb:27:in</code>call'  actionpack (3.2.11) lib/action_dispatch/middleware/reloader.rb:65:in <code>call'  actionpack (3.2.11) lib/action_dispatch/middleware/remote_ip.rb:31:in</code>call'  actionpack (3.2.11) lib/action_dispatch/middleware/debug_exceptions.rb:16:in <code>call'  actionpack (3.2.11) lib/action_dispatch/middleware/show_exceptions.rb:56:in</code>call'  railties (3.2.11) lib/rails/rack/logger.rb:32:in <code>call_app'  railties (3.2.11) lib/rails/rack/logger.rb:16:in</code>block in call'  activesupport (3.2.11) lib/active_support/tagged_logging.rb:22:in <code>tagged'  railties (3.2.11) lib/rails/rack/logger.rb:16:in</code>call'  actionpack (3.2.11) lib/action_dispatch/middleware/request_id.rb:22:in <code>call'  rack (1.4.3) lib/rack/methodoverride.rb:21:in</code>call'  rack (1.4.3) lib/rack/runtime.rb:17:in <code>call'  activesupport (3.2.11) lib/active_support/cache/strategy/local_cache.rb:72:in</code>call'  rack (1.4.3) lib/rack/lock.rb:15:in <code>call'  actionpack (3.2.11) lib/action_dispatch/middleware/static.rb:62:in</code>call'  railties (3.2.11) lib/rails/engine.rb:479:in <code>call'  railties (3.2.11) lib/rails/application.rb:223:in</code>call'  rack (1.4.3) lib/rack/content_length.rb:14:in <code>call'  railties (3.2.11) lib/rails/rack/log_tailer.rb:17:in</code>call'  rack (1.4.3) lib/rack/handler/webrick.rb:59:in <code>service' /Users/johndoe/.rbenv/versions/1.9.3-p327-perf/lib/ruby/1.9.1/webrick/httpserver.rb:138:in</code>service' /Users/johndoe/.rbenv/versions/1.9.3-p327-perf/lib/ruby/1.9.1/webrick/httpserver.rb:94:in <code>run'  /Users/johndoe/.rbenv/versions/1.9.3-p327-perf/lib/ruby/1.9.1/webrick/server.rb:191:in</code>block in start_thread'

    编辑#2 - 添加参与环境:

    此外,我们安装了Devise(和我相信的OmniAuth ......)。因此,我们有“current_user”,但我不知道我们是否有“用户名”或“电子邮件”方法,除非这是标准的Devise或其他东西 This is the engage.rb file. Engage.configure do |config| config.layout = 'application' config.user_model = 'User' config.mailer_sender = 'engage@knowledgethief.com' config.internal_authentication = false config.current_user_method = Proc.new { current_user } config.login_link = { :path => :new_user_session_path, :opts => { :remote => true } } config.username_method = Proc.new { username } config.email_method = Proc.new { email } end

    编辑#3 - 正如Geoff建议的那样,我查看了topics_controller的第46行。该行如下:

    @ topic.votes.create(:user =&gt; engage_current_user)

4 个答案:

答案 0 :(得分:1)

根据所提供的信息,我担心我不知道发生了什么,但我可以帮助解释一下你所得到的错误。

跟踪中的最后一个非库行是:

engagecsm(1.0.8)app / controllers / engage / topics_controller.rb:46:in“create”

此代码可能位于您的默认gem路径中。如果你在* nix上,可能是/usr/lib/ruby/gems/home/user_name/.gem/ruby

在处理该行代码时,它会遇到质量分配安全性错误。

它可能会帮助您查看该行代码以查看它所遇到的模型。 请勿在此处发布该行代码。 Engage的许可证!似乎不是标准的开源软件之一,如果您在此处发布,则可能违反了许可证。我可能错了,但比抱歉更安全。

有两个变量可以通过质量分配安全性来提升你。它们是属性和角色。将attr_accessible :user添加到适当的模型只有在使用默认角色时才有帮助。如果他们使用:admin角色,则您需要添加attr_accessible :user, as: :admin。我不认为您的User模型的属性为:user,因此可能需要将其添加到其他位置。

如果我不得不猜测你做错了什么,我想你可能已经遵循了安全指南:

http://guides.rubyonrails.org/security.html

在该指南中,他们建议您默认将配置设置为白名单。

config.active_record.whitelist_attributes = true

如果你这样做并且参与!不是用MassAssignment支持写的,这可以解释你所看到的错误。

对不起,我无法确切地解释你的问题,但我希望它有所帮助。

答案 1 :(得分:1)

查看您的stack trace问题在topices_controller中 - 因此可能意味着您需要将:user_id添加到attr_accessible模型中的Engage/Topic

答案 2 :(得分:1)

它是Engage的一员!开发者在这里我可以看到Engage有两个问题!如上所述。

我们已找到第一个解决方案(您使用attr_accessible :user解决了该解决方案),因此很快就会有一个Rubygems更新。

第二个(主题实际上未显示在索引页面上的事实)与cancan gem中的更改相关联。我们仍然需要找出解决此问题的最佳方法,但快速解决方法是将Cancan版本锁定在Gemfile中,如下所示:gem 'cancan', '1.6.7'

我们会在Rubygems上提供解决这些问题的更新时通知您。

干杯!

答案 3 :(得分:0)

我需要将以下行添加到Engage / Vote Model

attr_accessible :user

现在它会说&#34;主题已成功创建。&#34;

但是,我无法看到列出的主题。但是,我相信我已经解决了这部分问题。我去

时能看到这个话题
http://localhost:3000/engage/topics/1