与 start_form_tag 等效的 Rails3 是什么? [英] What is the Rails3 equivalent of start_form_tag?

查看:41
本文介绍了与 start_form_tag 等效的 Rails3 是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这里的新手......我正在学习,很明显,一个旧的 Rails 教程.

要创建一个表单,他们有以下代码:

<%= start_form_tag(:action => 'create')%><%= text_field(:album,:title)%><br/><%= text_field(:album,:artist)%><br/><%= text_field(:album,:genre)%><br/><%= datetime_select(:album, :release_date)%><br/><%= submit_tag("创建") %><%= end_form_tag %>

这段代码的正确 Rails3 语法是什么?

谢谢.

解决方案

自从 Rails 1 以来发生了很多变化,我对愿意花时间坐下来学习 Rails 3 并学习 Rails 3 的人印象深刻.差异(1).社区需要欢迎这些,而 RTFM 的人没有提供帮助.这是我认为我可以提供帮助的地方.

我会在 Rails 3 中这样写:

<%= form_for(@album) do |f|%><p><%= f.label :title %><%= f.text_field :title %></p><p><%= f.label :artist %><%= f.text_field :artist %></p><p><%= f.label :genre %><%= f.text_field :genre %></p><p><%= f.label :release_date %><%= f.datetime_select :release_date %></p><%= f.submit %><%结束%>

哇,这么多变化!我们从哪里开始?当然是最重要的!

form_for 是一种在 Rails 中使用了一段时间的方法,并且在过去的几次重大修订中变得非常有用.此方法将获取在控制器中设置的 @album 变量并检查它以确定几件事 1) 表单应该放在何处 2) 字段的值应该是什么.

在这个阶段我应该提到应该提到你的app/views/albums/new.html.erb现在应该是这样的:

新专辑

<%= 渲染表单"%>

天哪,更多新东西!没关系:render "form" 将渲染 app/views/albums/_form.html.erb 部分.这就是上面的 form_for tomfoolery 现在应该存在的地方.为什么?让我继续解释.

在您的 AlbumsControllernew 操作中,您将拥有如下内容:

def new@album = Album.new结尾

form_for 足够聪明:嘿,这是一个新的 Album 资源,他可能想要去 create 操作之所以采用这种形式,是因为他是个好小伙子,并且遵循 Rails 惯例",所以这正是它要做的.

除外:

这将引发一个异常,即没有定义 albums_path 方法.什么?

在 Ruby on Rails 2 中,RESTful 路由成为了大事.最好阅读路由指南,因为如果我解释它,我只会重复很多其中.基本上:RESTful 路由是应用程序 URL 路由的约定,它决定了当我们对 /albums 执行 POST(它是一个表单,还记得吗?)时,这将转到 create 动作.

但是 Rails 怎么知道这样做呢?

因为在您的 config/routes.rb 文件中,您已将这一行:

资源:专辑

这定义了控制器的七个"默认操作(索引、显示、新建、创建、编辑、更新和销毁)的路由.说真的,请阅读路线指南,了解关于这种美的一切.

回到我们的小form_for.它知道去 /albums 寻找一个新对象,这很酷.那么 f 块参数是怎么回事?嗯,这是一个表单构建器对象,它允许我们从我们的 new(很快,edit)为特定的 Album 对象构建表单元素行动.当我们打电话时:

<%= f.text_field :title %>

我们正在做的 Rails 1 相当于:

<%= text_field :album, :title %>

Rails 给你的只是一点点语法糖,让你的视图更加 DRY.

我还为您的表单添加了标签,因为用户需要知道他们正在填写哪些字段.这里的美妙之处在于用户不会看到 :title,但会看到请参阅 Title 代替.对于 :release_date,他们会看到发布日期".漂亮.

在表单的末尾,我使用 f.submit.这将生成一个提交按钮,如果 @album 是一个新对象,则显示创建相册",或者如果它是预先存在的对象,则显示更新相册".但是,预先存在的对象在哪里得到编辑?

edit动作中!

def 编辑@album = Album.find(params[:id]结尾

在您的 app/views/albums/edit.html.erb 文件中,您的 new.html.erb 兄弟文件中的内容大致相同:

编辑相册

<%= 渲染表单"%>

您可以在这两个中精确地使用 same 行来渲染相同的部分,但是如果对象是新的或者它是预先存在的(通常称为持久化对象),它将执行不同的).

edit 动作渲染这个部分的情况下,它会为表单生成一个到 /albums/1 的路由,并做一个 Rails-specialty PUT 请求到这个资源.另一件事路由指南会很好地解释,我希望.

