Ruby on Rails初めてですが、タイムカードを作っているところです。
退社ボタンクリック時に、退社時間をセットしたく、以下のようにしたところ
この行でエラーになりました。
ruby
1@employee.time_cards.finish_at = Time.current
error
1undefined method `finish_at=' for #<TimeCard::ActiveRecord_Associations_CollectionProxy:0x007f12bde78020> Did you mean? find_last 2 3
社員とタイムカードは関連付けさせてます。
こういう時はRoRではどのようにセットするのでしょうか?
宜しくお願い致します。
タイムカード・コントローラ
ruby
1 2 def update 3 4 @employee = Employee.find(current_employee.id) 5 6 _employee_params = employee_params 7 8 #退社 9 if params[:btnUpd] 10 @employee.time_cards.finish_at = Time.current 11 end 12 13 if @employee.update(_employee_params) 14 render :success 15 else 16 render :error 17 end 18 19 end 20
タイムカード・モデル
ruby
1class TimeCard < ApplicationRecord 2 3 belongs_to :employee 4 5end 6
社員・モデル
ruby
1 2class Employee < ApplicationRecord 3 4 has_many :time_cards 5 accepts_nested_attributes_for :time_cards, reject_if: proc {|attr| attr[:wk_day_on].blank?}, allow_destroy: true 6 7 8end 9
タイムカード・ビュー
ruby
1 <div class="row text-center" style =" margin-left: 0;"> 2 <%= form_for(@employee, url: time_cards_path) do |f| %> 3 4 <%= f.fields_for :time_cards, @time_card do |time_f| %> 5 <%= f.hidden_field :department_id, :value =>@employee.emp_group.department_id %> 6 <%= f.hidden_field :section_id, :value =>@employee.emp_group.section_id %> 7 <%= f.hidden_field :group_id, :value =>@employee.emp_group.group_id %> 8 9 <%= time_f.hidden_field :wk_day_on, :value =>@today %> 10 11 <% if !@time_card.start_at.nil? %> 12 <% if !@time_card.finish_at.nil? %> 13 <% else %> 14 <%= f.submit ' 退勤 ', class: 'btn btn-primary btn-lg' , name: 'btnUpd' %> 15 <% end %> 16 <% else %> 17 <%= f.submit ' 出勤 ', class: 'btn btn-primary btn-lg' %> 18 <% end %> 19 <% end %> 20 21 22 <% end %> 23 </div>
回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。