I want to show pretty URLs for my posts like year/month/title and don't want to show the id or controller name.
How can I do this in Ruby on Rails?
You should check out the friendly_id gem which lets you set another attribute than id to resolve models into URLs and vice versa.
Using this gem to generate user URLs by nick:
class User < ActiveRecord::Base
extend FriendlyId
friendly_id :name, use: :slugged
end
User.create! name: "Joe Schmoe"
GET http://localhost:3000/users/joe-schmoe
Related
I am trying to set up an application where I can route via a String key instead of an id attribute. To illustrate this, please consider the following:
I have a class Foo which inherits from the ActiveRecord::Base and is implemented as follows:
class Foo < ActiveRecord::Base
attr_accessible :subject
Subject is a string type which exists in my database.
I have a controller which I implement as follows:
class SubjectController < ApplicationController
def index
#snip
end
As you can see, the SubjectController inherits from the ApplicationController.
Running rake routes gives me the standard (GET, POST, PUT, DELETE) routes for my subject. This is the expected behavior and I understand this functionality.
I want to know how I can extend the routes.rb file so that I can use a string url in order to access a subject. For example:
Instead of typing in localhost:3000/subject/1, I would like this /:id to resolve when I type in the url: localhost:3000/subject/grumpy-cat-says-hello
What does this implementation look like?
How should I setup my routes.rb file to accommodate this?
How should I configure my application to allow for this type of implementation?
Thank you in advance.
I've always used https://github.com/FriendlyId/friendly_id for this stuff.
If you prefer something simpler, this will do
class Foo < ActiveRecord::Base
def to_param
[id, subject.parameterize].join("-")
end
end
Then you can access your resource with: localhost:3000/foos/1-grumpy-cat-says-hello
Basically, since the name of the resource still starts with a number, it will be converted to a number by Rails and everything will work seamlessly.
This Gist goes in much greater detail about this topic :)
I try to write business logic of my application. It is all ruby classes. There is no database or no UI framework like Rails, Sinatra. I only have a Gem_file on business logic and, Gem_file only contain "mini_test" gem. I use mini_test for testing business logic. Now, I need to add a database to the system. How can I do this?
mongoid configuration is made in application.file on Rails. But ,I don't use Rails or any other framework. Is there anyway to make configuration of mongoid without framework like Rails, Sinatra.
I hope I can explain my problem. Also, I add my codes in below:
this is my context-
class HeadTeacherDefineAcademicYearContext
attr_reader :person, :academicyear
def initialize(person, academicyear)
#person, #academicyear = person, academicyear
#person.extend HeadTeacher
end
def call
#person.define_academic_year #academicyear
end
end
this is my role module
module HeadTeacher
def define_academic_year(academicyear)
#i write db save process here using any database
end
end
my model class
class AcademicYear
attr_accessor :year
end
You have to include gem 'mongoid' in your Gemfile and install it. After that, you can require and initialize Mongoid where you need it:
require 'mongoid'
Mongoid.load!("mongoid.yml", :development)
It expects a mongoid.yml file with configuration. Examlpe:
development:
sessions:
default:
database: myapp_development
hosts:
- localhost:27017
Of course, you can use another context than :development, maybe assign it via a environment variable. Now, add Mongoid::Document to your model:
class AcademicYear
include Mongoid::Document
field :year, type: Integer
end
Add gem "mongoid", "~> 3.0.0" to your Gemfile
Then put configuration yaml file to your project with contents like this:
development:
sessions:
default:
database: mongoid
hosts:
- localhost:27017
Then use Mongoid.load!("path/to/your/mongoid.yml", :development) in your app.
In every class you want to save objects to DB you have to include Mongoid::Document.
So your example becomes:
class HeadTeacherDefineAcademicYearContext
attr_reader :person, :academicyear
field :person, type: String
field :academicyear, type: Date
...
end
You should better check mongoid docs for stuff to do next.
here's the Activities model
class Activity < ActiveRecord::Base
belongs_to :user
attr_accessible :week, :day, :bcalor, :bicycl, :ccalor, :foodid, :jog,
:lunges, :pushups, :situps, :squats
end
I'd like to add functionality, such as, after I save or update a activity, I be able to update the summary all the acitities. In .NET we have stuff like OnModelUpdating and OnModelUpdated. How do I accomplish the samething using activerecord?
Thanks for helping
In Rails, they are called callbacks. Here you go: http://api.rubyonrails.org/classes/ActiveRecord/Callbacks.html
I have a model group.rb with subclasses organization.rb, company.rb, etc. I'm wondering if there is a way to create an organization with the name "Rails Beginners Society" without also creating a company with the name "Rails Beginners Society"? As it stands right now it looks like if I do something like Organization.find_or_create_by_name(:name => #profile.organization) I end up not only creating an organization, but also a company and all the other subclasses of group.rb with the name I supply in my Controller.
Any ideas/guidance would be much appreciated!
My Models look like this:
class Group < ActiveRecord::Base
has_and_belongs_to_many :users
end
class Organization < Group
end
Etc...
It sounds like what you're trying to do is STI (Single Table Inheritance). Rails does support this.
class Group < ActiveRecord::Base
end
class Organization < Group
end
etc...
If you have a column type in your groups table, Rails should handle everything for you.
I'm planning to create a ruby gem which requires to get all the ActiveRecord models from the directory (typically)
RAILS_ROOT/app/models
how can I get the list of model names (physical) in ruby (ruby 1.9)
cheers
sameera
So, if the class name matches with the file name, you can use something like this:
# models/user.rb
class User < ActiveRecord::Base
end
Dir.glob("./models/*.rb").each {|model| require model}
user = User.new
ActiveRecord::Base.subclasses.collect(&:name)
Returns all the model name.