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

Call custom controller action from view when I click on a button Rails

我正在努力处理 rails 中的一个简单的操作调用,但我找不到问题所在以及为什么许多解决方案对我的情况不起作用。我提到我是 rails 的新人,来自 Java 世界。

问题是这样的:
我想在我的视图中有一个指向控制器动作的按钮,该动作更改 table.

中的列

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

</pre>

<p>
以及信息:一拳belongs_to:用户和用户has_many:拳

我在用户 table 中有一列显示 punched_in:true/false,我只想在单击 [=42= 时将该列设置为 false ] 从视图。

我尝试了很多解决方案,link_to、button_to、不同的路线等。 在这种情况下,我收到此错误:

The action 'userout' could not be found for PunchController

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

谢谢!

我有一些重构给你

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 routes" 吗?路线在里面吗?

此外,当您单击 link 时,rails 控制台会显示什么内容。

您的操作是保密的。将它移到专线上方,它会起作用