I am making a web application using Sinatra, Ruby, SQLite. When the user logs in, I need to record their username and password into the table. If this is the code for the main ruby file, what do I put in the erb file? Thank you!
require 'dm-core'
require 'dm-migrations'
DataMapper.setup(:default, "sqlite3://#{Dir.pwd}/user.db")
class User
include DataMapper::Resource
property :id, Serial
property :name, String
property :password, String
end
Getting individual column values in row:
#user[0].name
#user[0].password
Related
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
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.
I need pages to be attached to layouts only when users opting for this.
I mean when users editing pages, there are a dropdown to select layout from.
It works well if some layout selected.
However if user selecting <option value='0'> None option,
DataMapper throw an error saying layout_id should be greater than zero.
I think this should not happen cause i set required: false on belongs_to :layout association.
Here are my models:
class Layout
include DataMapper::Resource
property :id, Serial
property :name, String
end
class Page
include DataMapper::Resource
property :id, Serial
property :name, String
belongs_to :layout, required: false
end
You right about "under-the-hood" validation.
It is automatically added by belong_to association.
And you can get rid of it by redefining layout_id property.
In Page model simply add:
property :layout_id, Integer, index: true
This will keep the association but will redefine layout_id property
so it wont have automatically added validations on it.
However note that this will work only after Page.auto_migrate!
Or you can manually remove foreign key from your pages table.
Also, make sure layout_id is a index, otherwise you'll have performance issues.
Whatever columns/types/relations i'm using within my DataMapper models i'm always get same fatal error:
undefined method `include?' for nil:NilClass
a sample model:
class Book
include DataMapper::Resource
property :id, Serial
property :name, String
end
Even with this trivial model i get that weird error.
Latest datamapper, reinstalled to be sure it is no broken somehow.
Ruby 1.9.3
Mysql 5
Sequel works just well on same environment.
did you call DataMapper.finalize after defining your models?
try:
class Book
include DataMapper::Resource
property :id, Serial
property :name, String
end
DataMapper.finalize # this is required on any scenario
Official docs:
http://datamapper.org/getting-started.html
See Finalize Models at the bottom
I'm working with DataMapper and trying to use associations between models Project and Task. I have the models in separate files project.rb and task.rb. When I try associating them with each other I get the following error:
Cannot find the parent_model Project for Task in project (NameError)
I gather this is caused by project.rb requiring task.rb and vice versa, since the association works fine if I just include it in one of the files. Here's the code:
project.rb
require 'dmconfig'
require 'task'
class Project
include DataMapper::Resource
property :id, Serial
has n, :tasks
end
DataMapper.auto_upgrade!
DataMapper.finalize
task.rb
require 'dmconfig'
require 'project'
class Task
include DataMapper::Resource
property :id, Serial
belongs_to :project
end
DataMapper.auto_upgrade!
DataMapper.finalize
dmconfig.rb
require 'rubygems'
require 'dm-core'
require 'dm-migrations'
DataMapper::Logger.new($stdout, :debug)
DataMapper.setup(:default, 'sqlite://' + Dir.pwd + '/taskmanager.db')
If I remove the association from one of the files it works fine, at least from one direction:
require 'dmconfig'
class Project
include DataMapper::Resource
property :id, Serial
end
DataMapper.auto_upgrade!
DataMapper.finalize
If I want the association to work from both directions is the only reasonable solution to just put both classes in the same file? Or is there a way that I can keep them separated and still manage it?
You need to call finalize after you require all your models, not after each one. One of the things finalize does is sanity check your models, to make sure all the relevant models have been required. The application boot process, after requiring all the library files is an ideal place to do this. I suggest something like:
project.rb
class Project
include DataMapper::Resource
property :id, Serial
has n, :tasks
end
task.rb
class Task
include DataMapper::Resource
property :id, Serial
belongs_to :project
end
dmconfig.rb
require 'dm-core'
require 'dm-migrations'
require 'project'
require 'task'
# note that at this point, all models are required!
DataMapper::Logger.new($stdout, :debug)
DataMapper.setup(:default, 'sqlite://' + Dir.pwd + '/taskmanager.db')
DataMapper.finalize
DataMapper.auto_upgrade!
Or something of that nature. In your application, you require 'dmconfig' and have everything set up with that one require. DataMapper defers checking for the far end of relationships (say, projects in the Task model) until you call finalize or auto_upgrade!, so make sure all the models are required before you do this.
It looks like that might be caused by a typo in task.rb
belongs_to, :project
should be written as:
belongs_to :project
And for what it's worth, when using Sinatra, for example, I prefer to keep all of my models together in one lib/models.rb file... at least for as long as that's manageable.
First of all, call DataMapper.finalize before you call auto_upgrade. Secondly, it's better to load the models, call finalize and then do DataMapper.auto_migrate! instead of calling auto_upgrade after each model definition.