我正在使用rails中的简单动作调用,我找不到有什么问题以及为什么许多解决方案在我的情况下不起作用。我提到我是来自Java世界的新人。
问题是这样的:
我希望在我的视图中有一个指向控制器操作的按钮,这是一个更改表中列的操作。
的routes.rb
发布'punch / userout'=> 'punch #userout',:as => :userout
查看: punch \ out.erb
<%= link_to('Out', userout_path, method: :post) %>
controller: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
并且对于信息:one punch belongs_to:user and users has_many:punches
我在users表中有一个列,其中包含punched_in:true / false,当我从视图中单击链接/按钮时,我只希望将该列设置为false。
我尝试了很多解决方案,包括link_to,button_to,不同路线等。 在这种情况下,我收到此错误:
找不到PunchController
的动作'userout'
在其他情况下,我的按钮工作但无法达到我想要的操作。
谢谢!
答案 0 :(得分:2)
您的行为是私密的。将它移到专线上方,它将起作用
答案 1 :(得分:0)
我为你做了一些重构
def userout
if user_signed_in?
current_user.update_attributes(punched_in: false)
redirect_to punch_out_path, notice: "You've been punched out" # what path do you want to redirect to?
else
redirect_to new_user_session_path, notice: 'You are not logged in.'
end
end
你可以运行&#34; rake routes&#34;从命令行。路线在哪里?
当您单击链接时,rails控制台会说什么。