【发布时间】:2015-08-16 22:57:44
【问题描述】:
我正在尝试使用管理员用户更新记录,并使用“验证”按钮将记录的状态从待处理更改为已确认。我已经创建了一个表单,以及这样做的路线,但是控制器给我带来了麻烦,我不确定要为它编写什么代码来更新我点击验证的特定记录。
小时控制器
def index
@allhours = HourLog.where(status:'Pending')
end
def update
@hour = HourLog.where(status:'Pending')
@hour.update(@hour.id, :status)
end
小时/index.html.erb
<td><%=form_for(:hour_log, method: :put) do |f|%>
<%= f.hidden_field :status, :value => 'Confirmed' %>
<%= f.submit 'Validate'%>
<%end%>
</td>
任何帮助都会很棒,谢谢!
错误: HoursController 中的 NoMethodError#update #
的未定义方法“id”我知道更新定义中控制器的 (@hour.id) 部分有问题,但我不知道用什么替换它
编辑:耙路线
Prefix Verb URI Pattern Controller#Action
root GET / welcome#index
signup GET /signup(.:format) users#new
users GET /users(.:format) users#index
POST /users(.:format) users#create
new_user GET /users/new(.:format) users#new
edit_user GET /users/:id/edit(.:format) users#edit
user GET /users/:id(.:format) users#show
PATCH /users/:id(.:format) users#update
PUT /users/:id(.:format) users#update
DELETE /users/:id(.:format) users#destroy
GET /users(.:format) users#index
PUT /users(.:format) users#update
login GET /login(.:format) sessions#new
POST /login(.:format) sessions#create
logout DELETE /logout(.:format) sessions#destroy
dashboard GET /dashboard(.:format) hours#new
dashboard_hours GET /dashboard/hours(.:format) hours#index
hours PUT /hours(.:format) hours#update
GET /hours(.:format) hours#index
POST /hours(.:format) hours#create
new_hour GET /hours/new(.:format) hours#new
edit_hour GET /hours/:id/edit(.:format) hours#edit
hour GET /hours/:id(.:format) hours#show
PATCH /hours/:id(.:format) hours#update
PUT /hours/:id(.:format) hours#update
DELETE /hours/:id(.:format) hours#destroy
小时控制器
class HoursController < ApplicationController
before_action :require_user
before_action :require_admin, only: [:index]
def index
@allhours = HourLog.where(status:'pending')
end
def new
@hour = current_user.hour_logs.new
@entry = current_user.hour_logs.all
end
def edit
flash[:warning] = "Hash: #{params}"
end
def create
@hour = HourLog.new(hour_params)
@hour.user_id = current_user.id if current_user
@hour.status = 'pending'
if @hour.save
redirect_to '/dashboard'
end
end
private
def hour_params
params.require(:hour_log).permit(:assignment, :hours, :supervisor, :date)
end
def update_hour_params
params.require(:hour_log).permit(:status)
end
end
HourLog 方法
class HourLog < ActiveRecord::Base
belongs_to :user
end
【问题讨论】:
-
“但是控制器给我带来了麻烦。”这甚至意味着什么?您看到的确切问题是什么?
-
由于我输入的“id”,它没有更新记录。如果我输入 (params[:id], :status),那么它说它找不到没有 id 的记录。现在它说“.id”是一个未定义的方法
-
@DKucher 请在问题正文中发布完整的错误消息
标签: ruby-on-rails ruby-on-rails-4