form_for - Ruby on Rails [英] form_for - Ruby on Rails

查看:31
本文介绍了form_for - Ruby on Rails的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我不明白form_for.

I do not understand the form_for.

我尝试实施此教程 我不明白视图代码.
而且我不了解api,否则我不会在这里问..

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

I try to implement this tutorial and 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| %>

  1. :as => 是什么意思?:user 说?
  2. :url =>sign_in_path 很清楚,但为什么后面有一个(@user)?
  3. 我怎样才能在不同的View中访问@user?
    3.1.我还想要 application.html.erb(布局)中的登录表单,但是 @useruser_controller.rb 而不是在 application_controller.rb 中.
    我该怎么做?
  4. <%= form_for (User.new), ... 效果很好,但我认为它不对..
  5. 为什么会有类似 for/forEach 循环的东西?<代码>做 |f|%>
  1. What does the :as => :user say ?
  2. :url => sign_in_path is clear, but why is there a (@user) behind it?
  3. And how can I get access to @user in a different View?
    3.1. I also want the log-in-form in the application.html.erb (the layout), BUT the @user is in the user_controller.rb and 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| %>

感谢您的帮助!

推荐答案

一点说明 (form_for 文档在这里):

A little explanation ( form_for documentation here):

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

第 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]' #... />
                        #^^^^

<小时>

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

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

In the tutorial, @user is set like this:

  def sign_in
    @user = User.new
  end

<小时>

第 3 点. @user 可用于其他操作

您必须在您想要的每个操作中设置此变量.它可能是多余的,因此您可以使用 before_filter 来验证设置 @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 @user variable at each action your want:

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

<小时>

第 4 点. form_for (User.new)

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

We set the variable @user in the controller and pass it as an argument to form_for because 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.

示例:

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

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

# view
<%= @post.title %>

在控制器的操作中设置的实例变量在操作、其视图及其部分视图之间共享.

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

Point 5. do/end block in form_for

此时请给出您的意见,不知道如何解释

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

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:

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

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

Which I think is more readable and easier to understand.

这篇关于form_for - Ruby on Rails的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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