Start Ruby on Rails

In fact,I have started ROR before,but I have not project to do with it,so I have forgot it.Now I need to start again,but the all I have got before are forgot,orz.Now this blog is about my learning journey.I will continuing to updating it. css

2013/4/24 html

I have caught three issue when I work with the Rails guide: web

guide:http://guides.rubyonrails.org/getting_started.html and http://guides.ruby.tw/rails3/getting_started.html ruby

At first I using the second link,I got the error of error_message issue,but when I google it,it told me that this line code has been removed from the new guide,which is the first link,you only need to remove the code,all will be okay. app

second issue:post error,that's because I have forgot add the line into the comment class's comment.rb,it is  "belongs_to :post ",only need this code,this issue can be fixed. less

third issue:if you want to learn it and you are a new comer,so you may want to write code by yourself,not copy the code from document,so often you will write the wrong word,e.g,you forget to write the last letter of the word,or you write the wrong letter,etc. ide

forth error: can's mass-assign protected attributes: tags_attributes.And now I still don't know how to fix it,any ideas? post

Just now I have found the way to fix it,the link is:http://stackoverflow.com/questions/9441819/cant-mass-assign-protected-attributes-tags-attributes ui

I have modified the first line:attr_accessible :content, :name, :title to attr_accessible :content, :name, :title, :tags_attributes this

only add ":tags_attributes" in the last.In fact,the new first has told me,but I have not got it first.orz.

2013/4/26

Today when I read the book named "Agile Web Development with Rails",I have got a very old information,but now I just get it.It is for the modal's meaning,it said that modal is not only the vector of data,also include the basic operator for data,e.g:product,price less than $20,can not be off,this logic should be completed by modal.

What's use case?use case is describing user entity how to use the system.For a online shop.use case is:customer use to browse the products and purchase them,vender set the depot's product list and its property,so simple!

2013/4/28

I found the route rule is the main of rails.

2013/5/3

Today I need to record the basic command of ROR so that I can use it better.

- rails new projectName

Use it to create a new project in your current directory,'projectName' is the name you want for your project.

- rails server rails s

Use it to start the web server so that you can visit the app in the browser

- rails console / rails c

Use it to open the console

- bundle install

Use it to install all the gems in the Gemfile of your for app

- rspec spec/

Use it to test the spec folder's test code

- projectName/mate .

Use it to use textmate to open the project

2013/5/13

If you want to use Guard and Spork to help you run the test,you may catch the issue like this:

- /Users/Name/.rvm/gems/ruby-2.0.0-p0/bin/guard: No such file or directory - growlnotify

You should install the growlnotify in you computer.the link is:http://growl.info/downloads

Please download the General Download's growlnotify.And don't need to download Growl,because it needs purchased and we only growlnotify.

-ERROR - Guard::Spork failed to achieve its <start>, exception was:
> [#78014015D7C8] ChildProcess::LaunchError: No such file or directory - bundle exec spork -p 8989

This issue has been caught by many developers,so you can google it.You can do like this to fix this issue:

- add the word to your app's Gemfile->gem 'childprocess', '0.3.6'

- run the command->bundle update childprocess

Then it is okay.

2013/5/14

I want to use '/about' to replace '/static_pages/about',and "Ruby on Rails Tutorial" has told us how to do it.But when I test home page using "rspec spec/requests/static_pages_spec.rb",I always get the error:

1) Static pages Home page should have the h1 'Sample App'
     Failure/Error: page.should have_selector('h1', :text => 'Sample App')
       expected css "h1" with text "Sample App" to return something
     # ./spec/requests/static_pages_spec.rb:11:in `block (3 levels) in <top (required)>'


  2) Static pages Home page should have the custom page title
     Failure/Error: page.should have_selector('title',
       expected css "title" with text "| Home" to return something
     # ./spec/requests/static_pages_spec.rb:16:in `block (3 levels) in <top (required)>'


  3) Static pages Home page should have the base title
     Failure/Error: page.should have_selector('title', :text => "Ruby on Rails Tutorial Sample App")
       expected css "title" with text "Ruby on Rails Tutorial Sample App" to return something
     # ./spec/requests/static_pages_spec.rb:22:in `block (3 levels) in <top (required)>'

And the other pages,such as help,about,contact,all are okay,how is it?

The book give the answer,we need to delete the public/index.html,then test again,the error will be fixed.

2013/7/5

1-Start to learn our web project.We always use config/routes.rb to write all the routes.And there will many key word to present the browser's request,e.g:GETPOST,PATCHPUT and DELETE.And I also find that many routes will use 'match'

so match is means the browser's all path are use this route.For example:

get '/about' to: "static_pages#about"
 is equal to 
match '/about' to: "static_pages#about", via: get

2-Routes rule

resourceful routes rule format:

resource :user
the result is:
HTTP Verb Path action used for
GET /users index display a list of all users
GET /users/new new return an HTML form for creating a new user
POST /users create create a new user
GET /users/:id show display a specific user
GET /users/:id/edit edit return an HTML form for editing a user
PATCH/PUT /users/:id update update a specific user
DELETE /users/:id destroy delete a specific user

2013/7/9

In Rails,if you have completed some operating,for example,if you generate a controller,but then you found you don't like the controller's name,want to change,so how would you do?To change the file's name by yourself?

No,one reason is that if you generate one controller,the rails will create one more file in the project,if you do via yourself,maybe you will forget to modify somewhere,and then you will catch some issues.so the best way is to use rails's command:

if you generate a controller like this:

rails generate controller controller_1 method_1 method_2

you can run the command like this to undo the pre-operating:

rails destroy controller controller_1 method_1 method_2

then you can generate the controller using the new name you want to generate the controller

The operating is also available for Model.

If you generate a model like this:

rails generate model User name:string email:string

you can do like this to undo the operating:

rails destroy model User

For database's migrate operating,you can do like this:

before you do like this:

rake db:migrate

then you can do like this to undo:

rake db:rollback

or

rake db:migrate VERSION=0

of course,you can use the version index you want to replace the number "0".

Good luck!

Two methods of rails will use often are:provide and yield

example: <% provide(:title, 'Home') %> is put the string 'Home' to the variable ':title'

<%= yield(:title) %> is to output the variable ':title',and then put the value in the current location.

......

Thanks,

Blues