【问题标题】:Call custom controller action from view when I click on a button Rails当我单击按钮 Rails 时,从视图中调用自定义控制器操作
【发布时间】: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


【解决方案1】:

您的操作是私密的。把它移到专线上面就可以了

【讨论】:

  • 非常感谢!这是一个菜鸟的错误。我认为 private 将在下面的方法中以关键字“end”结束。再次感谢您!
  • 不用担心,随时乐意为您提供帮助。将答案标记为正确,以便其他人可以看到他们现在不需要转移此问题。
  • P.S.如果您想改进您的风格,请安装 rubocop gem 并针对您的应用程序运行它。它将调出所有错误的样式选择,并使您的代码更清洁 100 倍。我最近才开始使用它,我的风格已经在改进。我已经编写 RoR 代码超过 6 年了
  • 哦,不知道。会做!感谢您的建议。
【解决方案2】:

我有一些重构给你

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”吗?里面有路线吗?

当您单击链接时,rails 控制台会显示什么内容。

【讨论】:

  • 是的,我可以看到路线。感谢您的重构,我正在学习,所以它对我有很大帮助。问题依然存在。
  • 打孔控制器顶部有类 PunchController
  • 控制台内容: AbstractController::ActionNotFound(找不到 PunchController 的操作“userout”):actionpack (4.2.4) lib/abstract_controller/base.rb: 132:in process' actionview (4.2.4) lib/action_view/rendering.rb:30:in process' actionpack (4.2.4) lib/action_controller/metal.rb:196:in dispatch' actionpack (4.2.4) lib/action_controller/metal/rack_delegation.rb:13:in dispatch'
  • 控制台也说了些什么。这可能是下一个最有用的信息。错误之前有什么。像 GET "path/blah" 或 POST 等
  • 啊,也许重启就可以了。你重启了吗?常见错误
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2018-11-26
  • 1970-01-01
  • 1970-01-01
  • 2011-02-11
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多