上一节里,我们搭建了一个数据库的结构,并用index验证了request-response cycle,如下图:
这一节,我们要继续丰富我们的controller:
While index gave all categories, show allows us to access one category. This this is helpful when we want to show just one Category at a time in our Etsy app.
The show method works like this. we write:
The show method finds a single category of a given id and stores it in @category. Look a little closer at params[:id]. Seem familiar?
It's a hash that we use to find an object by its unique id. We can find the exact object we want.
我们知道,【在main Controller里的每个action都需要一个route & view】
这里我们在controller里加入了show method,Rails自动update了the route and the view for us.
提醒:在route.rb文件中,我们已有的语句:resources :categories已经takes care of our route for show. Rails 还创建了show.html.erb在我们的Views中!
Rails did this for show, just like it did for the index method.
Rails generates these when we enter methods in your rails generate controller command[rails generate controller Categories index show new edit delete], other we need do it manually.
此时我们的routes.rb还是老样子:
Rails.application.routes.draw do get '/' => 'pages#home' resources :categories get 'categories/:id/delete' => 'categories#delete', :as => :categories_delete end**show.html.erb:
<%= render 'shared/topnav' %> <p> <strong>Name:</strong> <%= @category.name %> </p> <p> <strong>Thumburl:</strong> <%= image_tag @category.thumburl %> </p> <%= link_to 'Edit', edit_category_path(@category) %> | <%= link_to 'Back', categories_path %> <%= render 'shared/footer' %>**request in browser: http://localhost:8000/categories/1
<%= form_for (@category) do |f| %> <!--for the person at hand, we'll pass the following information to our form.--> <div class="field"> <%= f.label :name %><br> <!--Heading for name, labels in Rails, allow us to label our field -!--冒号后面不能有空格---> <%= f.text_field :name %> <!--The field where we put our stuff, text_field is for text, --> </div> <div class="field"> <%= f.label :thumburl %><br> <%= f.url_field :thumburl %> <!--rails we can use url_field for things like urls--> </div> <div class='actions'> <%= f.submit %> <!--the button to submit the form.--> </div> <% end %>All this information gets saved in a file called _form.html.erb[_form.html.erb]!
===================================
好了,我们要接着写 增加 的Controller methods 了。
def new @category = Category.new #creating a whole new instance of Category, and storing it in @category end但是建立了一个instance of Category还不够,它并没有保存,需要建立'create' method.
The create method looks like this:
def create @category = Category.new(category_params) #save that information as our new data through our strong params method if @category.save redirect_to(:action => 'index') #If it saves, we redirect to the index page else render('new') #otherwise the new page will render again. end end此时的 *** app/controllers/categories_controller.rb 文件就成了:
class CategoriesController < ApplicationController def index @categories = Category.all end def show @category = Category.find(params[:id]) end def new @category = Category.new end def create @category = Category.new(category_params) # I do not udrstand the .new and the () if @category.save redirect_to(:action => 'index') else render('new') end end def edit end def delete end private def category_params params.require(:category).permit(:name, :thumburl) end end在浏览器中请求:localhost:8000/categories/new, 我们得到:
<%= render 'form' %>
so,
new.html.erb 文件里:
<%= render 'shared/topnav' %> <h1>New Category</h1> <!-- Render form here --> <%= render 'form' %> <%= link_to 'Back', categories_path %> <%= render 'shared/footer' %>
===================================
好了,我们要接着写 改 的Controller methods 了。
The edit method looks like this:
def edit
@category = Category.find(params[:id])
end
edit method 就是根据id找到category并把它存入@category中。跟show method是一模一样的。这还不够,我们还要一个update method来存它。
def update
@category = Category.find(params[:id]) #find a category by its id
if @category.update_attributes(category_params)
redirect_to(:action => 'show', :id => @category.id)
else
render('index')
end
end
所以此时我们的**app/controllers/categories_controller.rb文件就成了:
class CategoriesController < ApplicationController def index @categories = Category.all end def show @category = Category.find(params[:id]) end def new @category = Category.new end def create @category = Category.new(category_params) if @category.save redirect_to(:action => 'index') else render('new') end end def edit @category = Category.find(params[:id]) #finds a single category of a given id and stores it in @category end def update @category = Category.find(params[:id]) #finds a person by its id and stores it in person if @category.update_attributes(category_params)#check if the attributes are updated in our model redirect_to(:action => 'show', :id => @category.id)#go to show page for that object else render('index') #render our index again end end def delete end private def category_params params.require(:category).permit(:name, :thumburl) end endIn your browser, visit localhost:8000/categories/1/edit to see what you created.
接下来是修改view: app/views/categories/edit.html.erb
We just need an edit page and the update page will process the information.
Here, our form_for comes in handy again. We go to our edit page and write: <%= render 'form' %>
That's it. We're good!
<%= render 'shared/topnav' %> <h1>Edit Category</h1> <!-- Render form here --> <%= render 'form' %> <%= link_to 'Back', categories_path %> <%= render 'shared/footer' %>浏览器请求:http://localhost:8000/categories/1/edit
===================================
好了,我们要接着写 删 的Controller methods 了。
we could write a delete method like this:
def delete
@category = Category.find(params[:id])
end
它看起来有点像show & edit,The destroy method looks like this:
def destroy
Category.find(params[:id]).destroy
redirect_to(:action => 'index')
end
Here we find a category we want to permanently delete. Once we delete it, we get redirected to the index page.
修改view: app/views/categories/delete.html.erb 成:
<p> <strong>Name:</strong> <%= @category.name %> </p> <p> <strong>Thumburl:</strong> <%= image_tag @category.thumburl %> </p> <%= link_to "Delete", categories_delete_path(:id => @category.id) %> | <%= link_to 'Edit', edit_category_path(@category) %> | <%= link_to 'Back', categories_path %> <p>1</p> <p>2</p> <p>3</p>
Congratulations! You created the Categories page of the Etsy app. We did this by creating the Model, Controller, and Views for Categories.
总结一下:
We first generated the Category Model using a terminal command. We added columns to our Categories Migration table and migrated and seeded our database.
We then generated a Categories Controller with the index, show, new, edit, and delete methods. This gave us a route and View for those methods.
We created Controller methods for eight of our CRUD actions, and Views for index, show, new, edit, and delete.