Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/ruby-on-rails/67.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ruby on rails 单击按钮时从视图调用自定义控制器操作_Ruby On Rails_Ruby_View_Controller_Action - Fatal编程技术网

Ruby on rails 单击按钮时从视图调用自定义控制器操作

Ruby on rails 单击按钮时从视图调用自定义控制器操作,ruby-on-rails,ruby,view,controller,action,Ruby On Rails,Ruby,View,Controller,Action,我正在努力处理rails中的一个简单的操作调用,但我找不到哪里出了问题,也找不到为什么许多解决方案在我的案例中不起作用。我提到我是rails的新手,来自Java世界 问题是这样的: 我想在视图中有一个按钮,它指向一个控制器动作,一个改变表中一列的动作 routes.rb post'punch/userout'=>'punch#userout',:as=>:userout 视图:punch\out.erb 控制器:punch_controller.rb class PunchContro

我正在努力处理rails中的一个简单的操作调用,但我找不到哪里出了问题,也找不到为什么许多解决方案在我的案例中不起作用。我提到我是rails的新手,来自Java世界

问题是这样的:
我想在视图中有一个按钮,它指向一个控制器动作,一个改变表中一列的动作

routes.rb

post'punch/userout'=>'punch#userout',:as=>:userout

视图:punch\out.erb


控制器: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

类PunchController应用程序控制器
在\u筛选器之前:仅授权\u管理员::索引
布局“应用程序”
布局错误,:除外=>:新建
#GET方法从数据库中获取所有产品
def索引
#@punchins=Punchin.all
@filterrific=初始化\u filterrific(
蓬钦,
参数[:filterrific]
)或返回
@punchins=@filterrific.find.page(参数[:page])
回应待办事项|格式|
format.html
format.js
结束
rescue ActiveRecord::RecordNotFound=>e
#持久化参数集存在问题。重置它。
puts“必须重置filterrific参数:#{e.message}”
重定向到(重置过滤器参考url(格式::html))并返回
结束
#获取新产品表单的方法
def新
@punchin=punchin.new
如果是当前的_user.admin
将\重定向到根\路径
开始
elsif当前用户已打孔
重定向到打孔路径
=结束
结束
结束
#处理表单数据的POST方法
def创建
#@punchin.user\u id=当前用户id
#@punchin=punchin.new(冲压参数)
@punchin=当前用户.punchins.build(punch参数)
@punchin.server\u time=time.now.strftime(“%Y-%m-%d%H:%m”)
#@punchin.is_punched=真;
#从punchin获取当前用户
@user=@punchin.user
#使用true在用户上设置穿孔
@user.punched_in=true;
#更新用户
@user.save
#@punchin.user.punched_in=true;
如果@punchin.save
闪光[:注意]=“打孔!”
#告诉Punchinailer在保存后发送通知电子邮件
PunchinMailer.punchin_电子邮件(@punchin)。稍后发送
将\u重定向到\u路径中的punch\u
其他的
flash[:error]=“未能编辑打孔!”
渲染:新
结束
结束
#基于id在数据库中更新产品的PUT方法
def更新
@punchin=punchin.find(参数[:id])
如果@punchin.update_属性(冲压参数)
flash[:notice]=“Punchin已更新!”
将\重定向到根\路径
其他的
flash[:error]=“编辑Punchin失败!”
渲染:编辑
结束
结束
#基于id从数据库中删除产品的删除方法
def销毁
@punchin=punchin.find(参数[:id])
如果@punchin.delete
flash[:notice]=“Punchin已删除!”
将\重定向到根\路径
其他的
flash[:error]=“删除此Punchin失败!”
渲染:销毁
结束
结束
私有的
#我们使用强参数来验证参数
def冲压参数
参数require(:punchin).permit(:服务器\时间,:地址\地理位置,:工作\类型,:工作\描述,:用户\ id)
结束
def秀
#方法级渲染
@punchin=punchin.find(参数[:id])
结束
#打卡时
def in
结束
def用户输出
如果用户已登录?
当前用户。更新属性(:punched\u in=>false)
其他的
重定向到新用户会话路径,注意:“您未登录。”
结束
结束
结束
关于信息:一个打孔属于:user,用户有多个:打孔

我在users表中有一列,上面写着punched_in:true/false,我只希望在视图中单击链接/按钮时将该列设置为false

我尝试了很多解决方案,包括链接到、按钮到、不同的路线等。 在这种情况下,我得到以下错误:

找不到PunchController的操作“userout”

在其他情况下,我的按钮工作,但无法达到我想要的行动


谢谢

我有一些重构要给你

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
可以从命令行运行“rake路由”吗。路线在那里吗


另外,当您单击链接时,rails控制台会说什么

我有一些重构要给你

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
可以从命令行运行“rake路由”吗。路线在那里吗



另外,当您单击链接时,rails控制台会说什么

你的行动是私人的。将其移动到专用线上方,它将起作用

您的操作是专用的。将其移到专用线上方,它将工作

您可以从终端发布
punch_控制器
的路由吗。@重新启动userout post/punch/userout(:format)punch#userout如果您更改了
routes.rb
文件,请尝试重新启动服务器。我重新启动了服务器,但问题仍然存在。您的控制器中是否有名称空间?请从终端发布
punch\u控制器的路由。@Reboot userout post/punch/userout(:format)punch\userout如果您更改了
routes.rb
文件,请尝试重新启动服务器。我重新启动了服务器,但是问题仍然存在。您的控制器中有任何名称空间吗?是的,我可以看到路由。谢谢你的重构,我正在学习,所以它对我帮助很大。问题仍然存在。是否在PunchController的顶部有类PunchControllerprocess”actionview(4.2.4)lib/action\u view/rendering.rb:30:in
process'actionpack(4.2.4)lib/action\u controller/metal.rb:196:in
dispatch'actionpack(4.2.4)lib/action\u controller/metal/rack\u delegation.rb:13:in
dispatch'控制台也会说些什么。这可能是下一个最有用的信息。在出错之前有什么事吗。例如获取“路径/废话”或POST etcAh m