Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/ruby-on-rails/59.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/ruby/24.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ruby on rails 窗体从变量赋值_Ruby On Rails_Ruby_Ruby On Rails 3 - Fatal编程技术网

Ruby on rails 窗体从变量赋值

Ruby on rails 窗体从变量赋值,ruby-on-rails,ruby,ruby-on-rails-3,Ruby On Rails,Ruby,Ruby On Rails 3,我试图让我的表单为文本字段赋值。我有一个定义当前用户的帮助文件。我希望文本_字段:name是当前的_user.name <%= form_for @message, :url => contact_path do |form| %> <%= form.text_field :name = current_user.name %> <%= form.text_field :email = current_user.email %> ... contact

我试图让我的表单为文本字段赋值。我有一个定义当前用户的帮助文件。我希望文本_字段:name是当前的_user.name

<%= form_for @message, :url => contact_path do |form| %>
<%= form.text_field :name = current_user.name %>
<%= form.text_field :email = current_user.email %>
...
contact|u path do| form|%>
...

我已经试过#{name},params,你如何分配这个值?

你应该在你的控制器中这样做

例如:(见第三行)

除非您的意思是希望它成为页面呈现时的默认值,否则:

<%= form.text_field :name, value: current_user.name %>

或者如果你喜欢的话

<%= form.text_field :name, placeholder: current_user.name %>

您应该在控制器中执行此操作

例如:(见第三行)

除非您的意思是希望它成为页面呈现时的默认值,否则:

<%= form.text_field :name, value: current_user.name %>

或者如果你喜欢的话

<%= form.text_field :name, placeholder: current_user.name %>

选项添加到
文本\u字段
帮助程序中,如下所示:

<%= form_for @message, :url => contact_path do |form| %>
  <%= form.text_field :name, value: current_user.name %>
  <%= form.text_field :email, value: current_user.email %>
  ...
contact|u path do| form|%>
...

选项添加到
文本\u字段
帮助程序中,如下所示:

<%= form_for @message, :url => contact_path do |form| %>
  <%= form.text_field :name, value: current_user.name %>
  <%= form.text_field :email, value: current_user.email %>
  ...
contact|u path do| form|%>
...

=f.name,value:current\u user.names由于该值将被固定(在本例中为名称),为什么要指定文本字段。只需将当前用户名显示为静态文本。这将是更好的方法。@prasad.surase当然,您如何做到这一点以使表单获得值?您根本不提交名称(静态数据)。在控制器操作中显式分配它们。例如,如果您有“new”和“create”操作,则在new.html.haml中将名称显示为静态文本,并在“create”操作中明确指定“@obj.name=current_user.name”。如果您使用f.name、value:current_user.name,并且该文本字段不应由用户操纵,则我的方法更好。如果希望对字段进行操作,但需要指定一些默认值,则使用“value:current_user.name”更好。=f.name,value:current_user.name因为该值将是固定的(在本例中为名称),为什么要指定文本字段。只需将当前用户名显示为静态文本。这将是更好的方法。@prasad.surase当然,您如何做到这一点以使表单获得值?您根本不提交名称(静态数据)。在控制器操作中显式分配它们。例如,如果您有“new”和“create”操作,则在new.html.haml中将名称显示为静态文本,并在“create”操作中明确指定“@obj.name=current_user.name”。如果您使用f.name、value:current_user.name,并且该文本字段不应由用户操纵,则我的方法更好。如果希望对字段进行操作,但需要指定一些默认值,则使用“value:current_user.name”更好。