【问题标题】:debugger in rails with byebug not working带有byebug的rails中的调试器不起作用
【发布时间】:2016-06-15 12:31:58
【问题描述】:

当我尝试在带有 byebug 的 Rails 中使用调试器时遇到问题...我安装了 byebug gem 没有任何问题...在 gemfile 中:

group :development, :test do
  gem 'sqlite3'
  gem 'byebug'
end

将调试器放入我的控制器中:

class ArticlesController < ApplicationController
  before_action :set_article, only: [:edit, :update, :show, :destroy]

  def new
      @article = Article.new
  end

  def create
      debugger
      @article = Article.new(article_params)
      # @article.user = User.first
    if @article.save
      flash[:success] = "Article was successfully created"
      redirect_to article_path(@article)
    else
      render 'new'
    end
  end

  def show
  end

  def index
    @articles = Article.all
  end

  def edit
  end

  def update
    if @article.update(article_params)
      flash[:success] = "Article was successfully updated"
      redirect_to article_path(@article)
    else
        render 'edit'
    end
  end

  def destroy
    @article.destroy
    flash[:danger] = "Article was successfully deleted"
    redirect_to articles_path

  end

  private
    def set_article
        @article = Article.find(params[:id])
    end
    def article_params
        params.require(:article).permit(:title, :description)
    end

end

(我在windwos 7中使用gitbash) 问题是当我尝试调用 article_params 时我只得到一个空行很长时间没有响应我试图重新启动我的服务器并再次尝试调试但同样的问题....Here is an image for the problem

这是来自 git bash 的代码(与图片相同):

    5:          @article = Article.new
    6:  end
    7:
    8:   def create
    9:                  debugger
=> 10:          @article = Article.new(article_params)
   11:          # @article.user = User.first
   12:     if @article.save
   13:       flash[:success] = "Article was successfully created"
   14:       redirect_to article_path(@article)
(byebug) article_params
-(here goes the blank line)

有人可以帮忙吗?

【问题讨论】:

  • 能把控制器的全部代码放上来吗?
  • Milan Pudasini ..我更新了它,你可以看到我的控制器的整个代码..
  • 删除调试器一次并把 byebug 放在那个地方
  • 我建议不要使用 Windows 来开发 Rails 应用程序。 Ruby CLI、Rails CLI、Git CLI 和一般部署过程在基于 Linux 的终端/环境中要好得多。如果那是你唯一的 PC,我会用 VirtualBox 安装 Ubuntu 并改用它。
  • 嗨@MilanPudasini 我通过迁移到 ubuntu 解决了这个问题,就像他们在上面告诉我的那样......无论如何,谢谢你;)

标签: ruby-on-rails debugging ruby-on-rails-4 rubygems byebug


【解决方案1】:

所以我在 2017 年 1 月发布的 Stackoverflow Byebug fully supports Windows or not? 上发现了这个问题。

我关注了 Github 上的问题,并在 rails 中发现了一个提交(对不起,我的网络阻塞了 github,所以我不喜欢他们)。 windows 和 rails 不支持 mri 平台Gemfile 生成器模板现已更新为。

gem 'byebug', platform: [:mri, :mingw, :x64_mingw]

我进行了更改,然后运行了我的代码,byebug 现在可以在 Windows 上运行!

更改后可能需要运行 bundle。

【讨论】:

  • 下一步是让 irb 在 Git Bash 中工作,因为这会阻止 byebug 正常工作。如果您的rails console 输出中有Switch to inspect mode.,请参阅此问题。 stackoverflow.com/questions/38132561/…
【解决方案2】:

适用于我的一个应用程序。我使用pry 作为我的默认调试工具,只是将byebug 放入工作完美无缺。

  [58, 67] in /Users/../controllers/items_controller.rb
     58:
     59:   # POST /items
     60:   # POST /items.json
     61:   def create
     62:     debugger
  => 63:     @item = Item.new(item_params)
     64:
     65:     respond_to do |format|
     66:       if @item.save
     67:         format.html { redirect_to edit_item_path(@item), notice: 'Item was successfully created.' }
  (byebug) item_params
  <ActionController::Parameters {"name"=>"asd", "description"=>"asdasd", "hub_id"=>1} permitted: true>
  (byebug)

所以,我认为我们需要更多代码。也许你的文章参数?

private
  # Use callbacks to share common setup or constraints between actions.
  def set_item
    @item = Item.find(params[:id]).decorate
  end

  # Never trust parameters from the scary internet, only allow the white list through.
  def item_params
    params.require(:item).permit(
      :photo,
      :name,
      :description,
      :location_id,
      :item_category_id,
      :x_coordinates,
      :y_coordinates,
      :inspection_date
    ).merge(hub_id: current_hub)
  end

【讨论】:

  • 我不知道你的代码有什么问题。从我的 POV 看起来很好。我认为您的问题出在其他地方。您是否尝试过 pry 而不是 byebug
  • 不,我没有......它是宝石吗??你能给我提供链接吗?
  • github.com/pry/pry .. 在你的文件中做 require 'pry' 在类的顶部,然后用 binding.pry 替换 debugger
  • 谢谢..试试看
  • @JayKilleen 是的,我搬到了 ubuntu,它解决了我的问题......问题是我在 windows 上工作,这带来了很多问题
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-07-10
  • 2013-12-06
相关资源
最近更新 更多