我正在尝试使用按钮更新记录,通过单击使用此表单的确认提交按钮来管理用户tyoe确认记录,但它只会更新数据库中的第一条记录,而不是特定的记录。我可以将“hour.id”放在form_for部分中,以使其更新我想要的记录吗?
index.html.erb表单视图
<% @allhours.each do |hour| %>
<tr id="dashfield">
<td><%= hour.user.first_name + " " +hour.user.last_name %></td>
<td><%= hour.assignment %></td>
<td><%= hour.hours %></td>
<td><%= hour.supervisor %></td>
<td><%= hour.date %></td>
<td><%= link_to "confirm", edit_hour_path(hour.id, :status => 'confirmed')%></td>
</tr>
<% end %>
控制器
def index
@allhours = HourLog.where(status:'pending')
end
def update
@hour = HourLog.find_by(status:'pending')
@hour.update_attribute(update_hour_params)
redirect_to '/dashboard/hours'
end
def edit
@hour = HourLog.find(params[:id])
@hour.update_attributes(update_hour_params)
redirect_to '/dashboard/hours'
end
hour_log模型
class HourLog < ActiveRecord::Base
belongs_to :user
end
路由
Rails.application.routes.draw do
root 'welcome#index'
get '/signup' => 'users#new'
resources :users
get '/dashboard/users' => 'users#index'
put '/dashboard/users' => 'users#update'
get '/login' => 'sessions#new'
post '/login' => 'sessions#create'
delete '/logout' => 'sessions#destroy'
get '/dashboard' => 'hours#new'
get '/dashboard/hours' => 'hours#index'
put '/dashboard/hours' => 'hours#update'
resources :hours
谢谢!