- ruby script/generate model event name:string description:text
- rake db:migrate
- ruby script/generate controller events
- 編輯 /app/controllers/events_controller.rb 加入
def index
@events = Event.find(:all)
end
def show
@event = Event.find(params[:id])
end
def new
@event = Event.new
end
def create
@event = Event.new(params[:event])
@event.save
redirect_to :action => :index
end
def edit
@event = Event.find(params[:id])
end
def update
@event = Event.find(params[:id])
@event.update_attributes(params[:event])
redirect_to :action => :show, :id => @event
end
def destroy
@event = Event.find(params[:id])
@event.destroy
redirect_to :action => :index
end
- 新增 /app/views/events/index.html.erb,內容如下:
<ul>
<% @events.each do |event| %>
<li>
<%= link_to event.name, :controller => 'events', :action => 'show', :id => event %>
<%= link_to 'edit', :controller => 'events', :action => 'edit', :id => event %>
<%= link_to 'delete', :controller => 'events', :action => 'destroy', :id => event %>
</li>
<% end -%>
</ul>
<%= link_to 'new event', :controller => 'events', :action => 'new' %>
- 新增 /app/views/events/show.html.erb,內容如下:
<%=h @event.name %>
<%=h @event.description %>
<p><%= link_to 'back to index', :controller => 'events', :action => 'index' %></p>
- 新增 /app/views/events/new.html.erb,內容如下:
<% form_for @event, :url => { :controller => 'events', :action => 'create' } do |f| %>
<%= f.label :name, "Name" %>
<%= f.text_field :name %>
<%= f.label :description, "Description" %>
<%= f.text_area :description %>
<%= f.submit "Create" %>
<% end %>
- 新增 /app/views/events/edit.html.erb,內容如下:
<% form_for @event, :url => { :controller => 'events', :action => 'update', :id => @event } do |f| %>
<%= f.label :name, "Name" %>
<%= f.text_field :name %>
<%= f.label :description, "Description" %>
<%= f.text_area :description %>
<%= f.submit "Update" %>
<% end %>
- 連往 http://localhost:3000/events