WEB+DB 詳解Ruby on Rails ãæµã ãã®ï¼ æ¤ç´¢æ©è½è¿½å
ã¨ãããã¨ã§æ®µã
é©å½ã«ãªã£ã¦ãã¾ããããã£ãã¨çµããããã
- ä½è : æ¾ç°æ,大竹æºä¹,ã¯ã¾ã¡ã2,å¤æåä»,横éå·§ä¹,島ç°æ ¶æ¨¹,å¢äºä¿ä¹,ããã¯,åç°è£ä»,ä¼è¤ç´ä¹,å¡ä¸å¿å¤«,大沢åå®,åæ ,æµæ¬éç,uupaa,ç¢éãã,ä¸å³¶è¡,ä¸å³¶æ,è§ç°ç´è¡,WEB+DB PRESSç·¨éé¨
- åºç社/ã¡ã¼ã«ã¼: æè¡è©è«ç¤¾
- çºå£²æ¥: 2010/08/24
- ã¡ãã£ã¢: 大åæ¬
- è³¼å ¥: 29人 ã¯ãªãã¯: 338å
- ãã®ååãå«ãããã° (39件) ãè¦ã
åå
WEB+DB 詳解Ruby on Rails を流す その2 コメント機能追加 - 僕の車輪の再発明
WEB+DBの詳解Rails3を流す その1 - 僕の車輪の再発明
ä»åã¯ã³ã¼ãã®å¤æ´ã®ã¿ã
ã¡ãªã¿ã«ã³ã¼ãã¯âã«ããã¾ãã
kazuph/sample_rails_blog_app · GitHub
æ¤ç´¢ã®Viewã®è¿½å
app/views/posts/index.html.erb <%= form_for @search_form, :url => posts_path, :html => {:method => :get} do |f| %> <%= f.search_field :q %> <%= f.submit 'æ¤ç´¢' %> <% end %> <h1>Listing posts</h1> <table> <tr> <th>Title</th> <th>Body</th> <th></th> <th></th> <th></th> </tr> <% @posts.each do |post| %> <tr> <td><%= post.title %></td> <td><%= post.body %></td> <td><%= link_to 'Show', post %></td> <td><%= link_to 'Edit', edit_post_path(post) %></td> <td><%= link_to 'Destroy', post, method: :delete, data: { confirm: 'Are you sure?' } %></td> </tr> <% end %> </table> <br /> <%= link_to 'New Post', new_post_path %>
ã¢ãã«è¿½å
app/models/search_form.rb #!/usr/bin/env ruby # coding : utf-8 class SearchForm extend ActiveModel::Naming include ActiveModel::Conversion attr_accessor :q def initialize(params) self.q = params[:q] if params end def persisted? false end end
ã³ã³ããã¼ã©ã¼
app/controllers/posts_controller.rb class PostsController < ApplicationController # GET /posts # GET /posts.json def index @search_form = SearchForm.new params[:search_form] @posts = Post.scoped if @search_form.q.present? @posts = @posts.title_or_body_matches @search_form.q end respond_to do |format| format.html # index.html.erb format.json { render json: @posts } end end ... end
æ稿ã®ã¢ãã«ã«è¿½è¨
app/models/post.rb class Post < ActiveRecord::Base attr_accessible :body, :title has_many :comments validates :title, :presence => true, :length => {:maximum => 20} scope :title_or_body_matches, lambda{|q| where 'title like :q or body like :q', :q => "%#{q}%" } end
ã¯ãã§ãã¾ããï½ï¼é©å½ï¼