form_for - Ruby on Rails

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/22941664/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-02 23:31:29  来源:igfitidea点击:

form_for - Ruby on Rails

ruby-on-railsrubyruby-on-rails-4

提问by Ismoh

I do not understand the form_for.

我不明白form_for

I try to implement this tutorialand I do not understand the view-code.
Moreover I dont understand the api, otherwise I wouldnt asked here..

<%= form_for @user, :as => :user, :url => sign_in_path(@user) do |f| %>

我尝试实现本教程,但我不理解视图代码。
而且我不明白api,否则我不会在这里问..

<%= form_for @user, :as => :user, :url => sign_in_path(@user) do |f| %>

  1. What does the :as => :usersay ?
  2. :url => sign_in_pathis clear, but why is there a (@user)behind it?
  3. And how can I get access to @userin a different View?
    3.1. I also want the log-in-form in the application.html.erb(the layout), BUT the @useris in the user_controller.rband not in the application_controller.rb.
    How can I do this?
  4. <%= form_for (User.new), ...works well, but I think it isn't right..
  5. Why is there something like a for/forEach-loop? do |f| %>
  1. 什么是:as => :user说什么?
  2. :url => sign_in_path很清楚,但为什么(@user)背后有一个?
  3. 我怎样才能获得@user不同的访问权限View
    3.1. 我也想的登录表单中application.html.erb(布局),但@useruser_controller.rb与不在application_controller.rb
    我怎样才能做到这一点?
  4. <%= form_for (User.new), ...效果很好,但我认为这是不对的..
  5. 为什么会有类似 for/forEach 循环的东西? do |f| %>

Thanks for your help!

谢谢你的帮助!

回答by MrYoshiji

A little explanation (form_fordocumentation here):

一点解释(这里的文档):form_for

<%= form_for @user, :as => :user, :url => sign_in_path(@user) do |f| %>

Point 1.:as => :user

第 1 点。:as => :user

This is the name used to generate the input's name (and the params' names), example:

这是用于生成输入名称(和参数名称)的名称,例如:

= form_for Admin.new, as: :user do |f|
                          #^^^^
  = f.input :username

# will generate an input like this:
<input type='text' name='user[username]' #... />
                        #^^^^


Point 2.:url => sign_in_path(@user)

第 2 点。:url => sign_in_path(@user)

In the tutorial, @useris set like this:

在教程中,@user是这样设置的:

  def sign_in
    @user = User.new
  end


Point 3.@useravailable in other actions

Point 3.@user可在其他动作中使用

You have to set this variable in each action you want it. It can be redundant, so you can use a before_filter in order to authenticate set the @uservariable at each action your want:

您必须在您想要的每个操作中设置此变量。它可能是多余的,因此您可以使用 before_filter 来验证@user在您想要的每个操作中设置变量:

class UsersController < ApplicationController
  before_filter :set_user_variable

  def set_user_variable
    @user ||= User.find(session[:user_id]) if session[:user_id].present?
  end
end

If you want to make it available everywhere in your app (implies that you must be connected to a user account to browse the app):

如果你想让它在你的应用程序的任何地方都可用(意味着你必须连接到用户帐户才能浏览应用程序):

class ApplicationController < ActionController::Base
  before_filter :set_user_variable, except: [:sign_in, :login]

  def set_user_variable
    @user ||= User.find(session[:user_id]) if session[:user_id].present?
  end


Point 4.form_for (User.new)

第 4 点。form_for (User.new)

We set the variable @userin the controller and pass it as an argument to form_forbecause it is a Rails Convention to never call a Model's name directly in the views, and it is deprecated to provoke SQL queries in the view.

我们@user在控制器中设置变量并将其作为参数传递给 ,form_for因为它是 Rails 约定,从不直接在视图中调用模型的名称,并且不推荐在视图中引发 SQL 查询。

Example:

例子:

######## WRONG
# view
<%= Post.find(params[:id]).title %>

######## MUCH BETTER
# controller's action:
def show
  @post = Post.find(params[:id])

# view
<%= @post.title %>

Instance Variablesset in the Action of a Controller are shared between the actions, its view and its partial views.

在控制器的动作中设置的实例变量动作、它的视图和它的部分视图之间共享。



Point 5.do/end block in form_for

要点 5.在 form_for 中执行/结束块

Please give your input at this point, not sure how to explain it

请在此时提供您的意见,不知道如何解释

This part of the code is called a do/end block, it represents a piece of code that will be executed in the context of the form_for. We use the form_for's instance as the variable defined in the pipes, here it is |f|. I usually don't use |f|, it is not really relevant to me. I prefer to use this kind of variable name:

这部分代码称为 do/end 块,它表示将在form_for. 我们使用form_for的实例作为管道中定义的变量,这里是|f|。我通常不使用|f|,它与我无关。我更喜欢使用这种变量名:

= form_for @user do |user_form_builder|
  = user_form_builder.input :username

Which I think is more readable and easier to understand.

我认为它更具可读性和更容易理解。