很抱歉这个答案太长,但这个问题没有真正的简短答案,这是一个很大的变化,但是当我说它真的更好时请相信我.>

Rails 3 岩石.

(1) 不喜欢的人就不那么重要了.

Newbie here...I am just going through, what is clearly, an old rails tutorial.

To create a form they have the following code:

<%= start_form_tag(:action => 'create')%>

        <%= text_field(:album, :title) %><br />
        <%= text_field(:album, :artist) %><br />
        <%= text_field(:album, :genre) %><br />
        <%= datetime_select(:album, :release_date) %><br />

        <%= submit_tag("Create") %>

    <%= end_form_tag %> 

What is the proper Rails3 syntax for this code?

Thanks.

解决方案

Quite a lot has changed since Rails 1 and I'm really impressed by the people who are willing to spend the time to sit down with Rails 3 and learn the differences (1). The community needs to be welcoming to these and the RTFM'ers aren't being helpful. This is where I think I can help out.

I would write that like this in Rails 3:

<%= form_for(@album) do |f| %>
  <p>
    <%= f.label :title %>
    <%= f.text_field :title %>
  </p>

  <p>
    <%= f.label :artist %>
    <%= f.text_field :artist %>
  </p>

  <p>
    <%= f.label :genre %>
    <%= f.text_field :genre %>
  </p>

  <p>
    <%= f.label :release_date %>
    <%= f.datetime_select :release_date %>
  </p>

  <%= f.submit %>
<% end %>

Woah, so many changes! Where do we begin? The top line, of course!

form_for is a method that's been in Rails for a while and has become exceedingly useful over the past couple of major revisions. This method will take the @album variable set up in the controller and inspect it to determine a couple of things 1) where the form should go 2) what the values of the fields should be.

I should mention at this stage that it should be mentioned that your app/views/albums/new.html.erb should now look like this:

<h2>New Album</h2>
<%= render "form" %>

OMG, more new stuff! It's ok: render "form" will render the app/views/albums/_form.html.erb partial. This is where the above form_for tomfoolery should live now. Why? Let me continue explaining.

In your AlbumsController's new action, you'd have something like this:

def new
  @album = Album.new
end

form_for is smart enough to go: "hey, that's a new Album resource, he probably wants to go to the create action for this form because he's being a good lad and following the Rails conventions", so that's precisely what it'll do.

Except:

This will raise an exception that there's no albums_path method defined. What the?

In Ruby on Rails 2, RESTful routing became a Big Thing. It's best if you read the routing guide, since if I explained it I would only be repeating a lot of it. Basically: RESTful routing is a convention for the URL routes of your application that determines that when we do a POST (it's a form, remember?) to /albums, this will go to the create action.

But how does Rails know to do this?

Because in your config/routes.rb file you've put this line:

resources :albums

This defines routes to "The Seven" default actions of your controller (index, show, new, create, edit, update and destroy). Seriously, read the routing guide to know all about this beauty.

So back to our little form_for. It knows to go to /albums for a new object, which is cool and all. So what's up with the f block argument? Well, this is a form builder object which allows us to build form elements for the specific Album object from our new (and soon, edit) actions. When we call:

<%= f.text_field :title %>

We are doing the Rails 1 equivalent of:

<%= text_field :album, :title %>

It's just a little bit of syntatic sugar that Rails gives you in order to make your views more DRY.

I also added labels to your form because users need to know what the fields are that they're filling in. The beautiful part here is that the user won't see :title, but will see Title instead. For :release_date, they'll see "Release Date". Beautiful.

At the end of the form, I use f.submit. This will generate a submit button that says either "Create Album" if @album is a new object, or "Update Album" if it's a pre-existing object. But where do the pre-existing objects get edited?

In the edit action!

def edit
  @album = Album.find(params[:id]
end

In your app/views/albums/edit.html.erb file you'd have much the same you have in its new.html.erb sibling:

<h2>Editing Album</h2>
<%= render "form" %>

You can use precisely the same line in both of these to render the same partial, but it will perform differently if the object is new or if it is pre-existing (commonly referred to as persisted).

In the case of the edit action rendering this partial, it will generate a route to /albums/1 for the form and do a Rails-specialty PUT request to this resource. Another thing the routing guide will explain very well, I hope.

I'm sorry for the length of this answer but there's no real short answer for this question, it's quite a large change but trust me when I say it really is for the better.

Rails 3 rocks.

(1) Less so with the people who aren't.

这篇关于与 start_form_tag 等效的 Rails3 是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