2015-10-13 173 views
0

我是Ruby on Rails的新手,我試圖創建一個插件。作爲第一步,我試圖用新的表單字段修改用戶帳戶頁面。到目前爲止,我有這個領域,如果我在數據庫中手動插入數據,它會被預填充,但是,我無法獲取它保存輸入的數據。我正在使用view_my_account鉤子。我的補丁中也使用了validates_presence_of驗證設置。它拋出當我提交一個空場的錯誤,和填充的字段 - 我猜,因爲現場沒有正確與@user.save在管理平臺擴展Redmine用戶模型

連接

這裏就是我有:

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> 

和原始頁面入我掛鉤(管理平臺/應用/視圖/我的/ 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)) -%> 

回答

1

新字段必須先添加到模型中。

通常在Ruby on Rails中您必須生成並運行migration,然後才能執行現場執行。

但是,Redmine支持其模型擴展,無需遷移,甚至無需編程。如果您想要將字段添加到user模型,請使用可在Redmine管理設置中添加的custom fields。立即在user表格中顯示新字段,而不重新啓動。

當然,您可以使用視圖鉤子來查看字段,並根據需要更改標準字段的標準執行。有關如何使用自定義字段值,請參閱我的answer

+0

謝謝你。我確實在數據庫中運行了一個寶貝遷移新領域。我已經閱讀過有關自定義字段的內容,但很想好好利用遷移來完成它。在那種情況下,我錯過了什麼? – djt

+0

你能顯示遷移代碼嗎?遷移是否成功並添加了字段?另外我認爲你必須在保存之前在'''​​UsersController'''中設置字段值(在''''''''''''和'''''''''方法) –

+0

用戶在@ user.safe_attributes = params [ :用戶]在'''UsersController'''中的行。也許你的領域無法獲得價值,因爲這一行 - 它內部調用delete_unsafe_attributes方法,可能會將你的價值降低爲不安全 –