Ruby on Rails Merit gem与Devise

时间:2013-06-30 17:10:56

标签: ruby-on-rails ruby devise merit-gem

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

然而,当我使用它时,它说

undefined method `biography' for true:TrueClass

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 grant_on 'registrations#update', badge: 'autobiographer', temporary: true do |user| user.biography.present? end

merit.rb

<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 %>

<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 %>

2 个答案:

答案 0 :(得分:2)

merit需要一个Devise Controllers不实例化的实例变量。您需要这样做,如:https://github.com/merit-gem/merit/wiki/How-to-grant-badges-on-user-using-Devise

中所述

答案 1 :(得分:0)

我知道Devise非常适合用几次击键来设置用户帐户管理,但最终难以与其他宝石和功能集成。根据我的经验,开始时需要额外的时间来设置用户身份验证,就像遵循Michael Hartl的示例http://ruby.railstutorial.org/ruby-on-rails-tutorial-book一样。使用Merit和Devise生成的模型(如User)存在明确的问题,但在使用Hartl之后没有。