`
jiajie0531
  • 浏览: 28817 次
  • 性别: Icon_minigender_1
  • 来自: 上海
社区版块
存档分类
最新评论

rails4 6.4 Generating a Controller

阅读更多

创建一个控制器

刚在在学习模型的相关内容,你可以调整一下注意力来创建一个相对应的控制器。我们将再一次用以前用过的相同的命令:

$ 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

  • 大小: 14.8 KB
分享到:
评论

相关推荐

    Rails 4 in Action, Second Edition.pdf

    - **MVC架构**:Rails采用了经典的Model-View-Controller架构,其中模型负责处理业务逻辑,视图负责显示界面,控制器则作为二者之间的桥梁。 - **Active Record**:这是Rails提供的ORM(对象关系映射)解决方案,...

    The Rails 4 Way

    ### 关于《The Rails 4 Way》的知识点总结 #### 标题:The Rails 4 Way 这本书主要讲述了Ruby on Rails 4版本的核心特性和最佳实践。Ruby on Rails(简称Rails)是一个用Ruby语言编写的开源全栈Web应用框架。本书...

    Agile Web Development with Rails 4th edition(敏捷Web开发与Rails:程序指南 第四版)

    在第四版中,作者们详细介绍了Rails 4的新特性和改进,包括ActiveRecord的更新、ActionController的变化、Turbolinks的引入以及资产管道的优化等。 1. **ActiveRecord**: ActiveRecord是Rails的核心组件之一,负责...

    Agile Web Development with Rails 4

    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 ...

    Ruby on Rails 4 Tutorial 中文版

    Rails 4的改进包括ActionController的改进,如Strong Parameters,用于更安全地处理参数;Asset Pipeline的升级,使得前端资源管理更加高效;以及ActiveRecord的查询接口增强,使得数据库操作更为简洁。 在阅读...

    Rails3常用命令行命令

    rails g controller products index ``` 这会生成一个`products_controller.rb`文件以及对应的视图文件`index.html.erb`。 Rails3还支持安装插件,可以通过以下命令将插件添加到项目中: ```bash rails plugin ...

    Beginning Rails 4

    ### Beginning Rails 4 #### 关于本书 《Beginning Rails 4》是一本旨在为初学者提供全面、系统性介绍 Ruby on Rails 框架的书籍。本书覆盖了从安装环境到构建完整 Web 应用程序的全过程,并深入探讨了 Rails 的...

    Rails 4 in Action

    唔,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...

    Crafting Rails 4 Applications

    ### 关于《Crafting Rails 4 Applications》的关键知识点解析 #### 标题解析:Crafting Rails 4 Applications - **Rails 4**:Rails 4是Ruby on Rails框架的一个版本,该版本在2013年发布。Ruby on Rails(简称...

    Rails项目源代码

    Ruby on Rails,通常简称为Rails,是一个基于Ruby编程语言的开源Web应用框架,遵循MVC(Model-View-Controller)架构模式。这个“Rails项目源代码”是一个使用Rails构建的图片分享网站的完整源代码,它揭示了如何...

    Rails 4 Application Development.pdf

    Rails 4是Ruby on Rails(简称Rails)的一个重要版本,它是一种用于Web应用程序开发的模型-视图-控制器(Model-View-Controller,MVC)架构框架。Rails 4引入了许多新特性与改进,旨在提高开发效率和应用程序的安全...

    Rails 4 Test Prescriptions(Pragmatic,2014)

    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 3 in Action

    3. **MVC架构**:Rails采用Model-View-Controller架构,书中详细解释了模型(Model)、视图(View)和控制器(Controller)之间的交互与职责。 4. **路由**:Rails的路由系统允许灵活地定义URL到控制器动作的映射,...

    Rails 4 Days - Chinese

    《Rails 4 Days - Chinese》是一本针对中文用户编写的关于Ruby on Rails框架的快速学习教程,旨在帮助读者在短短四天内掌握Rails的核心概念和技术。Rails是Ruby语言的一个流行Web开发框架,以其“DRY”(Don't ...

Global site tag (gtag.js) - Google Analytics