2012-01-14 4 views
0

私は、複数の登録(診断、患者、検査室検査、サービス、クライアント、ユーザー、サプライヤ)。最初に、これらはデータベースをシードすることによって作成されます。 2.現在Iでデータ入力ユーザRails 3 - 外部クラスのbefore_saveコールバックのcheck_box_tag(モデルの一部ではない)に基づく説明の変更方法

によって指定された - の要件のいずれか 1.アプリケーション(まだ決定するために、いくつかの構成設定)によって指定された記述コードを混合するためのケース(大文字最初の単語)であります現在、私はbefore_saveコールバックを使用してい 1.コード(常に大文字する) 0002(最初のワードがcheck_box_tag値に基づいて大文字)

:2つのフィールドが含まれている診断のためのモデル、ビュー&コントローラを有していますモデルを変換を実装するが、私はそれがcheck_box_tagが選択されていないときにのみ働くようにすることはできません。つまり、check_box_ta g。

check_box_tagをcheck_boxに変更して、attr_assessorをモデルに追加しようとしましたが(格納する必要はないため、sqlite3 dbは含まれません)。

これも機能しませんでした。

どうすればよいですか?内部アプリケーション構成ファイルのチェックボックスを使用するオプションを無効にすると、アプリケーション構成でユーザーが選択できないことが指定されている場合、チェックボックスが「使用不可」または表示されなくなります。

モデル(diagnosis.rb)DescriptionHelper.rbで

require 'DescriptionHelper' 

class Diagnosis < ActiveRecord::Base 
    attr_accessible :code, description 

    string_correct_case = DescriptionHelper.new([:code, :description]) 

    validates :code, :presence => true, :length => { :minimum => 4, :maximum => 4 } 
    validates :description, :presence => true 

    before_save string_correct_case 

end 

コールバック

class DescriptionHelper 
    def initialize(attribute) 
    @attrs_to_manage = attribute 
    end 

    def before_save(record) 
    @attrs_to_manage.each do |attribute| 
     record.send("#{attribute}=", capitaliseWords(record.send("#{attribute}"))) 
    end 
    end 

    private 
    def capitaliseWords(value) 
     value = value.mb_chars.downcase.to_s.gsub(/\b\w/) { |first| first.upcase } 
    end 
    end 

コントローラ(diagnoses_controller.rb)

class DiagnosesController < ApplicationController 

    def new 
    @diagnosis = Diagnosis.new 
    end 

    def create 
    @diagnosis = Diagnosis.new(params[:diagnosis]) 

    if @diagnosis.save 
     flash[:notice] = "Diagnosis created with params [#{@diagnosis.attributes.inspect}" #for debugging, once fixed will be just 'Diagnosis created.' 
     redirect_to @diagnosis 
    else 
     flash[:alert] = "Diagnosis not created." 
     render :action => "new" 
    end 
    end 

    .. other controller actions - edit, show, destroy 

end 

ビュー(_form.html.erb)

<%= form_for(@daignosis) do |f| %> 
    <div class="field"> 
    <%= f.label :code %> 
    <%= f.text_field :code %> 
    </div> 
    <div class="field"> 
    <%= f.label :description %> 
    <%= f.text_field :description %> 
    </div> 
    <div class="field"> 
    <%= check_box_tag("diagnosis_desc_dont_convert", 1, false) %><%= f.label "Leave as entered" %> 
    </div> 
    <div class="actions"> 
    <%= f.submit %> 
    </div> 
<% end %> 

これが現在実行されている場合、check_box_tagは無視されます。

モデルにattar_assessor:description_correctcaseを追加し、f.check_box 'description_correctcase'を使用するようにビューを変更すると、これは引き続き無視されます。

これはどのように機能するのですか?

事前にレール開発者を歓迎してくれてありがとうございます。

答えて

0

最後に、私の質問の構成部分にさまざまなSOソリューションを読んだり再読んだりして、問題を解決しました。私はレールの面で正しいのか分かりませんが、うまくいきます。

あなたが私にもっと良い解決策を提供できるなら、私はこれから確かに学びます。

私の解決策です。

モデル(診断。RB)

require 'DescriptionHelper' 

class Diagnosis < ActiveRecord::Base 
    attr_accessor :do_not_correctcase 
    attr_accessible :code, :description, :do_not_correctcase 

    before_save DescriptionHelper.new([:code, :description]), :if => 
         lambda { |d| d.do_not_correctcase.to_s == '0' } 

    validates :code, :presence => true, :length => { :minimum => 4, :maximum => 4 } 
    validates :description, :presence => true 

end 

Iは、以下SO溶液から参照これ - https://stackoverflow.com/a/6388691/1108010

コントローラ(diagnoses_controller.rb)

class DiagnosesController < ApplicationController 

    def new 
    @diagnosis = Diagnosis.new 
    end 

    def create 
    @diagnosis = Diagnosis.new(params[:diagnosis]) 
    @diagnosis.do_not_correctcase = params[:diagnosis][:do_not_correctcase] 

    logger.debug "New diagnoses: #{@diagnosis.attributes.inspect}" 
    logger.debug "Diagnosis should be valid: #{@diagnosis.valid?}" 
    logger.debug "code has value #{params[:code]}" 


    if @diagnosis.save 
     flash[:notice] = "Diagnosis created with params [#{@diagnosis.attributes.inspect}" #for debugging 
     redirect_to @diagnosis 
    else 
     flash[:alert] = "Diagnosis not created." 
     render :action => "new" 
    end 
    end 

    .. other controller actions - edit, show, destroy 

end 

IはまたCHECK_BOXとcheck_box_tagを交換するビューを変更しました。

ビュー(_form.html.erb)これを得ることは仕事にもかかわらず、

<%= form_for(@daignosis) do |f| %> 
    <div class="field"> 
    <%= f.label :code %> 
    <%= f.text_field :code %> 
    </div> 
    <div class="field"> 
    <%= f.label :description %> 
    <%= f.text_field :description %> 
    </div> 
    <div class="field"> 
    <%= f.check_box 'do_not_correctcase' %><%= f.label "Leave as entered" %><br /> 
    </div> 
    <div class="actions"> 
    <%= f.submit %> 
    </div> 
<% end %> 

は、だから私は以下のとおりである上明確ではないよ:

  1. "#{@diagnosis.attributes.inspect}"と属性を検査します。 attr_accessor変数がNew診断出力に含まれていない理由は、それがデータベーステーブルの一部ではないため、Active Reocrdは新しい診断レコードの一部として@ diagnosis.newを追加しないと仮定します。 誰かがそれを確認するのに十分親切。

  2. なぜログにはlogger.debug "code has value #{params[:code]}"の値がありませんか?ロガー出力でparams[:code]がnullになる原因私は、これは非常にDRYか、クリーンではないと感じて、私は心から、すべてこれを行うための正しい方法は何か知っていると思い

    Started POST "/diagnoses" for 127.0.0.1 at 2012-03-05 09:36:38 +0000 
        Processing by DiagnosesController#create as HTML 
        Parameters: {"utf8"=>"✓", "authenticity_token"=>"RW/mzkhavGeaIW0hVLn0ortTnbCDlrX+FfzH4neLLsA=", "diagnosis"=>{"code"=>"tt02", "description"=>"description for tt02", "do_not_correctcase"=>"1"}, "commit"=>"Create Diagnosis"} 
    New diagnosis: {"code"=>"tt02", "created_at"=>nil, "description"=>"description for tt02", "updated_at"=>nil} 
    Diagnosis should be valid: true 
    code has value 
    

ログファイルには、次のエントリが含まれています。

関連する問題