- Lightning Rails
- Posts
- How to Add AI to Your Rails App (in 5 Minutes)
How to Add AI to Your Rails App (in 5 Minutes)
I am working on a new AI tool so it is perfect time to share with you a simple way to add AI to your Rails app.
🤯 300% More AI in Your Rails App? Yes, Please.
AI is everywhere, and now Lightning Rails makes it ridiculously easy to integrate OpenAI’s API (or even DeepSeek if you’re feeling adventurous).
I got tired of manually setting up AI-powered features in every Rails project, so I figured:
Why not bake it into Lightning Rails?
Now, with just a few tweaks, you can have ChatGPT analyze products, generate insights, or help automate user workflows inside your app. Let’s get you set up in 5 minutes or less. 🛠️
🚀 Step 1: Get Your OpenAI API Key
First, head over to OpenAI’s dashboard and sign in (or sign up).
Navigate to API Keys.
Click Generate New Key and copy it somewhere safe (you won’t see it again).
That’s it. Step 1 complete. ✅
💎 Step 2: Install the OpenAI Ruby Gem
We need a simple gem to talk to OpenAI’s API.
In your Gemfile, add:
gem "ruby-openai"
Then run:
bundle install
Boom. You’re now AI-enabled. 💡
🔐 Step 3: Securely Store Your API Key
Throw this inside your .env
file (or set it up with Rails credentials if you’re fancy):
OPENAI_ACCESS_TOKEN=sk-...MIQ4
If you don’t have dotenv installed yet, add it to your Gemfile and run bundle install
:
gem "dotenv-rails"
Done? Good. Now let’s get to the fun part. 🎉
🛠️ Step 4: Build an AI-Powered Service
We’ll create a simple service object to handle AI requests.
Inside app/services/open_ai_api.rb
, add this:
# app/services/open_ai_api.rb
class OpenAiApi
def initialize
@client = OpenAI::Client.new(access_token: ENV["OPENAI_ACCESS_TOKEN"])
end
def analyze_product(product)
chatgpt_response = @client.chat(parameters: {
model: "gpt-4o-mini",
messages: [
{ role: "system", content: "You are a product analyst expert..." },
{ role: "user", content: "Here is a product to analyze: #{product}..." }
]
})
chatgpt_response["choices"][0]["message"]["content"]
end
end
This little piece of code turns ChatGPT into your product analyst—it can break down product features, suggest improvements, and even analyze market potential. 📊
💡 Bonus tip: Copy and adapt this prompt to your needs, it was published on twitter by Greg Brockman, the current president of Open AI

🎯 Step 5: Use AI Inside Your Rails Controllers
Now let’s wire this up inside a controller:
class ProductsController < ApplicationController
def analyze
@product = params[:product]
openai_service = OpenAiApi.new
@analysis = openai_service.analyze_product(@product)
end
end
And don’t forget to add a route in config/routes.rb
:
post "/analyze_product", to: "products#analyze"
That’s it! Your Rails app can now think. 🧠
🔥 What’s Next? AI + Lightning Rails = Magic
With AI in your Lightning Rails app, the possibilities are endless:
✅ Automate content generation
✅ Enhance user experiences with smart recommendations
✅ Analyze user feedback & products instantly
✅ Improve internal workflows with AI-driven automation
Want to see it in action? Try it out today and let me know what cool AI features you build with Lightning Rails. 🚀
Happy coding! 🦾💡
PS: I gave a huge discount to the Lifetime membership to celebrate the Rails 8 update (50% off). If you were on the fence about getting Lightning Rails, now is your chance.
Reply