<code class = 'sh' lang = 'sh'> $> sudo apt-get install mysql-server-5.0 libmysql-ruby </ code>
<code class = 'sh' lang = 'sh'> $> mysqladmin ping -u root mysqld is alive </ code>
<code class = 'sh' lang = 'sh'> $> mysqladmin create example_development -u root $> mysqladmin create example_test -u root </ code>
config/database.yml
file. Here are the parameters for connecting to the database. Usually nothing needs to be changed; by default, mysql creates the root user with all rights and without a password.<code class = 'sh' lang = 'sh'> $> rake </ code>
ActiveRecord::Base
<code class = 'ruby' lang = 'ruby'> class Article <ActiveRecord :: Base end </ code>
<code class = 'ruby' lang = 'ruby'> # Suppose there is a title field in the Articles table Article.create (: title => 'Hello World!') article = Article.find (1) print article.title # => Hello World! </ Code>
set_table_name "mytablename"
to the classArticle.find(id)
- find an article by id (PrimaryKey in the database, usually an Integer)Article.find(:all)
- select all articlesArticle.find_by_title('Hello World!')
- find an article with the title “Hello World!”Article.create(:title => 'Hello World!')
- create an article and save to databasearticle.update(:title => 'Goodbye World!')
- update the article in the databasearticle.destroy
- remove an article from the database<code class = 'sh' lang = 'sh'> `$> gem_server` </ code>
script/generate
to create a model, controller, and views for the directory.<code> $> ruby ​​script / generate scaffold_resource article title: string body_format: string body: text </ code>
app/models/article.rb
- model for the articleapp/controllers/articles\_controller.rb
- the controller for managing the article directoryconfig/routes.rb
- added map.resources :articles
line map.resources :articles
app/views/articles/...
- views for creating, editing and viewing articlesapp/views/layouts/articles.rhtml
- template pages for working with the directorydb/migrate/001_create_articles.rb
- create a table for database articlesindex
- the page displays the entire collection of articles.show
- page displays one articlenew
- page for creating a new articleedit
- the page for editing an existing articlecreate
- handler post form create a new articleupdate
- the post processor of the article editing formdestroy
- article deletion request handlermap.resources :articles
in the config/routes.rb
adds the necessary URL routing rules. Here is what urls look like:/articles (GET)
- index/articles/:id (GET)
- show (: id - article id)/articles;new (GET)
- new/articles/:id;edit (GET)
- edit/articles (POST)
- create/articles/:id (PUT)
- update/articles/:id (DELETE)
- destroy<code class = 'sh' lang = 'sh'> `$> ruby ​​script / server` </ code>
<code> Mysql :: Error: Table 'example_development.articles' does not exist: SELECT * FROM articles </ code>
articles
table in the database, it would be necessary to create it.articles
table ( db/migrate/001_create_articles.rb
). We need to apply it to the database, go to the folder with the application and run<code> $> rake db: migrate </ code>
make
, ant
, maven
.<code class = 'sh' lang = 'sh'> $> rake -T </ code>
require 'tasks/rails'
. For example, the db:migrate
task description looks like this:<code class = 'ruby' lang = 'ruby'> desc "Migrate the database through scripts in db / migrate. Target specific version with VERSION = x" task: migrate =>: environment do ActiveRecord :: Migrator.migrate ("db / migrate /", ENV ["VERSION"]? ENV ["VERSION"]. To_i: nil) Rake :: Task ["db: schema: dump"]. Invoke if ActiveRecord :: Base.schema_format ==: ruby end </ code>
ant
or maven
.db:migrate
task starts migrations, and you can both apply and roll back the changes. If we look in the db/migrate/001_create_articles.rb
, we will see that the CreateArticles
class has two methods: up and down. These methods are called when the migration is applied and rolled back accordingly. The numbers 001
in the file name are the sequence number of the migration, it is used to determine the order in which the migrations are applied, while the rails store its version in the database in order not to apply one migration several times. To migrate the database to a specific version, you need to run db:migrate
with the VERSION parameter:<code> $> rake db: migrate VERSION = 0 </ code>
db:migrate
again without parameters, in the end all migrations will be applied.Article
model now appeared, but if we place the code in the model, then we tightly associate the formatting with a specific model, and this will lead to the fact that we cannot reuse the code , although it does not depend on the model that will be formatted.<code> class Article <ActiveRecord :: Base acts_as_formatted: body end </ code>
body
field, and the formatted text of the article can be obtained using the body_as_html
method. The format in which the article is written is in the body_format
field.<code> $> ruby ​​script / generate plugin acts_as_formatted </ code>
vendor/plugins
folder. What's inside:lib
- this folder contains the codelib/acts_as_formatted.rb
- there will be a plugin codetasks
- the plugin can add tasks for Rake, they will appear in the general listtest
- the plugin should be well testedinit.rb
- this file is executed when loading, hence the plug-in files included in the lib
install.rb
, uninstall.rb
- these files are executed when installing and removing the plugin, we will not need themRakefile
- file with Rake tasks for the plugin (running tests and generating documentation)acts_as_formatted
method to ActiveRecord::Base
. And in this method, generate the code needed to support formatting. To do this, we need to know how to do this in Ruby.<code class = 'ruby' lang = 'ruby'> print "Hello World!" </ code>
extend
and include
methods of modules and classes, for example:<code class = 'ruby' lang = 'ruby'> class MyClass extend enumerable end </ code>
<code class = 'ruby' lang = 'ruby'> class MyClass end MyClass.extend (Enumerable) </ code>
Enumerable
module will appear in the MyClass
class.acts_as_formatted.rb
file.ActiveRecord::Acts::ActsAsFormatted::Formatting
- the code responsible for formattingActiveRecord::Acts::ActsAsFormatted::ClassMethods
- the only method in it is acts_as_formatted
, this module will be added to ActiveRecord::Base
acts_as_formatted
method acts_as_formatted
.body
, body_format
, body_as_html
), then we add a validation rule to check that the format field contains a valid format (the object cannot be saved if it does not pass validation), and we add two methods to the class get supported formats and formatted fields ( supported_formats
and body_as_html
).ActiveRecord::Acts::ActsAsFormatted::Formatting
module. There are methods that format the text: format
, format_markdown
and format_textile
, and the supported_formats
method, which defines the supported formats, based on the methods that are in the module.init.rb
:<code class = 'ruby' lang = 'ruby'> require 'acts_as_formatted' ActiveRecord :: Base.extend (ActiveRecord :: Acts :: ActsAsFormatted :: ClassMethods) </ code>
app/helpers/application_helper.rb
. The edit_form_for
method determines whether the model has already been saved and, depending on this, generates a form for creating or updating the model. The submit_edit
and cancel_edit
create a button to submit the form and a link to return from the form.new
and edit
view code becomes quite simple and comes down to one line:<code class = 'ruby' lang = 'ruby'> <% = render: partial => 'article',: object => @article%> </ code>
preview
method in the controller that will return the formatted text of the article.<code class = 'ruby' lang = 'ruby'> def preview article = Article.new (params [: article]) render_text article.body_as_html end </ code>
<code class = 'ruby' lang = 'ruby'> map.resources: articles, : collection => {: preview =>: any} </ code>
/articles;preview
. :collection
means that the collection url will be used ( /articles
), since it does not matter for which particular article the preview is generated. Instead of :collection
you can use :member
, then the URL will be for a specific article ( /articles/:id
), in our case it will not allow you to make a preview of the articles being created. :any
means that you can use any HTTP method for the call, in our case POST will be used when creating and PUT when editing.<code class = 'ruby' lang = 'ruby'> <% = javascript_include_tag 'prototype'%> </ code>
<code> script / destroy controller input preview </ code>
<code> $> rake rails: freeze: gems </ code>
vendor/rails
folder, and the server will use it, so you will no longer need to worry about what version the hoster has.<code> $> gem install gemsonrails </ code>
<code> $> gemsonrails Installed gems_on_rails 0.6.4 to ./vendor/plugins/gemsonrails </ code>
<code> $> rake -T ... rake gems: freeze # Freeze a RubyGem into this Rails application; init.rb will be loaded on startup. rake gems: link # Link a RubyGem into this Rails application; init.rb will be loaded on startup. rake gems: unfreeze # Unfreeze / unlink a RubyGem from this Rails application ... </ code>
gems:link
- adds to vendor/gems
code that loads vendor/gems
when loading an application, if there is no gem, then the application will not load (it is convenient to find out that there are no gems right away, and not during operation)gems:freeze
- makes a local copy of the heme, this copy will be used in the applicationgems:unfreeze
- deletes the local copy and code generated by gems:link
<code> $> rake gems: freeze GEM = maruku $> rake gems: freeze GEM = redcloth </ code>
maruku-0.5.6
and RedCloth-3.0.4
folders in the vendor/gems
folder.Source: https://habr.com/ru/post/12570/
All Articles