[英]Merit gem - Display a specific badge and the users who obtained it
我想創建一個方法badges#show
,例如在StackOverFlow中完成,其中顯示所選徽章以及獲得它的用戶。 我創建了一個BadgesController
,其中包含以下內容:
# Find badge
before_action :set_badge, only: %i[show]
# GET /badges/1
def show; end
private
# Set badge
def set_badge
@badge = Merit::Badge.find(params[:badge_id])
end
在merit.rb
(我只顯示下面第一個徽章的代碼):
# Use this hook to configure merit parameters
Merit.setup do |config|
# Check rules on each request or in background
# config.checks_on_each_request = true
# Define ORM. Could be :active_record (default) and :mongoid
# config.orm = :active_record
# Add application observers to get notifications when reputation changes.
# config.add_observer 'MyObserverClassName'
# Define :user_model_name. This model will be used to grand badge if no
# `:to` option is given. Default is 'User'.
# config.user_model_name = 'User'
# Define :current_user_method. Similar to previous option. It will be used
# to retrieve :user_model_name object if no `:to` option is given. Default
# is "current_#{user_model_name.downcase}".
# config.current_user_method = 'current_user'
end
Merit::Badge.create!(
id: 1,
name: 'just-registered',
description: 'New member',
custom_fields: {
icon_tag: 'user',
category: 'user'
}
)
# More badges [...]
但是,當我嘗試通過執行<%= @badge.name %>
來顯示徽章名稱時,我得到以下信息: undefined method 'name' for #<#<Class:0x000000001290cc18>:0x000000000c8c9d10>
。
我在調用badges#show方法時傳遞的參數是1 (在 url: /badges/1中)。
以下行引發相同的錯誤:
@badge = Merit::Badge.find_by_id(params[:badge_id])
@badge = Merit::Badge.find_by_id(params[:id])
@badge = Merit::Badge.find(params[:id])
如果我將@badge = Merit::Badge.find(params[:badge_id])
更改為@badge = Merit::Badge.find(:id)
我得到這個: Could not find Merit::Badge with key "id"
。
當我嘗試調試我的代碼時,視圖中的<%= debug @badge %>
會發生以下情況(使用@badge = Merit::Badge.find(params[:badge_id])
):
#<#<Class:0x000000000da762c0>:0x000000000af48a80 @keys=[1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49], @mapper=#<Ambry::Mapper:0x000000000af48f58 @klass=Merit::Badge(id, name, level, description, custom_fields), @adapter_name=:main, @indexes={}, @lock=#<Thread::Mutex:0x000000000af48d28>, @options={}, @hash={1=>{:id=>1, :name=>"just-registered", :level=>nil, :description=>"New member", :custom_fields=>{:icon_tag=>"user", :category=>"user"}} [...]
它顯示了一個 hash 以及我在初始化文件merit.rb
中擁有的所有徽章。 我不明白為什么 controller 沒有傳遞id參數。
<%= @badge.find(1).name %>
在視圖中工作正常。
我做錯了什么?
先感謝您!
我終於設法讓它工作了!
這是我的解決方案:
在badges_controller.rb
:
class BadgesController < ApplicationController
# GET /badges/1
def show; end
private
# Set badge
def set_badge
@badge = Merit::Badge.find(params[:badge_id])
@id = @badge.keys[params[:id].to_i]
@badge = @badge.find(@id)
end
end
然后在視圖show.html.erb
中,可以根據文檔調用常用方法:
<%= @badge.name %>
<%= @badge.description %>
<% @badge.users.each do |user| %>
<%= user.full_name %>
<%= (user.sash.badges_sashes[params[:id].to_i].created_at).to_formatted_s(:long) %>
<% end %>
有關信息,路徑必須是badge_path(badge.id - 1)
才能重定向到正確的徽章(數組從 0 開始)。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.