gpt4 book ai didi

ruby-on-rails - Redmine:用于添加新闻的自定义过滤器字段

转载 作者:行者123 更新时间:2023-12-02 00:13:29 24 4
gpt4 key购买 nike

我想在 Redmine 的“添加新闻”页面添加一个自定义过滤器字段,这样当我添加新新闻时,我可以选择电子邮件应该发送到的用户组。

该字段本身是一个 Redmine 用户组列表,每个用户至少分配给其中一个。

有人做过吗?任何建议将不胜感激

我找到了与该问题相关的 3 个文件:

  • /app/controller/news_controller.rb
  • /app/models/news.rb
  • /app/views/news/_form.html.erb
Environment:  Redmine version                          2.2.1.stable.11156  Ruby version                             1.8.7 (x86_64-linux)  Rails version                            3.2.11  Environment                              production  Database adapter                         MySQLRedmine plugins:  no plugin installed

So far I've done only 1 modification in Redmine, which sends added news to all registered users.File: /app/modelsmailer.rb

Overview:

http://www.lwks.com/images/custom/redmine_question.png

EDIT: Following your advice I moved mailer function to the controller:

  def create
@news = News.new(:project => @project, :author => User.current)
@news.safe_attributes = params[:news]
@news.save_attachments(params[:attachments])

if @news.save
#news_added(@news)
if params[:group]
mail :to => GroupsUser.find(params[:group][:ids]).joins(:users).select("users.mail").compact,
:subject => "[#{@news.project.name}] #{l(:label_news)}: #{@news.title}"
else
render :new
end
end
end

但我收到错误:NameError (uninitialized constant NewsController::GroupsUser): pointing to line

mail :to => GroupsUser.find

最佳答案

新闻 Controller .rb:

def new
@news = News.new
@groups = GroupsUser.all
end

新闻/_form.html.erb:

<%= label_tag :group_ids "Groups"
<%= collection_select :group, :ids, @groups, :id, :name, {}, multiple: true %>

编辑:

我将不得不对您的 Controller 的外观进行一些猜测,但我会给您一些接近的东西。根据您提供的邮件程序功能,我假设在保存 News 之后从 create Controller 中调用了它。之后我会调用邮件功能。像这样:

def create
news = News.new(params[:news]
if news.save
news_added(news)
send_mail_to_groups(params[:group][:ids]) if params[:group]
redirect_to ...
else
render :new
end
end

邮寄部分应该从news_added中移除

def news_added(news)
redmine_headers 'Project' => news.project.identifier
@author = news.author
message_id news
@news = news
@news_url = url_for(:controller => 'news', :action => 'show', :id => news)
end

支持它自己的新例程:

send_mail_to_users_by_group_ids(group_ids)
# Woo: Sent to all users, despite their email settings
mail :to => GroupsUser.find(group_ids).joins(:users).select("users.mail").compact,
:subject => "[#{@news.project.name}] #{l(:label_news)}: #{@news.title}"
end

您可能想要添加一个 where 子句以仅包含活跃用户。

我认为这是对的。我正在做这件事,所以那里可能有一两个错别字或错误。希望它能为您指明正确的方向。

关于ruby-on-rails - Redmine:用于添加新闻的自定义过滤器字段,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14339721/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com