Ruby on rails ruby on rails-无法加载此类文件--serialport

Ruby on rails ruby on rails-无法加载此类文件--serialport,ruby-on-rails,serial-port,Ruby On Rails,Serial Port,我正在尝试使用RubyonRails的串行端口。我使用require'serialport'。我安装了gem串口。 我有一个错误: cannot load such file -- serialport 这一行突出显示: require 'serialport' 这是我的剧本: class ApplicationController < ActionController::Base # Prevent CSRF attacks by raising an exception.

我正在尝试使用RubyonRails的串行端口。我使用require'serialport'。我安装了gem串口。 我有一个错误:

cannot load such file -- serialport
这一行突出显示:

require 'serialport'
这是我的剧本:

class ApplicationController < ActionController::Base
  # Prevent CSRF attacks by raising an exception.
  # For APIs, you may want to use :null_session instead.
  protect_from_forgery with: :exception

  def hello
    render text: "hello1, world!"
  end

  def cmd
    require 'serialport'

    @result = params[:parametr]
    @dioda = params[:dioda]

    #params for serial port  
    port_str = "/dev/ttyACM0"  #may be different for you  
    baud_rate = 9600  
    data_bits = 8  
    stop_bits = 1  
    parity = SerialPort::NONE  
    sp = SerialPort.new(port_str, baud_rate, data_bits, stop_bits, parity) 

    case @result
      when "1"
        render html: "<strong>Led_on</strong>".html_safe
      when "0"      
        render html: "<strong>Led_off</strong>".html_safe
      when "sos"
        render html: "<strong>Led_sos</strong>".html_safe
    end

    sleep 4
    puts sp.readlines
  end
end
class ApplicationController发光二极管亮起”。html\u安全
当“0”时
呈现html:“Led\u关闭”。html\u安全
当“sos”
呈现html:“Led\u sos”。html\u安全
结束
睡眠4
放置sp.readlines
结束
结束

如果您使用的是Rails,则需要将
gem'serialport'
放入
gem文件
,然后运行
bundle安装

通过更改使用错误ruby解释器的乘客配置来解决问题。当我在命令行中以用户身份检查时,一切似乎都正常,但使用Apache/Passenger是一个错误,因为在这两种情况下使用了不同的ruby解释器。

是的,我使用Rails。我安装了gem'serial'。这是我的控制台的输出:greg@dom-NAS:~/ruby/arduino$bundle安装|使用serialport 1.3.1进行grep串行greg@dom-NAS:~/ruby/arduino$我看到这里安装了gem serial:bundle show serialport/home/greg/.rbenv/versions/2.1.3/lib/ruby/gems/2.1.0/gems/serialport-1.3.1如果你把它放在gem文件中,你就不需要
要求
它(由于Rails的自动加载)。如果删除该行,会发生什么?(通常,当您使用
require
时,您不希望在方法内部执行此操作。您应该只在文件顶部或初始值设定项中执行此操作。但是,在这里根本不需要它。)您是对的,require不是必需的。但问题在于phusion乘客使用了错误的ruby解释器。我改变了乘客的配置,它工作了。