Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/sql-server-2008/3.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 使用designe创建嵌套表单_Ruby On Rails_Devise - Fatal编程技术网

Ruby on rails 使用designe创建嵌套表单

Ruby on rails 使用designe创建嵌套表单,ruby-on-rails,devise,Ruby On Rails,Devise,我正在尝试使用Desive构建一个嵌套表单,使Desive的特点是在新用户注册时发送电子邮件。我想要这样的东西: <%= form_for(resource, :as => resource_name, :url => registration_path(resource_name)) do |f| %> <%= devise_error_messages! %> <%= f.fields_for(:information) do |info|

我正在尝试使用Desive构建一个嵌套表单,使Desive的特点是在新用户注册时发送电子邮件。我想要这样的东西:

<%= form_for(resource, :as => resource_name, :url => registration_path(resource_name)) do |f| %>
  <%= devise_error_messages! %>

  <%= f.fields_for(:information) do |info| %>

    <div><%= info.text_field :name, :placeholder => 'Nome' %></div>

    <div><%= info.text_field :surname, :placeholder => 'Cognome' %></div>

  <% end %>


  <div><%= f.email_field :email, :autofocus => true, :placeholder => 'E-mail' %></div>

  <div><%= f.password_field :password, :placeholder => 'Password' %></div>

  <div><%= f.password_field :password_confirmation, :placeholder => 'Conferma password' %></div>

  <div><%= f.submit "Registrati", class: "btn btn-large btn-info" %></div>
<% end %>
information.rb中

class User < ActiveRecord::Base
  has_one :information, dependent: :destroy
  # Include default devise modules. Others available are:
  # :timeoutable and :omniauthable
  devise :database_authenticatable, :registerable, 
     :recoverable, :rememberable, :trackable,
     :validatable, :confirmable, :lockable

  accepts_nested_attributes_for :information, update_only: true
end
class Information < ActiveRecord::Base
  belongs_to :user
end
class UsersController < Devise::RegistrationsController

  def new
super
resource.build_information
  end

end

但是什么也没有发生,或者更好的是,
name
name
这两个字段没有出现,但我没有收到错误消息

我会将这条线从控制器移动到视图(字段\u用于获取2个参数)。如下所示

控制器

def new
   super
   resource.build_information #remove from here
end
看法


现在必须工作

换衣服
res…..

res

注意“@”,这将起作用。从视图和控制器中删除生成信息。

是的,它起作用了。只有一个问题,为什么我的解决方案不好?问题出在哪里?不是解释。让我为你提供一份思考的食物。“资源不是实例变量”。查看我的编辑。我知道
resource
是一个def resource@resource | |=User.new end方法,但它返回@resource实例…所以我认为调用
resource.build_information
应该有效。如果在视图中执行,控制器中的a=1将不会打印1,如果您在视图中打印,控制器中的@a=1将打印1。好的,我的意思是,你在控制器中所做的resource.build\u信息在视图中丢失了。很明显。我试图在我调用
resource
来设置
@resource
new
操作中解释我的想法,因为@resource是一个实例变量,所以我可以从视图中访问它,所以如果在视图中调用
resource
,我应该得到之前设置的相同实例变量(
@resource
)吗?
<!--Added here -->
<%= f.fields_for(:information,resource.build_information) do |info| %> 
<%= form_for(@resource, :as => res