ruby on rails - cannot load such file -- serialport - ruby-on-rails

I am trying to use serial port by ruby on rails. I use require 'serialport'. I installed gem serialport.
I have got this error:
cannot load such file -- serialport
and this line is highlighted:
require 'serialport'
This is my script:
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

If you're using Rails, you need to put gem 'serialport' in your Gemfile and then run bundle install.

Problem solved by changing configuration of passenger which used wrong ruby interpreter. When I checked as a user in command line everything seemed to be ok, but using Apache/Passenger was an error, everything because different ruby interpreters used in these two situations.

Related

"The action 'execute' could not be found for GraphqlController" when using GraphQL on an API only Rails app?

I've posted a bug for this on rmosolgo/graphql-ruby but just in case I may be doing something wrong, I'm hoping to see if anyone else has a solution to my problem.
When creating an API only rails application it seems that Rails believes the execute method in my GraphqlController is missing.
Here is my graphql_controller.rb file:
class GraphqlController < ApplicationController
# If accessing from outside this domain, nullify the session
# This allows for outside API access while preventing CSRF attacks,
# but you'll have to authenticate your user separately
protect_from_forgery with: :null_session
def execute
variables = ensure_hash(params[:variables])
query = params[:query]
operation_name = params[:operationName]
context = {
# Query context goes here, for example:
# current_user: current_user,
}
result = RailsApiGraphqlExecuteTestSchema.execute(query, variables: variables, context: context, operation_name: operation_name)
render json: result
rescue => e
raise e unless Rails.env.development?
handle_error_in_development e
end
# ... continues on
end
Here is my routes.rb file:
Rails.application.routes.draw do
post "/graphql", to: "graphql#execute"
# For details on the DSL available within this file, see https://guides.rubyonrails.org/routing.html
end
when I run rake routes this is what I get:
prompt> rake routes
Prefix Verb URI Pattern Controller#Action
graphql POST /graphql(.:format) graphql#execute
... continues on
You should be able to reproduce this with the following command line steps:
rails new execute-test --api
cd execute-test
vim Gemfile # or open an editor and add "gem 'graphql'"
bundle
rake db:create
rails g graphql:install
rake routes # to test that the route exists
rails s
When you use an app like GraphiQL and go to http://localhost:3000/graphql you'll get the following error:
Started POST "/graphql" for 127.0.0.1 at 2019-10-15 16:23:29 -0700
(0.3ms) SELECT sqlite_version(*)
AbstractController::ActionNotFound (The action 'execute' could not be found for GraphqlController):
... strack trace continues ...
Maybe I'm doing something wrong? Any help would be much appreciated.
It looks like this line in the execute method:
protect_from_forgery with: :null_session
is what caused the problem. I'll have to look into this some more. +1 and I'll even mark the answer correct if someone can figure out why this is happening.
Edit: The reason this is happening is because this method assumes you're inheriting from ActionController::Base and not ActionController::API (which doesn't have this method). the API class is supposed to be lighter and therefore doesn't support cookies/sessions out of the box.

How to find out where a function is overridden?

