我是Ruby on Rails的新手,我正在尝试创建一个插件。作为第一步,我正在尝试使用新的表单字段修改“用户帐户”页面。到目前为止,我有该字段,如果我在数据库中手动插入数据,它会预先填充,但是,我无法保存它以保存输入的数据。
我正在使用view_my_account
挂钩。我还使用validates_presence_of
在我的补丁中进行了验证设置。当我提交一个空字段和一个填充字段时,它会抛出一个错误 - 我假设因为该字段未与Redmine中的@user.save
正确连接
这就是我所拥有的:
init.rb
require 'redmine'
# Patches to Redmine Core
require 'user_patch'
# Hooks
require_dependency 'account_view_hook'
Redmine::Plugin.register :myTest do
# all plugin info
end
插件/ MYTEST / LIB / user_patch.rb
require_dependency 'user'
module UserPatch
def self.included(base)
base.extend(ClassMethods)
base.send(:include, InstanceMethods)
base.class_eval do
unloadable
validates_presence_of :my_new_field
end
end
module ClassMethods
end
module InstanceMethods
end
end
ActionDispatch::Callbacks.to_prepare do
User.send(:include, UserPatch)
end
插件/ MYTEST / LIB / account_view_hook.rb
class AccountViewHook < Redmine::Hook::ViewListener
render_on :view_my_account, :partial => 'account/my_new_field'
end
插件/ MYTEST /应用/视图/帐户/ _my_new_field.html.erb
<p><%= form.text_field :my_new_field, :required => true %></p>
和我正在挂钩的原始页面(redmine / app / views / my / account.html.erb)
<div class="contextual">
<%= additional_emails_link(@user) %>
<%= link_to(l(:button_change_password), {:action => 'password'}, :class => 'icon icon-passwd') if @user.change_password_allowed? %>
<%= call_hook(:view_my_account_contextual, :user => @user)%>
</div>
<h2>
<%= avatar_edit_link(@user, :size => "50") %>
<%=l(:label_my_account)%>
</h2>
<%= error_messages_for 'user' %>
<%= labelled_form_for :user, @user,
:url => { :action => "account" },
:html => { :id => 'my_account_form',
:method => :post } do |f| %>
<div class="splitcontentleft">
<fieldset class="box tabular">
<legend><%=l(:label_information_plural)%></legend>
<p><%= f.text_field :firstname, :required => true %></p>
<p><%= f.text_field :lastname, :required => true %></p>
<p><%= f.text_field :mail, :required => true %></p>
<% unless @user.force_default_language? %>
<p><%= f.select :language, lang_options_for_select %></p>
<% end %>
<% if Setting.openid? %>
<p><%= f.text_field :identity_url %></p>
<% end %>
<% @user.custom_field_values.select(&:editable?).each do |value| %>
<p><%= custom_field_tag_with_label :user, value %></p>
<% end %>
<%= call_hook(:view_my_account, :user => @user, :form => f) %>
</fieldset>
<%= submit_tag l(:button_save) %>
</div>
<div class="splitcontentright">
<fieldset class="box">
<legend><%=l(:field_mail_notification)%></legend>
<%= render :partial => 'users/mail_notifications' %>
</fieldset>
<fieldset class="box tabular">
<legend><%=l(:label_preferences)%></legend>
<%= render :partial => 'users/preferences' %>
<%= call_hook(:view_my_account_preferences, :user => @user, :form => f) %>
</fieldset>
</div>
<% end %>
<% content_for :sidebar do %>
<%= render :partial => 'sidebar' %>
<% end %>
<% html_title(l(:label_my_account)) -%>
答案 0 :(得分:1)
必须先将新字段添加到模型中。
通常在Ruby on Rails中您必须生成并运行migration,然后才能执行字段执行。
但Redmine支持其模型扩展而无需迁移,甚至无需编程。如果您想要向user
模型添加字段,请使用可在Redmine管理设置中添加的custom fields。新字段将立即显示在user
表单中,而不会重新启动Redmine。
当然,您可以根据需要使用视图挂钩查看字段并更改字段的标准效果。有关如何使用自定义字段值,请参阅我的answer。