Ruby Rails:将引擎的功能扩展到整个应用程序
Ruby on Rails: Extend Engine's Functionality to Entire Application
所以我在 Rails 上为我的应用程序使用 Ruby,我在社区方面使用 Thredded,在身份验证方面使用 Devise。
尽管我相信这个问题不是针对任何实际的 Gem 问题,而是针对 Rails 问题的一般性 Ruby。
为了安装 Thredded Gem,我将此代码放入我的 route.rb 文件中:
mount Thredded::Engine => '/community/'
这使得 Thredded gem 在 /community/ 中完美运行。
我想在整个网站上使用 Thredded 导航。但是,我只能访问网站的 /community/ 部分中的 Thredded.current_user 或其他特定的 Thredded 变量。
有没有什么方法可以将 Thredded::Engine 安装在 /community/ 中,但也可以在该范围之外使用它的某些方面?
这是 Thredded Gem: https://github.com/thredded/thredded
这是 Thredded 导航:
<nav class="thredded--navigation" role="navigation">
<ul class="thredded--user-navigation<%= ' thredded--user-navigation-standalone' if Thredded.standalone_layout? %>">
<% if signed_in? && Thredded.standalone_layout? %>
<li class="thredded--user-navigation--profile thredded--user-navigation--item">
<%= link_to thredded_current_user, user_path(thredded_current_user) %>
</li>
<% end %>
<%= render 'thredded/shared/nav/notification_preferences', messageboard: messageboard_or_nil %>
<%= render 'thredded/shared/nav/private_topics' %>
<% if Thredded.standalone_layout? %>
<%= render 'thredded/shared/nav/standalone' %>
<% end %>
</ul>
<div class="thredded--main-navigation">
<%= yield :thredded_breadcrumbs %>
<%= render 'thredded/search/form', messageboard: messageboard_or_nil %>
</div>
</nav>
这是导航正在呈现的共享文件:https://github.com/thredded/thredded/tree/master/app/views/thredded/shared
这是 Thredded Initializer 文件:
# frozen_string_literal: true
# Thredded configuration
# ==> User Configuration
# The name of the class your app uses for your users.
# By default the engine will use 'User' but if you have another name
# for your user class - change it here.
Thredded.user_class = 'User'
# User name column, used in @mention syntax and should be unique.
# This is the column used to search for users' names if/when someone is @ mentioned.
Thredded.user_name_column = :name
# The path (or URL) you will use to link to your users' profiles.
# When linking to a user, Thredded will use this lambda to spit out
# the path or url to your user. This lambda is evaluated in the view context.
Thredded.user_path = lambda do |user|
user_path = :"#{Thredded.user_class.name.underscore}_path"
main_app.respond_to?(user_path) ? main_app.send(user_path, user) : "#{user.to_param}"
end
# This method is used by Thredded controllers and views to fetch the currently signed-in user
Thredded.current_user_method = :"current_#{Thredded.user_class.name.underscore}"
# User avatar URL. rb-gravatar gem is used by default:
Thredded.avatar_url = ->(user) { user.profile_picture.url }
# ==> Permissions Configuration
# By default, thredded uses a simple permission model, where all the users can post to all message boards,
# and admins and moderators are determined by a flag on the users table.
# The name of the moderator flag column on the users table.
Thredded.moderator_column = :admin
# The name of the admin flag column on the users table.
Thredded.admin_column = :admin
# This model can be customized further by overriding a handful of methods on the User model.
# For more information, see app/models/thredded/user_extender.rb.
# ==> Email Configuration
# Email "From:" field will use the following
# Thredded.email_from = 'no-reply@example.com'
# Incoming email will be directed to this host
# Thredded.email_incoming_host = 'example.com'
# Emails going out will prefix the "Subject:" with the following string
# Thredded.email_outgoing_prefix = '[My Forum] '
# Reply to field for email notifications
# Thredded.email_reply_to = -> postable { "#{postable.hash_id}@#{Thredded.email_incoming_host}" }
# ==> View Configuration
# Set the layout for rendering the thredded views.
Thredded.layout = 'thredded/application'
# ==> Error Handling
# By default Thredded just renders a flash alert on errors such as Topic not found, or Login required.
# Below is an example of overriding the default behavior on LoginRequired:
#
# Rails.application.config.to_prepare do
# Thredded::ApplicationController.module_eval do
# rescue_from Thredded::Errors::LoginRequired do |exception|
# @message = exception.message
# render template: 'sessions/new', status: :forbidden
# end
# end
# end
线程:
Routes for Thredded::Engine:
theme_preview GET /theme-preview(.:format) thredded/theme_previews#show
new_private_topic GET /private-topics/new(.:format) thredded/private_topics#new
private_topic GET /private-topics/:id(/page-:page)(.:format) thredded/private_topics#show {:page=>/[1-9]\d*/}
private_topic_private_posts POST /private-topics/:private_topic_id(.:format) thredded/posts#create
new_private_topic_private_post GET /private-topics/:private_topic_id/new(.:format) thredded/posts#new
edit_private_topic_private_post GET /private-topics/:private_topic_id/:id/edit(.:format) thredded/posts#edit
private_topic_private_post PATCH /private-topics/:private_topic_id/:id(.:format) thredded/posts#update
PUT /private-topics/:private_topic_id/:id(.:format) thredded/posts#update
DELETE /private-topics/:private_topic_id/:id(.:format) thredded/posts#destroy
private_topics GET /private-topics(.:format) thredded/private_topics#index
POST /private-topics(.:format) thredded/private_topics#create
edit_private_topic GET /private-topics/:id/edit(.:format) thredded/private_topics#edit
PATCH /private-topics/:id(.:format) thredded/private_topics#update
PUT /private-topics/:id(.:format) thredded/private_topics#update
DELETE /private-topics/:id(.:format) thredded/private_topics#destroy
autocomplete_users GET /autocomplete-users(.:format) thredded/autocomplete_users#index
messageboards_search GET / thredded/topics#search
messageboard_search GET /:messageboard_id(.:format) thredded/topics#search
edit_preferences GET /preferences/edit(.:format) thredded/preferences#edit
preferences PATCH /preferences(.:format) thredded/preferences#update
PUT /preferences(.:format) thredded/preferences#update
new_messageboard GET /messageboards/new(.:format) thredded/messageboards#new
edit_messageboard_preferences GET /:messageboard_id/preferences/edit(.:format) thredded/preferences#edit
messageboard_preferences PATCH /:messageboard_id/preferences(.:format) thredded/preferences#update
PUT /:messageboard_id/preferences(.:format) thredded/preferences#update
new_messageboard_topic GET /:messageboard_id/topics/new(.:format) thredded/topics#new
messageboard_topics GET /:messageboard_id(/page-:page)(.:format) thredded/topics#index {:page=>/[1-9]\d*/}
categories_messageboard_topics GET /:messageboard_id/category/:category_id(.:format) thredded/topics#category
messageboard_topic GET /:messageboard_id/:id(/page-:page)(.:format) thredded/topics#show {:page=>/[1-9]\d*/}
messageboard_topic_posts POST /:messageboard_id/:topic_id(.:format) thredded/posts#create
new_messageboard_topic_post GET /:messageboard_id/:topic_id/new(.:format) thredded/posts#new
edit_messageboard_topic_post GET /:messageboard_id/:topic_id/:id/edit(.:format) thredded/posts#edit
messageboard_topic_post PATCH /:messageboard_id/:topic_id/:id(.:format) thredded/posts#update
PUT /:messageboard_id/:topic_id/:id(.:format) thredded/posts#update
DELETE /:messageboard_id/:topic_id/:id(.:format) thredded/posts#destroy
POST /:messageboard_id(.:format) thredded/topics#create
edit_messageboard_topic GET /:messageboard_id/:id/edit(.:format) thredded/topics#edit
PATCH /:messageboard_id/:id(.:format) thredded/topics#update
PUT /:messageboard_id/:id(.:format) thredded/topics#update
DELETE /:messageboard_id/:id(.:format) thredded/topics#destroy
messageboards GET / thredded/messageboards#index
POST / thredded/messageboards#create
root GET / thredded/messageboards#index
从您的 main app
引用引擎中的导航
<%= link_to 'Link text', your_engine_name.path_name_from_the_engines_routes_path, title:"Rails engine" %>
在你的例子中
<%= link_to thredded_current_user, thredded.user_path(thredded_current_user) %>
在 main app
.
对引擎的所有引用中使用小写字母 thredded
如果您的引擎已正确安装和配置,这将起作用。我之所以提到这一点,是因为我不知道您的配置是什么,并且 rails 引擎完全依赖于约定。
所以我在 Rails 上为我的应用程序使用 Ruby,我在社区方面使用 Thredded,在身份验证方面使用 Devise。
尽管我相信这个问题不是针对任何实际的 Gem 问题,而是针对 Rails 问题的一般性 Ruby。
为了安装 Thredded Gem,我将此代码放入我的 route.rb 文件中:
mount Thredded::Engine => '/community/'
这使得 Thredded gem 在 /community/ 中完美运行。
我想在整个网站上使用 Thredded 导航。但是,我只能访问网站的 /community/ 部分中的 Thredded.current_user 或其他特定的 Thredded 变量。
有没有什么方法可以将 Thredded::Engine 安装在 /community/ 中,但也可以在该范围之外使用它的某些方面?
这是 Thredded Gem: https://github.com/thredded/thredded
这是 Thredded 导航:
<nav class="thredded--navigation" role="navigation">
<ul class="thredded--user-navigation<%= ' thredded--user-navigation-standalone' if Thredded.standalone_layout? %>">
<% if signed_in? && Thredded.standalone_layout? %>
<li class="thredded--user-navigation--profile thredded--user-navigation--item">
<%= link_to thredded_current_user, user_path(thredded_current_user) %>
</li>
<% end %>
<%= render 'thredded/shared/nav/notification_preferences', messageboard: messageboard_or_nil %>
<%= render 'thredded/shared/nav/private_topics' %>
<% if Thredded.standalone_layout? %>
<%= render 'thredded/shared/nav/standalone' %>
<% end %>
</ul>
<div class="thredded--main-navigation">
<%= yield :thredded_breadcrumbs %>
<%= render 'thredded/search/form', messageboard: messageboard_or_nil %>
</div>
</nav>
这是导航正在呈现的共享文件:https://github.com/thredded/thredded/tree/master/app/views/thredded/shared
这是 Thredded Initializer 文件:
# frozen_string_literal: true
# Thredded configuration
# ==> User Configuration
# The name of the class your app uses for your users.
# By default the engine will use 'User' but if you have another name
# for your user class - change it here.
Thredded.user_class = 'User'
# User name column, used in @mention syntax and should be unique.
# This is the column used to search for users' names if/when someone is @ mentioned.
Thredded.user_name_column = :name
# The path (or URL) you will use to link to your users' profiles.
# When linking to a user, Thredded will use this lambda to spit out
# the path or url to your user. This lambda is evaluated in the view context.
Thredded.user_path = lambda do |user|
user_path = :"#{Thredded.user_class.name.underscore}_path"
main_app.respond_to?(user_path) ? main_app.send(user_path, user) : "#{user.to_param}"
end
# This method is used by Thredded controllers and views to fetch the currently signed-in user
Thredded.current_user_method = :"current_#{Thredded.user_class.name.underscore}"
# User avatar URL. rb-gravatar gem is used by default:
Thredded.avatar_url = ->(user) { user.profile_picture.url }
# ==> Permissions Configuration
# By default, thredded uses a simple permission model, where all the users can post to all message boards,
# and admins and moderators are determined by a flag on the users table.
# The name of the moderator flag column on the users table.
Thredded.moderator_column = :admin
# The name of the admin flag column on the users table.
Thredded.admin_column = :admin
# This model can be customized further by overriding a handful of methods on the User model.
# For more information, see app/models/thredded/user_extender.rb.
# ==> Email Configuration
# Email "From:" field will use the following
# Thredded.email_from = 'no-reply@example.com'
# Incoming email will be directed to this host
# Thredded.email_incoming_host = 'example.com'
# Emails going out will prefix the "Subject:" with the following string
# Thredded.email_outgoing_prefix = '[My Forum] '
# Reply to field for email notifications
# Thredded.email_reply_to = -> postable { "#{postable.hash_id}@#{Thredded.email_incoming_host}" }
# ==> View Configuration
# Set the layout for rendering the thredded views.
Thredded.layout = 'thredded/application'
# ==> Error Handling
# By default Thredded just renders a flash alert on errors such as Topic not found, or Login required.
# Below is an example of overriding the default behavior on LoginRequired:
#
# Rails.application.config.to_prepare do
# Thredded::ApplicationController.module_eval do
# rescue_from Thredded::Errors::LoginRequired do |exception|
# @message = exception.message
# render template: 'sessions/new', status: :forbidden
# end
# end
# end
线程:
Routes for Thredded::Engine:
theme_preview GET /theme-preview(.:format) thredded/theme_previews#show
new_private_topic GET /private-topics/new(.:format) thredded/private_topics#new
private_topic GET /private-topics/:id(/page-:page)(.:format) thredded/private_topics#show {:page=>/[1-9]\d*/}
private_topic_private_posts POST /private-topics/:private_topic_id(.:format) thredded/posts#create
new_private_topic_private_post GET /private-topics/:private_topic_id/new(.:format) thredded/posts#new
edit_private_topic_private_post GET /private-topics/:private_topic_id/:id/edit(.:format) thredded/posts#edit
private_topic_private_post PATCH /private-topics/:private_topic_id/:id(.:format) thredded/posts#update
PUT /private-topics/:private_topic_id/:id(.:format) thredded/posts#update
DELETE /private-topics/:private_topic_id/:id(.:format) thredded/posts#destroy
private_topics GET /private-topics(.:format) thredded/private_topics#index
POST /private-topics(.:format) thredded/private_topics#create
edit_private_topic GET /private-topics/:id/edit(.:format) thredded/private_topics#edit
PATCH /private-topics/:id(.:format) thredded/private_topics#update
PUT /private-topics/:id(.:format) thredded/private_topics#update
DELETE /private-topics/:id(.:format) thredded/private_topics#destroy
autocomplete_users GET /autocomplete-users(.:format) thredded/autocomplete_users#index
messageboards_search GET / thredded/topics#search
messageboard_search GET /:messageboard_id(.:format) thredded/topics#search
edit_preferences GET /preferences/edit(.:format) thredded/preferences#edit
preferences PATCH /preferences(.:format) thredded/preferences#update
PUT /preferences(.:format) thredded/preferences#update
new_messageboard GET /messageboards/new(.:format) thredded/messageboards#new
edit_messageboard_preferences GET /:messageboard_id/preferences/edit(.:format) thredded/preferences#edit
messageboard_preferences PATCH /:messageboard_id/preferences(.:format) thredded/preferences#update
PUT /:messageboard_id/preferences(.:format) thredded/preferences#update
new_messageboard_topic GET /:messageboard_id/topics/new(.:format) thredded/topics#new
messageboard_topics GET /:messageboard_id(/page-:page)(.:format) thredded/topics#index {:page=>/[1-9]\d*/}
categories_messageboard_topics GET /:messageboard_id/category/:category_id(.:format) thredded/topics#category
messageboard_topic GET /:messageboard_id/:id(/page-:page)(.:format) thredded/topics#show {:page=>/[1-9]\d*/}
messageboard_topic_posts POST /:messageboard_id/:topic_id(.:format) thredded/posts#create
new_messageboard_topic_post GET /:messageboard_id/:topic_id/new(.:format) thredded/posts#new
edit_messageboard_topic_post GET /:messageboard_id/:topic_id/:id/edit(.:format) thredded/posts#edit
messageboard_topic_post PATCH /:messageboard_id/:topic_id/:id(.:format) thredded/posts#update
PUT /:messageboard_id/:topic_id/:id(.:format) thredded/posts#update
DELETE /:messageboard_id/:topic_id/:id(.:format) thredded/posts#destroy
POST /:messageboard_id(.:format) thredded/topics#create
edit_messageboard_topic GET /:messageboard_id/:id/edit(.:format) thredded/topics#edit
PATCH /:messageboard_id/:id(.:format) thredded/topics#update
PUT /:messageboard_id/:id(.:format) thredded/topics#update
DELETE /:messageboard_id/:id(.:format) thredded/topics#destroy
messageboards GET / thredded/messageboards#index
POST / thredded/messageboards#create
root GET / thredded/messageboards#index
从您的 main app
<%= link_to 'Link text', your_engine_name.path_name_from_the_engines_routes_path, title:"Rails engine" %>
在你的例子中
<%= link_to thredded_current_user, thredded.user_path(thredded_current_user) %>
在 main app
.
thredded
如果您的引擎已正确安装和配置,这将起作用。我之所以提到这一点,是因为我不知道您的配置是什么,并且 rails 引擎完全依赖于约定。