Bucket Place/Ruby on Rails

[Ruby on Rails] 메모2...

Cloud Travel 2014. 5. 12. 14:03

* Add new Model
 1. controller generate
   [example] $ rails g controller Users new

 2. model generate

   [example] $ rails g model User name:string email:string

 3. Databse migration

   [example] $ rake db:migrate

 4. Model setting

   [Where] $ vi app/models/user.rb

   If you access to model's page(for example, http://localhost:3000/users/1), You must see this page.

 

   This page  is seen when a model is not RESTful Architecture.

   So, You must make model to RESTful Architecture. Follow next step.

 5. Add resource in router.rb

  - Make model to RESTful Architecture

   [example] $ vi config/routes.rb

   ...

   resources :users

   ...

   If you re-access to model's page(upon URL), You see this page now.

 

   This page is seen when a controller is not existed.

   So, You must add actions which you need in controller.

 6. Add action in controller & view

   [example] $ vi app/controller/user_controller.rb

   [example] $ vi app/views/user/show.html.erb



* Strong parameters

 - In create method(of RESTful Architecture), It is not good at using parameter which is sent by users.

  [example] @user = User.new(params[:user])

 - It occurs security problem(like SQL injection or access not allowed attribute in database)

 - So, Rails gives you "strong parameter". You can choose use some attribues or not.

   [example] @user = User.new(params.require(:user).

permit(:name, :email, :password, :password_confirmation)

 - This action is used another methods, so to create method for strong parameters.

   [example]

   private

     det user_params

       params.require(:user).permit(:name, :email, :password, :password_confirmation)

     end