お早うございます。
現在、初めてルビ言語で開発している僕には問題があってどっちらの方がこういう問題が解決方法などが分かれば教えて頂けませんか?
問題としてはエラーメッセンジ表示に関することです。
詳しくは:一つビューで二つのモデルのエラーを表示したいです。
例えば:
ビュー:Tester → こっちらに二つのモデルのエラーメッセンジが表示される
モデル:Tester、HourlyPay
コントローラ:Tester_controller
一つ気づいたのはRENDERの方が間違ってるはずだと思いますが。どうやってすれば【http://localhost:3000/testers/905773432/edit】に移動して
エラーを表示すろことが出来るのか迷っています。
コード:
ビュー:Tester
<%= form_for(@tester) do |f| %> <table class='ta1'> <%= pluralize(@tester.errors.count, "error") %> <%= pluralize(@tester.errors[:hourly_pay].count, "error") %> <% if @tester.errors.any? %> <tr id="error_explanation"> <th class="tamidashi" colspan="2"> <ul> <% @tester.errors.full_messages.each do |message| %> <li><%= message %></li> <% end %> </ul> </th> </tr> <% end %> <% if @tester.errors[:hourly_pay].any? %> <tr id="error_explanation"> <th class="tamidashi" colspan="2"> <ul> <% @tester.errors[:hourly_pay].each do |message| %> <li><%= error %></li> <% end %> </ul> </th> </tr> <% end %> </table> <% end %>
モデル:
Tester
class Tester < ActiveRecord::Base include SearchHelper has_many :hourly_pay accepts_nested_attributes_for :hourly_pay ...... end
モデル:
HourlyPay
class HourlyPay < ActiveRecord::Base belongs_to :tester default_scope { order(:update_date) } validate :validate_amount validate :validate_before_insert_hourlypay def validate_amount if new_record? && amount.nil? errors.add("時給", "を入力してください。") end end def validate_before_insert_hourlypay if update_date.nil? errors.add("日付", "を入力してください。") else if new_record? && HourlyPay.where(update_date: update_date).size > 0 errors.add("日付", "が存在しています。") end end end end
コントローラ:
Tester_controller
def update respond_to do |format| if @tester.update(tester_params) format.html do if params[:update_password] str_array = ('a'..'z').to_a + ('A'..'Z').to_a + ('0'..'9').to_a password = ( Array.new(8){ str_array[rand(str_array.size)] } ).join @tester.password = password @tester.crypt_password @tester.save flash[:password]=password # redirect_to action: 'show' redirect_to action: 'show' and return # and return を書かないとエラーになる elsif params[:delete_hourly_pay] @user = HourlyPay.find(params[:tester][:hourlypay_id]).destroy redirect_to action: 'edit', id: params[:tester][:tester_id] elsif params[:new_hourly_pay] @update_date_hourlypay = params[:new_updatedate] @amount = params[:new_amout] @user_id = current_user[:user].id @hourlypay = HourlyPay.new(:tester_id => @tester.id, # :update_date => Date.parse(@update_date_hourlypay), :update_date => @update_date_hourlypay, :amount => @amount, :user_id => @user_id) @hourlypay.save end pp "ERRORS ======================>" pp @tester.errors pp @hourlypay.errors render :edit end format.json { render :show, status: :ok, location: @tester } else format.html { render :edit } format.json { render json: @tester.errors, status: :unprocessable_entity } end end end
コマンドで取得出来た情報:
#<ActiveModel::Errors:0x89dc7f8 @base= #<HourlyPay:0x8a74c88 id: nil, tester_id: "905773432", user_id: "118", update_date: nil, amount: nil, created_at: nil, updated_at: nil>, @messages={:時給=>["を入力してください。"], :日付=>["を入力してください。"]}>
回答2件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2017/02/18 15:21
2017/02/21 02:46