Ruby require directive causes an error - ruby-on-rails

Trying to create a simple flickr application using Ruby on Rails 3.2.3, I faced a problem. First I installed a gem called flickraw
sudo gem install flickraw
Then I tried to use it
require 'rubygems'
require 'flickraw'
class HomeController < ApplicationController
def index
end
def index2
end
def index3
end
end
And I got an error:
LoadError in HomeController#index
cannot load such file -- flickraw
Any suggestions?

You should not require gems from the controller. You should just add flickraw to your Gemfile
gem "flickraw"

Related

undefined local variable or method `byebug', already in Gemfile and required

I have the same problem as this question. I use byebug in this class:
class BasesController < ApplicationController
before_action :set_available_bases, only: [:index]
def index
end
private
def set_available_bases
byebug
#bases = Base.all
end
end
And I get
NameError (undefined local variable or method `byebug' for #<BasesController:0x5952e10>):
If I add require 'byebug' as proposed in the first answer, I get cannot load such file -- byebug. byebug is already, by default with Rails 5, in my Gemfile
group :development, :test do
# Call 'byebug' anywhere in the code to stop execution and get a debugger console
gem 'byebug', platform: :mri
end
I have already run gem install byebug, bundler install. I have ruby 2.3.3p222, rails 5.0.1, running on Windows 10.
Any idea of how to solve this?

Making Jbuilder work with Rails 5 API Mode

I want to use Jbuilder with Rails 5.0.0.beta1.1 in API mode. Out of the box, it doesn't work, even when creating the app/views directory.
For example, I have:
# app/controllers/tests_controller.rb
class TestsController < ApplicationController
# The requests gets inside the action
def test
end
end
# app/views/tests/test.json.jbuilder
json.test "It works!"
The error I'm getting is
No template found for TestsController#test, rendering head :no_content
I guess I have to change some things in the config files. What do I have to do?
Doing an explicit render from the controller like this works:
render 'controller_name/action.json.jbuilder'
With API mode.
You need include module like bellow
class ApplicationController < ActionController::API
include ActionController::ImplicitRender # if you need render .jbuilder
include ActionView::Layouts # if you need layout for .jbuilder
end
I got the same error, but in my case I had simply forgotten to add the jbuilder gem in the Gemfile:
gem 'jbuilder', '~> 2.5'

Authority gem gives 'undefined method'

I'm getting undefined method 'authorize_actions_for' in the controller when using rails-api with the Authority gem. Any ideas what I need to include? Here's my code:
Gemfile:
...
gem 'authority', '~> 2.9.0'
gem 'rails-api', '~> 0.1.0'
...
app/authorizers/session_authorizer.rb:
class SessionAuthorizer < ApplicationAuthorizer
def deletable_by?(user)
user.sessions.include?(resource)
end
end
app/controllers/v1/sessions_controller.rb:
class V1::SessionsController < ApplicationController
authorize_actions_for Session
...
end
Include Authority::Controller
As we discussed on Github, authorize_actions_for is defined in Authority::Controller.
In a normal Rails app, that module gets included into ActionController::Base by Authority's railtie.rb. Apparently the railtie isn't being required when using rails-api (maybe the Rails constant isn't defined?).
You can include the appropriate module yourself, though:
class ApplicationController < ActionController::Base
include Authority::Controller
end

Rails: error in setup google-api-client

in Rails (rails 4.0.0 rc2, ruby 1.9.3p429), in the Gemfile i've the following line:
gem 'google-api-client', :require => 'google/api_client'
and then bundle install.
In a controller:
require 'google/api_client'
class PagesController < ApplicationController
def home
#client = Google::ApiClient.new
end
end
If i go to 127.0.0.1:3000 i've the following error:
NameError in PagesController#home
uninitialized constant Google::ApiClient
Anyone can tell me how integrate the ruby google api in rails?
Thanks
It is APIClient.new.
#client = Google::APIClient.new
You should also set the application name and version number or you get an ugly stdout when run your code. Like so :
#client = Google::APIClient.new(:application_name => "MyApplication",:application_version => "0.1")

Uninitialized constant of gem

I've installed JWT gem - https://github.com/progrium/ruby-jwt
There is gem 'jwt' in Gemfile, of course. Inspite on that, I have the error of
uninitialized constant HomeController::JWT
in HomeController
class HomeController < ApplicationController
def method1
#jwt_token = JWT.encode({.... })
end
end
I believe you need a require 'jwt'.
HTH

Resources