기본적인 문법 class PostsController < ApplicationControllerdef edit@post = Post.find(params[:id])if session[:user_id] != @post.user_idflash[:notice] = "Sorry, you can't edit this post"redirect_to post_pathendendend 대체가능한 문법class PostsController < ApplicationControllerdef edit@post = Post.find(params[:id])if session[:user_id] != @post.user_idredirect_to (post_path, notice: "Sorry, you can't edit this p..
기본 Create 문법 t = User.newt.name = "Jinny"t.phone = "01012341234"t.save 대체가능 Create 문법1t. = User.new(name: "Jinny"phone: "01012341234") t.save 대체가능 Create 문법2t = User.create(name: "Jinny", phone: "01012341234") 기본 Update 문법 t = User.find(3)t.name = "Jinny"t.phone = "01012341234"t.save 대체가능 Update 문법1t = User.find(3)t.attributes = {name: "Jinny"phone: "01012341234"} t.save 대체가능 Update 문법2t = User...
Rails 에서 외부 폰트의 위치 경로와 사용 시 path 입력 방법을 알아보겠습니다. 환경 : Ruby 2.0.0-p598 Rails 4.1 위의 그림과 같이 app - assets 하위에 기존의 images, javascripts, stylesheets 와 동일한 위치에 fonts 디렉터리를 생성해줍니다. 그리고 config/application.rb 에 다음과 같은 내용을 삽입해 줍니다. config.assets.paths
어떠한 Action을 취한 후 이전 URL로 돌아가는 경우는 흔히 있다. 예를들어 해당 게시글을 수정 혹은 삭제 후 다시 원래 URL로 돌아가는 것이 당연하다.이를 구현하기 위해서는 해당 Controller 에서 이전 URL을 저장하고 Action이 끝난 후 저장해둔 URL로 redirection 시켜주면 된다. 예를들어 해당 report 삭제 후 다시 url 로 돌아가는 것을 구현하고자 하면 destory action에 이전 url을 저장한다.store_location 은 private method 로 다음와 같다. 그리고 원하는 행동(예제에서는 해당 report 를 destroy 후) redirect_to 를 이용해 저장해둔 url로 redirection 시켜주면된다.
Rails 4 에서 Paperclip 과 Image Magick 으로 파일 업로드 및 이미지 처리를 할 수 있습니다. Paperclip 을 이용한 프로필 사진 기능을 만들어보도록 하겠습니다. Requirements1. Ruby 버전 1.9.2 이상2. Rails 버전 3.0 이상3. ImageMagick 설치 1. ImageMagick 설치$ sudo apt-get install imagemagick$ sudo apt-get instal libmagickwand-dev 2. Paperclip gem 설치Gemfile 에 추가gem "paperclip", "~> 4.1"$ bundle install 3. User 모델에 컬럼추가$ rails g paperclip user avatar$ rake db:mig..
Rails 3.2.12 버전 db:create creates the database for the current envdb:create:all creates the databases for all envsdb:drop drops the database for the current envdb:drop:all drops the databases for all envsdb:migrate runs migrations for the current env that have not run yetdb:migrate:up runs one specific migrationdb:migrate:down rolls back one specific migrationdb:migrate:status shows current migr..
Ubuntu 에서 Ruby & Rails 설치하기 1. RVM 설치$ curl -sSL https://get.rvm.io | bash -s stable 2. RVM 실행$ rvm get stable 3. Ruby 설치에 필요한 패키지들 설치$ rvm requirements ("Command not found" 에러시)$ source ~/.rvm/scripts/rvm 4. 패키지 management 설치$ apt-get install libyaml-dev 5. RVM의 OpenSSL 환경변수 등록$ rvm install 2.0.0 --with-openssl-dir=$HOME/.rvm/usr 6. Rails 애플리케이션을 실행하기 위한 필요한 Gemset 설정rvm use 2.0.0@railstutorial..