创建一个控制器
刚在在学习模型的相关内容,你可以调整一下注意力来创建一个相对应的控制器。我们将再一次用以前用过的相同的命令:
$ rails generate controller Comments |
这个命令会创建六个文件和一个空文件夹:
File/Directory |
Purpose |
app/controllers/comments_controller.rb |
Comments 控制器 |
app/views/comments/ |
控制器的视图被存放在这里 |
test/controllers/comments_controller_test.rb |
关于控制器的测试文件 |
app/helpers/comments_helper.rb |
一个视图的helper文件 |
test/helpers/comments_helper_test.rb |
对于helper的测试文件 |
app/assets/javascripts/comment.js.coffee |
关于控制器的CoffeeScript文件 |
app/assets/stylesheets/comment.css.scss |
关于控制器的CSS文件 |
如同其他的blog一样,我们的读者会在查看article之后直接创建他们的comments,一旦他们已经新增了他们的comment,将会返回到article显示界面,可以看到属于它的那些comment。为了做到这点,我们的CommentsController提供了一个方法来创建comments和删除多余的comments。
因此首先,我们来打开Article的显示模板 app/views/articles/show.html.erb,让我们来创建一个新的comment:
<p> <strong>Title:</strong> <%= @article.title %> </p>
<p> <strong>Text:</strong> <%= @article.text %> </p>
<h2>Add a comment:</h2> <%= form_for([@article, @article.comments.build]) do |f| %> <p> <%= f.label :commenter %><br> <%= f.text_field :commenter %> </p> <p> <%= f.label :body %><br> <%= f.text_area :body %> </p> <p> <%= f.submit %> </p> <% end %>
<%= link_to 'Back', articles_path %> | <%= link_to 'Edit', edit_article_path(@article) %> |
在Article显示界面上新增了一个form,通过调用CommentsController的create action来创建了一个新的comment。这里form_for的调用使用了一个数组循环,在其中内建了路由,类似于/articles/1/comments。
让我们来打开文件 app/controllers/comments_controller.rb,新建方法 create:
classCommentsController < ApplicationController defcreate @article= Article.find(params[:article_id]) @comment= @article.comments.create(comment_params) redirect_to article_path(@article) end
private defcomment_params params.require(:comment).permit(:commenter, :body) end end |
在这里你会看到稍微有点复杂,超过你以前所编辑过的articles控制器。你所建立的内嵌的会有个副作用。对于comment的每一个request必须能够跟踪到每一个新增comment所属的article,,因此对于Article模型的find方法的初始化调用,来获取所需查询中的article。
另外,代码利用了一些涉及到关联性调用的方法。我们使用@article.comments的create方法来创建和保存comment。这会自动关联到comment,使其属于特定的article。
一旦我们已经创建了一个新的comment, 我们就跳转到原来的article,使用的是article_path(@article)这个helper。正如 我们已经看到的,调用了ArticlesController的show action,然后跳转到show.html.erb模板。这就是我们想要显示comment的地方,让我们来增加这个视图 app/views/articles/show.html.erb.
<p> <strong>Title:</strong> <%= @article.title %> </p>
<p> <strong>Text:</strong> <%= @article.text %> </p>
<h2>Comments</h2> <% @article.comments.each do |comment| %> <p> <strong>Commenter:</strong> <%= comment.commenter %> </p>
<p> <strong>Comment:</strong> <%= comment.body %> </p> <% end %>
<h2>Add a comment:</h2> <%= form_for([@article, @article.comments.build]) do |f| %> <p> <%= f.label :commenter %><br> <%= f.text_field :commenter %> </p> <p> <%= f.label :body %><br> <%= f.text_area :body %> </p> <p> <%= f.submit %> </p> <% end %>
<%= link_to 'Edit Article', edit_article_path(@article) %> | <%= link_to 'Back to Articles', articles_path %> |
现在你可以在你的blog中增加多个article和comment,把它们显示在正确的地方。
original text: http://guides.rubyonrails.org/getting_started.html#generating-a-controller
—end
相关推荐
Now in version 4, Rails is mature and powerful, and to use it effectively you need more than a few Google searches. You'll find no substitute for the guru's-eye-view of design, testing, deployment, ...
We still start with a step-by-step walkthrough of building a real application, and in-depth chapters look at the built-in Rails features. This edition now gives new Ruby and Rails users more ...
rails常用命令,例如新建rails项目,新建controller、model 等等
唔,1分应该还是有人下的吧,共同学习进步,Ruby on Rails is an open source web framework.... "Rails 4 in Action" is a fully-revised second edition of "Rails 3 in Action." This hands-on, compreh...
Beginning Rails 4
Rails 4 Test Prescriptions.pdf
Rails test described by RSpec
Rails 4 Test Prescriptions is a comprehensive guide to how tests can help you design and write better Rails applications. In this completely revised edition, you'll learn why testing works and how to...
Rails 4 in Action, Second Edition.pdf
[Pragmatic Bookshelf] Crafting Rails 4 Applications Expert Practices for Everyday Rails Development (E-Book) ☆ 图书概要:☆ Get ready to see Rails as you've never seen it before. Learn how to ...
RailsSpace teaches you Ruby on Rails by developing a real-world application: RailsSpace, a social networking website aimed at the Rails community itself. We take you step by step, from the virtually...
一本Rails 4开发进阶教程,适合有一定开发经验的Ruby on Rails开发人员阅读
学习ruby!
Bootstrap 3 和 Rails 4(样例用的是Ruby 2.1.1,Rails 4.1.4) Table of Contents Preface 1 Chapter 1: Introducing Web Application Development in Rails 7 Why Bootstrap with Rails? 8 Setting up a Todo ...
Ruby on Rails4 api.chm ruby on rails 4 最新api chm版本,方便大家阅览!
Crafting Rails 4 Applications
要弄明白Rails 4 中Strong Parameters机制,首先我们要看看Rails3中的Parameters 在 Rails3 中创建或更新 Active Record 对象时,会有 Mass Assignment 安全问题。所以 Model 中需要列一个白名单,声明哪些属性可以...
Ruby on Rails框架是Ruby的一个杀手级应用框架,可以敏捷开发Web应用,是目前为止开发效率最高的Web开发技术