In my Rails project, I saw the link_to is overridden in config/initializers/extend_action_view.rb
module ActionView
module Helpers
def link_to(name, options = {}, html_options = nil)
...
end
end
end
I found that with the famous command line in linux grep -r 'def link_to' *.
My question is : Is there a way to find out it in rails console ? Is there a native function rails or ruby can give us the path of file ? Something like .ancestors for a object.
ps: My IDE is vi
If you use the pry gem, you can find it by using Method#source_location.
Pass binding.pry in your view and render it. Then write:
method(:link_to).source_location
=> ["path_to_helper.rb", 124]
Use $ command in pry gem
Insert binding.pry in your code, which sets a breakpoint and then in the Pry commandline use the $ command
[1] pry(#<AdminController>)> $ Person.find
From: /Users/joe_example/.rvm/gems/ruby-1.9.3-p551/gems/composite_primary_keys-8.1.0/lib/composite_primary_keys/core.rb # line 21:
Owner: ActiveRecord::Core::ClassMethods
Visibility: public
Number of lines: 38
def find(*ids) # :nodoc:
# We don't have cache keys for this stuff yet
return super unless ids.length == 1
...
The $ command is literally worth dollars. It shows you where a method is defined as well as the source code.

I don't see "hello world" showing up in ruby on rails

I just started Michael Hartl's ruby on rails tutorial book (3rd edition). I've been following all the steps in getting rails set up through cloud9. I installed all the necessary gems and file versions.
I set up my server using this code:
~/workspace/hello_app **$ rails server -b $IP -p $PORT**
Than I changed the settings in the application controller:
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: "hello, world!"
end
end
And than I changed the route in routes.rb to:
# root 'application#hello'
I think I'm following the book exactly but when I go to load the URL it still just shows the default page and none of my changes have gone into effect. I don't see "hello, world!". I have no idea what to do.
You need to uncomment
# root 'application#hello'
So it reads
root 'application#hello'
Update your routes.rbfile
root 'application#hello'

Rails - conditionally log for a specific hostname

I'm looking for a way to configure a Rails server log only if the client has contacted a specific hostname. e.g. I could make it so that http://public.example.com doesn't get logged, but http://debug.example.com (same underlying Rails app server) does get logged (or ideally gets logged in more detail than the regular host). It would help with production debugging.
You can use gem Lograge to customize your log. This gem will give you much more custom to your log. For example, in your case, I will do this
After install the gem. Create a file at config/initializers/lograge.rb
# config/initializers/lograge.rb
Rails.application.configure do
config.lograge.enabled = true
config.lograge.custom_options = lambda do |event|
# custom log on specific domain
if event.payload[:host] == "debug.example.com"
{:host => event.payload[:host]}
else
{}
end
end
end
And in your Application Controller
# app/controllers/application_controller.rb
class ApplicationController < ActionController::Base
# This will add request's host to lograge so you can use it to filter log later
def append_info_to_payload(payload)
super
payload[:host] = request.host
end
end
Now you can customize your log base on domain, on how to customize it please read at: https://github.com/roidrage/lograge

Emulation of each_slice without block in ruby < 1.8.7

Im trying to test my rails applications javascript using jruby 1.3.1,celerity and culerity.
The application itself runs under ruby 1.8.7 + phusion passenger (and runs fine, sans test :))
Everything installation-wise works fine but my app uses some_enumerable.each_slice(10) to split a larger array into smaller subarray with 10 elelents each.
Celerity need jruby and jruby is only ruby 1.8.6 compatible and therefor doesnt support a blockless each_slice.
So I'm thinking about defining an initalizer which adds this functionality if RUBY_PLATFORM == "java " (or RUBY_VERSION < 1.8.7)
This far I got (defunct code of cause):
if true #ruby 1.8.6
module Enumerable
alias_method :original_each_slice, :each_slice
def each_slice(count, &block)
# call original method in 1.8.6
if block_given?
original_each_slice(count, block)
else
self.enum_for(:original_each_slice, count).to_a
end
end
end
end
This code obviously is not working and I would really appreciate someone pointing me to a solution.
Thanks!
Update:
Solution thanks to sepp2k for pointing me to my errors:
if RUBY_VERSION < "1.8.7"
require 'enumerator'
module Enumerable
alias_method :original_each_slice, :each_slice
def each_slice(count, &block)
if block_given?
# call original method when used with block
original_each_slice(count, &block)
else
# no block -> emulate
self.enum_for(:original_each_slice, count)
end
end
end
end
original_each_slice(count, block) should be original_each_slice(count, &block).
Also if you leave out the to_a, you'll be closer to the behaviour of 1.8.7+, which returns an enumerator, not an array.
(Don't forget to require 'enumerator' btw)
checkout the 'backports' gem :)

Resources