簡體   English   中英

Ruby on Rails Merit寶石搭配Devise

[英]Ruby on Rails Merit gem with Devise

我正在嘗試將Merit gem與Devise一起使用,這樣,如果用戶在Devise表單中填寫“傳記”字段,那么他們將被授予徽章。

但是,當我使用它時,它說

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

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 

優點需要一個Devise Controllers不實例化的實例變量。 您需要這樣做,如以下網址所述: https : //github.com/merit-gem/merit/wiki/How-to-grant-badges-on-user-using-Devise

我知道Devise非常適合通過幾次按鍵來設置用戶帳戶管理,但是最終很難與其他gem和功能集成。 根據我的經驗,值得一開始就額外花費一些時間來自己設置用戶身份驗證,例如按照Michael Hartl的示例http://ruby.railstutorial.org/ruby-on-rails-tutorial-book進行操作 將Merit與Devise生成的模型(例如User)結合使用時肯定存在問題,但在使用Hartl的模型后則沒有問題。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM