【发布时间】:2016-07-24 15:19:03
【问题描述】:
我正在为 Rails 中的一个简单的操作调用而苦苦挣扎,我找不到问题所在以及为什么许多解决方案在我的情况下不起作用。我提到我是来自 Java 世界的 Rails 新手。
问题是这样的:
我想在我的视图中有一个按钮,它指向一个控制器动作,一个改变表中列的动作。
routes.rb
post 'punch/userout' => 'punch#userout', :as => :userout
视图:punch\out.erb
<%= link_to('Out', userout_path, method: :post) %>
控制器:punch_controller.rb
class PunchController ApplicationController
before_filter :authorize_admin, only: :index
layout 'application'
layout false, :except => :new
# GET method to get all products from database
def index
#@punchins = Punchin.all
@filterrific = initialize_filterrific(
Punchin,
params[:filterrific]
) or return
@punchins = @filterrific.find.page(params[:page])
respond_to do |format|
format.html
format.js
end
rescue ActiveRecord::RecordNotFound => e
# There is an issue with the persisted param_set. Reset it.
puts "Had to reset filterrific params: #{ e.message }"
redirect_to(reset_filterrific_url(format: :html)) and return
end
# GET method for the new product form
def new
@punchin = Punchin.new
if current_user.admin
redirect_to root_path
=begin
elsif current_user.punched_in
redirect_to punch_out_path
=end
end
end
# POST method for processing form data
def create
#@punchin.user_id = current_user.id
#@punchin = Punchin.new(punch_params)
@punchin = current_user.punchins.build(punch_params)
@punchin.server_time = Time.now.strftime("%Y-%m-%d %H:%M")
#@punchin.is_punched = true;
#get current user from punchin
@user = @punchin.user
#set punched on user with true
@user.punched_in = true;
#update user
@user.save
#@punchin.user.punched_in = true;
if @punchin.save
flash[:notice] = 'Punched In!'
# Tell the Punchinailer to send a notification email after save
PunchinMailer.punchin_email(@punchin).deliver_later
redirect_to punch_in_path
else
flash[:error] = 'Failed to edit Punch!'
render :new
end
end
# PUT method for updating in database a product based on id
def update
@punchin = Punchin.find(params[:id])
if @punchin.update_attributes(punch_params)
flash[:notice] = 'Punchin updated!'
redirect_to root_path
else
flash[:error] = 'Failed to edit Punchin!'
render :edit
end
end
# DELETE method for deleting a product from database based on id
def destroy
@punchin = Punchin.find(params[:id])
if @punchin.delete
flash[:notice] = 'Punchin deleted!'
redirect_to root_path
else
flash[:error] = 'Failed to delete this Punchin!'
render :destroy
end
end
private
# we used strong parameters for the validation of params
def punch_params
params.require(:punchin).permit(:server_time, :address_geoloc, :work_type, :work_desc, :user_id)
end
def show
# method level rendering
@punchin = Punchin.find(params[:id])
end
#when punched in
def in
end
def userout
if user_signed_in?
current_user.update_attributes(:punched_in => false)
else
redirect_to new_user_session_path, notice: 'You are not logged in.'
end
end
end
关于信息:一拳属于_to :user 和用户有_many :punches
我在 users 表中有一个列,上面写着 punched_in: true/false,我只希望当我从视图中单击链接/按钮时将该列设置为 false。
我尝试了很多解决方案,包括 link_to、button_to、不同的路线等。 在这种情况下,我收到此错误:
找不到 PunchController 的操作“userout”
在其他情况下,我的按钮工作但无法达到我想要的操作。
谢谢!
【问题讨论】:
-
能否请您从终端发布
punch_controller的路线。 -
@Reboot userout POST /punch/userout(.:format) punch#userout
-
如果您更改了
routes.rb文件,请尝试重新启动服务器。 -
我重启了服务器,但问题依旧。
-
你的控制器中有命名空间吗?
标签: ruby-on-rails ruby view controller action