小编Soo*_*e J的帖子

从Ruby中的JSON文件解析并从嵌套哈希中提取数字

现在我正在从Ruby中的JSON文件中提取信息.那么如何从以下文本文件中提取"得分"一词旁边的数字呢?例如,我想要打开0.6748984055823062,0.6280145725181376.

{
  "sentiment_analysis": [
    {
      "positive": [
        {
          "sentiment": "Popular",
          "topic": "games",
          "score": 0.6748984055823062,
          "original_text": "Popular games",
          "original_length": 13,
          "normalized_text": "Popular games",
          "normalized_length": 13,
          "offset": 0
        },
        {
          "sentiment": "engaging",
          "topic": "pop culture-inspired games",
          "score": 0.6280145725181376,
          "original_text": "engaging pop culture-inspired games",
          "original_length": 35,
          "normalized_text": "engaging pop culture-inspired games",
          "normalized_length": 35,
          "offset": 370
        },
     "negative": [
    {
      "sentiment": "get sucked into",
      "topic": "the idea of planning",
      "score": -0.7923352042939829,
      "original_text": "Students get sucked into the idea of planning",
      "original_length": 45,
      "normalized_text": "Students …
Run Code Online (Sandbox Code Playgroud)

ruby parsing json

22
推荐指数
3
解决办法
3万
查看次数

为什么我不能在 rails 控制台中使用复数方法?

我刚刚才知道我不能在 rails 控制台或 IRB 中使用“复数”方法。这有什么我不明白的吗?

2.3.0 :001 > pluralize
NameError: undefined local variable or method `pluralize' for main:Object
Run Code Online (Sandbox Code Playgroud)

当它在 ruby​​ 或视图文件中使用时,它会得到很好的解释。为什么我不能在 rails 控制台中使用它?

ruby ruby-on-rails

2
推荐指数
1
解决办法
1131
查看次数

Windows 10中的bcrypt加载错误

现在我正在尝试在Windows 10中安装bcrypt,但是每当运行webrick服务器时我都会收到加载错误,如下所示.

C:/Ruby22-x64/lib/ruby/gems/2.2.0/gems/bcrypt-3.1.10-x64-mingw32/lib/bcrypt.rb:16:in 
`require': cannot load such file -- bcrypt_ext (LoadError)
Run Code Online (Sandbox Code Playgroud)

我使用rails 4.1.5和ruby 2.2.3.当我使用Windows 8.1时,bcrypt没有问题.当我使用Windows 8.1时,ruby版本是2.1.6

我该如何解决这个bcrypt问题?

ruby-on-rails bcrypt windows-10

1
推荐指数
1
解决办法
1989
查看次数

为什么控制器中的新动作需要初始化实例变量?

为什么控制器中的新动作需要初始化实例变量@article?我已经测试过,在新操作中没有实例变量的情况下,记录可以很好地保存到数据库中的表中。

class ArticlesController < ApplicationController
  def new
    @article = Article.new
  end

  def create
    @article = Article.new(article_params)

    if @article.save
      redirect_to @article
    else
      render 'new'
    end
  end
end
Run Code Online (Sandbox Code Playgroud)

ruby-on-rails instance-variables

1
推荐指数
1
解决办法
303
查看次数

params [:id]来自rails?

我是Rails的初学者.我现在正在学习使用"Beginning Rails 4"这本书.我想问你关于传递给params方法的'参数'.以下是典型的轨道控制器之一.

class CommentsController < ApplicationController
  before_action :load_article
  def create
    @comment = @article.comments.new(comment_params)
    if @comment.save
      redirect_to @article, notice: 'Thanks for your comment'
    else
      redirect_to @article, alert: 'Unable to add comment'
    end
  end

  def destroy
    @comment = @article.comments.find(params[:id])
    @comment.destroy
    redirect_to @article, notice: 'Comment Deleted'
  end

  private
    def load_article
      @article = Article.find(params[:article_id])
    end

    def comment_params
      params.require(:comment).permit(:name, :email, :body)
    end
  end 
Run Code Online (Sandbox Code Playgroud)

是的,这只是一个典型的评论控制器,用于创建附加到文章的评论.评论模型"属于"文章模型,文章模型"有很多"评论.

看看destroy方法.

def destroy
  @comment = @article.comments.find(params[:id])
  -- snip --
end
Run Code Online (Sandbox Code Playgroud)

它通过find(params [:id])找到与文章相关的评论.我的问题是,params [:id]来自哪里?

它来自URL吗?或者,当创建任何评论记录时,rails会自动保存params散列吗?所以我们可以通过find找到任何评论(params [:id])?

load_article方法类似.

def load_article
  @article = …
Run Code Online (Sandbox Code Playgroud)

ruby-on-rails params

0
推荐指数
1
解决办法
3541
查看次数