Ruby/Rails - How to Create a Class and Access It from the Controller

Ruby/Rails - How to Create a Class and Access it from the Controller

You should put location.rb wherever you feel it makes the most sense. Having it at app/models/location.rb will ensure that it's automatically required when your app starts, but some people expect that classes in app/models are backed by ActiveRecord.

You could also put it under lib/ if you prefer.

To make it available to the app, you can include require statement in project initializers inside your config folder:

require "#{Rails.root}/lib/location.rb"

As for creating it inside your Controller - definitely! It's just another instance of a class:

def show
@location = Location.new("My House", 12.345, 56.789)
end

And then in your view:

<%= draw_map_of @location %>

Don't forget – beneath Rails is all the power and flexibility of pure Ruby, ready to be used. You're not only limited to what Rails gives you.

Access method in controller from model

The concept which you are talking of is introducing a class method. The method needs to be defined on the Work class itself with the help of the self keyword.

It can be done as follows:

class Work < ActiveRecord::Base
def self.testMethod
return "string from test method"
end
end

To answer your second question, Rails being an opinionated framework, forces the pattern of pluralizing your Controller and Table names. This is really for being more grammatically correct.

In case you wish to tweak the pluralization you can do so by using the Inflections.

Rails: Loading custom class from lib folder in controller

Ruby on Rails requires following certain naming conventions to support autoloading.

Rails can autoload a file located at lib/services/my_service.rb if the model/class structure was Services::MyService.

Change your lib/services/my_service.rb to:

module Services
class MyService
# ...
end
end

And use that class like this in your controller:

service = Services::MyService.new

Please note that depending on your Ruby on Rails version, you might need to add the lib folder to the list of folders which are queried when looking for a file to autoload:

# add this line to your config/application.rb:
config.autoload_paths << "#{Rails.root}/lib"

Read more about autoloading in the Rails Guides.

how to create an action controller helper class and how to access that in controller action?

While there are cases where it makes sense to use a background processing. This is not one of them.

I assume 'show result to user' means rendering some template based on the data fetched from the api.

Actually, it might be a good idea to move accessing the external API into background, but that would require changing the flow. As for the updating the db record, its generally not a good idea to move it to background.

that being said, I wouldn't do the update in the controller, I'd move it into the model or a 'mutation' class:

class Model
def self.update_from_api
res = API.fetch ...
object = find res.id
object.update_from_api! res
res
end

def update_from_api(api_data)
update_attributes! info: api_data.gsub(....)
end
end

How does a Rails controller get access to a model?

This is not magical, and this is not limited to controllers and models, Rails (depending on the version you are using) autoloads every class and module under the app/ directory, meaning you have access to any class from any other class in the whole project. So if you add a new directory and file under the app/ directory, like app/services/foo_bar.rb. You can also access that from your controller, or your model, or from another service class, eg:

class ArticlesController < ApplicationController
def new
@article = Article.new
FooBar.do_something(@article)
end
end

or:

class Article < ApplicationRecord
#....

private

def lets_all_foo_our_bars
FooBar.foo_my_bar
end
end


Related Topics



Leave a reply



Submit