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:
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
<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>
<p>Find me in app/views/friends/list.rhtml</p>
Name:<%=@friend.name %><br/>
Age:<%=@friend.age %>
<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>
<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