rails Flashcards

1
Q

Why is the view of Rails application in the format *.erb.html? What does “erb” mean?

A

erb stands for “Embedded RuBy”. A .html.erb or .erb.html file is HTML with Ruby code embedded in; Rails will evaluate the Ruby to add content to the file dynamically, and will output a “pure” HTML file for rendering.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

create a new ruby app named demo

A

sudo rails new demo –skip-bundle

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

add less before installing bundle

A

gem ‘therubyracer’gem ‘less-rails’(added in Gemfile)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

update bundle

A

sudo bundle updatesudo rails server

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

create a new ‘todo’ controller, use ‘index’ as default page, ‘add’ and ‘delete’ as other view pages

A

sudo rails g controller Todos index add delete# Controller name is always plural as per convention. Todos instead of Todo, Tweets instead of Tweet, Blogs instead of Blog.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

what is a rails console and how to enter?

A

rails consoleRails console loads the complete Rails app environment. All the model and controller classes of the Rails app is available inside Rails console.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

what is a model?

A

When you create a model, the name is singular and the name starts with capital letter. Todo, instead of Todos.To get started, it’s important to understand that Rails is built on MVC architecture; that is, Model–View–Controller.Models are responsible for handling data storage and the business logic. Views are responsible for the layer that the user deals with. Controllers act as the bridge between the Models and the Views. In other words, Views generate your HTML, Models handle your database interactions (and any logic based on that data), while Controllers tie the two together, and are where you place logic that controls where the browser is redirected and what the user can do.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

how to create a model and undo the action?

A

rails g[enerate] model Todo todo_item:stringrails d[estroy] model TodoHowever if you have executed rake db:migrate, you need to execute “rake db:rollback”. This will remove the table and all the data from your database as well.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

what is Migration?

A

Migrations are a convenient way to alter your database schema over time in a consistent and easy way. They use a Ruby DSL so that you don’t have to write SQL by hand, allowing your schema and changes to be database independent.You can think of each migration as being a new ‘version’ of the database. A schema starts off with nothing in it, and each migration modifies it to add or remove tables, columns, or entries. Active Record knows how to update your schema along this timeline, bringing it from whatever point it is in the history to the latest version. Active Record will also update your db/schema.rb file to match the up-to-date structure of your database.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

how to use migration?

A

rake db:migratethe command pushes the database changes from the migration file to the actual database. In Rails the default database is sqlite.One of the purpose of Rails model is to shield us from dealing directly with sql database. Writing sql queries for every database task is tedious and rails handles all that for us.Tip You would hardly need to refer to the actual table by the name todos. You would only be accessing the data through the model Todo.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

how to add items into database through rails console: create a todo_item named “sing”

A

Todo.create(:todo_item => “sing”)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

how to access Models(database) in controller?

A

def index @todo_items = Todo.all end

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

how to create a new view file: app/views/todos/delete.html.erb

A

touch app/views/todos/delete.html.erb

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

the possible Model commands:

A

Todo.allTodo.deleteTodo.delete_allTodo.create(:todo_item => “xxx”)t = Todo.newt.todo_item = “xxx”t.saveTodo.firstTodo.lastTodo.find(5)Todo.find_by_todo_item(“xxx”)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

how to add link in the a tag, in the same project?

A

DeleteBack

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

run a second server on port 4000:

A

rails s -p 4000 -P tmp/pids/server2.pid

17
Q

TCPServer Error: Address already in use - bind(2)so how to kill current server port:

A

lsof -wni tcp:3000kill -9 [insert PID number here]

18
Q

how to validate that there is content being added

A

in todo.rbclass Todo < ActiveRecord::Base attr_accessible :todo_item validates :todo_item, presence: trueendvalidates is a Rails model validation helper.

19
Q

how to see all the routs in terminal?

A

rake routes

20
Q

why sometimes I get a wrong msg?

A

sometimes you need to restart server, sometimes you need to refresh page rather than click on Back button