close

step 1. 使用rails script建立project
os:~ $ rails new demo #建立新project,名稱為demo
os:~ $ cd demo
os:~/demo $ rails g controller Say hello #建立controller和view

step 2. app/views/say/hello.html.erb
os:~/demo $ cat app/views/say/hello.html.erb
<% if (params[:user] == nil) %>
  <!-- html form tag -->
  <form action="/say/hello.html" method="get">
  Input your name: <input type="text" name="user"/> <input type="submit" value="submit"/>
  </form>
  <!-- rails form tag -->
  <%= form_tag('/say/hello.html', :method => 'get') do %>
  Input your name: <%= text_field_tag 'user' %> <%= submit_tag 'submit' %>
  <% end %>
<% else %>
  <!-- Hello user -->
  <p>Hello, <%= params[:user] %></p>
<% end %>

step 3. web server
os:~/demo $ rails s #啟動web server
啟動web server後,在瀏覽器開啟http://localhost:3000/say/hello或http://localhost:3000/say/hello?user=owner網頁

arrow
arrow
    全站熱搜

    wsunccake 發表在 痞客邦 留言(0) 人氣()