Reputation:
I'm trying to use the Merit gem with Devise, such that if the user fills in the Biography field in the Devise form, then they would be awarded a badge.
However, when I use it, it says
undefined method `biography' for true:TrueClass
Here's the part that's giving me the error:
/models/merit/badge_rules.rb
grant_on 'registrations#update', badge: 'autobiographer', temporary: true do |user|
user.biography.present?
end
Why does it think that user is of the 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 %>
merit.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
Upvotes: 1
Views: 951
Reputation: 4382
merit needs an instance variable that Devise Controllers don't instantiate. You need to do so, as described in: https://github.com/merit-gem/merit/wiki/How-to-grant-badges-on-user-using-Devise
Upvotes: 2
Reputation: 21
I know Devise is great for setting up user account management with just a few keystrokes, but it ends up being hard to integrate with other gems and features. From my experience, it's worth the extra time in the beginning to setup user authentication yourself like following Michael Hartl's example http://ruby.railstutorial.org/ruby-on-rails-tutorial-book. There were definite problems using Merit with Devise's generated models like User, but none after using Hartl's.
Upvotes: 0