• start RubyOnRails


    Make the environment of RubyOnRails on windows

    * download ruby on ruby-lang.org
    * setup rails:
    cmd-->gem install rails --remote
    * setup mysql (Recommand 4.1)
    * setup mysql control center (Create DataBase easily)
    * create a web site
    cmd -->ruby websitename
    * start the web server(WEBrick) using (on /../websitename/ directionary): ruby scrip\server,and visit http://127.0.0.1:3000

    Create a simple application
    * Create a database named MyFriends,and create a table named Friends with colums:id,name,age
    * Config the webapplication database config file :/websitename/config/database.yml
    development:
      adapter: mysql
      database: myfriends
      username: root
      password: xxxxxx
      host: localhost

    * Restart the webserver using "ruby script\server".
    * Note:after erery character ":" have a "space" character.
    * Using command "ruby script\generate model Friends" to geneate the model named friends.rb on "/websitename/app/model/".
    * Using command "ruby script\generate controller Friends list view new edit " to generate the controller named friends_controller.rb on "/websitename/app/controllers" and other view template pages on "/websitename/app/views/friends/" such as "list.rhtml"、"edit.rhtml" and so on.

    edit friends_controller.rb file:

    class FriendsController < ApplicationController

      
    def list
          @friends_all
    =Friends.find_all
      end

      
    def view
          @friend
    =Friends.find(params[:id])
      end

      
    def new
      end

      
    def edit
          @friend
    =Friends.find(params[:id])
      end
      
      
    def create
          @friend
    =Friends.new
          @friend.name
    =params[:friend_name]
          @friend.age
    =params[:friend_age]
          
    if @friend.save
              redirect_to 
    '/friends/list'
          
    else
              redirect_to 
    '/friends/new'
          end
      end
      
      
    def update
          @friend
    =Friends.find(params[:id])
          
    if @friend.update_attribute("name",params[:friend_name]) and @friend.update_attribute("age",params[:friend_age])
              redirect_to 
    '/friends/list'
          
    else
              redirect_to 
    '/friends/list'
          end
      end
    end


    and other files in "/websitename/app/views/friends" edit like this:
    list.rhtml
    <html>
        
    <body>
            
    <h1>Friends List </h1>
            
    <p>
            
    <% @friends_all.each do |friend| %>
            Name:
    <%=link_to friend.name,:action=>"view",:id=>friend.id %>
            Age:
    <%=friend.age %>
            
    <%=link_to "Edit",:action=>"edit",:id=>friend.id %>
            
    <br/>
            
    <% end %>
            
    </p>
        
    </body>
    </html>
    view.rhtml
    <h1>Friends#list</h1>
    <p>Find me in app/views/friends/list.rhtml</p>
    Name:
    <%=@friend.name %><br/>
    Age:
    <%=@friend.age %>
    new.rhtml
    <h1>Friends#new</h1>
    <p>Find me in app/views/friends/new.rhtml</p>
    <form action="/friends/create" method="post">
    Name:
    <input id="friend_name" name="friend_name" value=""><br />
    Age:
    <input id="friend_age" name="friend_age" value=""></br />
    <input name="commit" type="submit" value="Create" />
    </form>
    edit.rhtml
    <h1>Friends#edit</h1>
    <p>Find me in app/views/friends/edit.rhtml</p>
    <form action="/friends/update" method="post">
    id:
    <input type="hidden" id="id" name="id" value="<%=@friend.id %>"></br>
    Name:
    <input name="friend_name" value="<%=@friend.name %>"><br />
    Age:
    <input name="friend_age" value="<%=@friend.age%>"><br/>
    <input type="submit" value="update" %>
    </form>

    over.
    now you already lenarned how to operate the database using RubyOnRails,next ,just read more books.
    some links:
    http://www.ruby-lang.org 
    http://www.ruby-doc.org 
    http://www.rubyonrails.org
    http://www.rubycentral.com/book/index.html 
    http://rubyforge.org/ 
    http://www.webrick.org 
    http://www.yaml.org 
    http://wiki.rubyonrails.org/rails/pages/Tutorial
    http://www.bizwiki.cn/teamblog/?p=24
  • 相关阅读:
    Docker 系列(四):Docker 容器数据卷简单使用
    【QML 动态对象】使用JS中的语句动态创建和销毁组件
    【QML 动态对象】Loader动态加载组件
    vue-cli2.0全局使用sass变量
    两边+居中 布局
    跳转子路由后左侧菜单跳转为空白页,路由地址出错
    el-tree可搜索单选
    el-tree固定高度加滚动条
    前端 权限控制 方式
    综合分析类
  • 原文地址:https://www.cnblogs.com/caca/p/428674.html
Copyright © 2020-2023  润新知