我正在尝试使用管理员用户更新记录,并使用“验证”按钮将记录的状态从待处理更改为已确认。我已经创建了一个表单以及执行此操作的路线,但是 Controller 给我带来了麻烦,我不确定要为其编写什么代码来更新我单击验证的特定记录。
时间 Controller
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#update 中没有方法错误 # 的未定义方法“id”
我知道更新定义中 Controller 的 (@hour.id) 部分有问题,但我不知道用什么替换它
编辑: rake 路线
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
时间 Controller
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
最佳答案
我看到这个答案已被接受,但我想为任何可能发现这个问题的人提出一个替代解决方案。假设:基本页面是包含所有待处理项目的索引,并且单击是简单的状态更改。 (可以轻松地将状态切换添加到此解决方案中。)
(我使用问题中的命名。)
使用 link_to 作为消息
可以通过使用 link_to 触发 Controller 操作来更新此标志来实现标志从“待处理”到“已确认”的状态更改。 哈希参数可以通过 link_to 传递,从而在 Controller 中启用简化的逻辑。
查看:Index.html.erb
<% @allhours.each do |hour| %>
<ul>
<li><%= hour.textdescriptionvar %> is an item pending approval.
<%= link_to(" Click to approve", edit_hour_path(hour, :status => "Confirmed"))
</li>
</ul>
<% end %>
Controller :HourLogsController
def edit
flash[:info] = "Hash: #{params}"
@hour = HourLog.find(params[:id])
end
def update
@hour = HourLog.find(params[:id])
@hour.update_attributes(hours_params)
if @hour.save
redirect_to @hour, :notice => "Successfully changed stat."
else
render 'index' # Any action here, index for example
end
end
private
def hours_params
params.require(:hour_log).permit(:status)
end
我建议在 link_to 中添加一条数据:确认消息,但我想模仿发布者发布的情况。
link_to 的 Rails 规范 here .
关于ruby-on-rails - 使用 Rails 4 更新记录,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32020908/