1

我正在尝试将 Merit gem 与 Devise 一起使用,这样如果用户在 Devise 表单中填写 Biography 字段,那么他们将获得徽章。

但是,当我使用它时,它说

undefined method `biography' for true:TrueClass

这是给我错误的部分:

/models/merit/badge_rules.rb

grant_on 'registrations#update', badge: 'autobiographer', temporary: true do |user|
  user.biography.present?
end

为什么它认为用户属于 TrueClass?
/views/devise/registrations/edit.html.erb

<h2>Edit <%= resource_name.to_s.humanize %></h2>
<%= simple_form_for(resource, :as => resource_name, :url => registration_path(resource_name), :html => { :method => :put, :class => 'form-vertical' }) do |f| %>
  <%= f.error_notification %>
  <%= display_base_errors resource %>
  <%= f.input :first_name, :required => true, :autofocus => true %>
  <%= f.input :last_name, :required => true %>
  <%= f.input :username, :required => true %>
  <%= f.input :email, :required => true %>
  <%= f.input :biography %>
  <%= f.input :password, :autocomplete => "off", :hint => "leave it blank if you don't want to change it", :required => false %>
  <%= f.input :password_confirmation, :required => false %>
  <%= f.input :current_password, :hint => "we need your current password to confirm your changes", :required => true %>
  <%= f.button :submit, 'Update', :class => 'btn-primary' %>
<% end %>
<h3>Cancel my account</h3>
<p>Unhappy? <%= link_to "Cancel my account", registration_path(resource_name), :data => { :confirm => "Are you sure?" }, :method => :delete %>.</p>
<%= link_to "Back", :back %>

优点.rb

# Create application badges (uses https://github.com/norman/ambry)
badges = [
{id: 1,name: 'just-registered',image:"/images/registered.png",custom_fields:"Registered for an account"},
{id: 2,name: 'verified-user',image:"/images/verified-user.png",custom_fields:"Verified as a valid user" },
{id: 3,name: 'first-image',image:"/images/registered.png",custom_fields:"Uploaded first profile picture"},
{id: 4,name: 'first_vote',image:"/images/vote.png",custom_fields:"Voted for your first professor"},
{id: 5,name: 'autobiographer',image:"/images/biography.png",custom_fields:"Wrote a biography about yourself."}
]
badges.each do |badge|
  Merit::Badge.create!(badge)
end

4

2 回答 2

2

优点需要设计控制器不实例化的实例变量。您需要这样做,如下所述:https ://github.com/merit-gem/merit/wiki/How-to-grant-badges-on-user-using-Devise

于 2013-07-01T09:20:45.130 回答
0

我知道 Devise 非常适合通过几次击键来设置用户帐户管理,但它最终很难与其他 gem 和功能集成。根据我的经验,在开始时自己设置用户身份验证是值得的,就像遵循 Michael Hartl 的示例http://ruby.railstutorial.org/ruby-on-rails-tutorial-book一样。将 Merit 与 Devise 生成的模型(如 User)一起使用时存在一定的问题,但在使用 Hartl 后则没有。

于 2014-03-12T07:21:25.543 回答