Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/ruby-on-rails/53.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
Html 如何在RubyonRails中使用object.build或build_对象使用隐藏字段?_Html_Ruby On Rails_Ruby On Rails 3_Forms_Model - Fatal编程技术网

Html 如何在RubyonRails中使用object.build或build_对象使用隐藏字段?

Html 如何在RubyonRails中使用object.build或build_对象使用隐藏字段?,html,ruby-on-rails,ruby-on-rails-3,forms,model,Html,Ruby On Rails,Ruby On Rails 3,Forms,Model,我有一个嵌套的用户注册表。我想插入3种型号的数据用户,存储和首选项。这些关系是一个用户拥有多个:商店和拥有一个:首选项 我遇到的问题是隐藏字段不会显示在我的视图中。我是否正确使用了@user.restaurants.build和@user.build\u首选项 以下是我的模型: class User < ActiveRecord::Base attr_accessible :email, :full_name, :password, :password_confirmation h

我有一个嵌套的用户注册表。我想插入3种型号的数据<代码>用户,
存储
首选项
。这些关系是一个
用户
拥有多个:商店
拥有一个:首选项

我遇到的问题是隐藏字段不会显示在我的视图中。我是否正确使用了
@user.restaurants.build
@user.build\u首选项

以下是我的模型:

class User < ActiveRecord::Base
  attr_accessible :email, :full_name, :password, :password_confirmation
  has_secure_password

  validates_uniqueness_of :email
  validates_presence_of :full_name

  has_many :stores, :dependent => :destroy
  has_one :preference, :dependent => :destroy

  accepts_nested_attributes_for :stores
  accepts_nested_attributes_for :preference
end

class Store < ActiveRecord::Base
  attr_accessible  :name, :tagline, :address, :city, :postal, :state, :phone, :hours, :user_id

  belongs_to :user
end

class Preference < ActiveRecord::Base
  attr_accessible :background_fill, :background_position, :body_color, :body_font, :body_size, :heading_color, :heading_font, :heading_size, :layout, :link_color, :logo_color, :logo_size, :logo_font

  belongs_to :user
end
class用户:销毁
拥有一个:首选项,:依赖=>:销毁
接受:存储的\u嵌套\u属性\u
为:首选项接受\u嵌套的\u属性\u
结束
类存储
我的用户控制器:

class UsersController < ApplicationController

def new
  @user = User.new
  @user.stores.build
  @user.build_preference
end
class UsersController
我的看法是:

<%= form_for @user do |f| %>
  <% if @user.errors.any? %>
  <div class="error_messages">
    <h2>There was an error!</h2>
    <ul>
      <% @user.errors.full_message.each do |message| %>
        <li><%= message %></li>
      <% end %>
    </ul>
  </div>
  <% end %>
  <%= f.label :full_name %>
  <%= f.text_field :full_name, :class => "target", :placeholder => "Your full name", :maxlength => "55", :autofocus => "autofocus" %>
  <%= f.label :email %>
  <%= f.email_field :email, :class => "target", :placeholder => "example@gmail.com", :maxlength => "55" %>
  <%= f.label :password %>
  <%= f.password_field :password, :class => "target", :placeholder => "Enter a password", :maxlength => "55" %>
  <%= f.label :password_confirmation, "Confirmation" %>
  <%= f.password_field :password_confirmation, :class => "target", :placeholder => "Enter your password again", :maxlength => "55" %>
  <% f.fields_for :stores do |builder| %>
    <%= builder.hidden_field :name, value: params[:name] %>
    <%= builder.hidden_field :tagline, value: params[:tagline] %>
    <%= builder.hidden_field :address, value: params[:address] %>
    <%= builder.hidden_field :city, value: params[:city] %>
    <%= builder.hidden_field :state, value: params[:state] %>
    <%= builder.hidden_field :postal, value: params[:postal] %>
    <%= builder.hidden_field :phone, value: params[:phone] %>
    <%= builder.hidden_field :hours, value: params[:hours] %>
  <% end %>
  <% f.fields_for :preference do |builder| %>
    <%= builder.hidden_field :layout, value: params[:layout] %>
    <%= builder.hidden_field :background_fill, value: params[:background_fill] %>
    <%= builder.hidden_field :background_position, value: params[:background_position] %>
  <% end %>
  <%= f.submit "Create an Account for Free", :class => "button cta" %>
<% end %>

有一个错误!
“目标”、:占位符=>“您的全名”、:maxlength=>“55”、:自动对焦=>“自动对焦”%> “目标”,:占位符=>”example@gmail.com“,:maxlength=>“55”%> “目标”、:占位符=>“输入密码”、:maxlength=>“55”%> “目标”、:占位符=>“再次输入密码”、:maxlength=>“55”%> “按钮cta”%>
此外,如果有任何帮助,我将尝试遵循Railscast的本教程:


谢谢

我对rails相当陌生,但我认为build应该在create操作中

@user.stores.build(参数[:user][:stores])

我在猜测,因为我没有尝试构建这样的视图,我通常会分步骤创建一个用户创建一个用户,然后通过build创建关联
<% f.fields_for :preference do |builder| %>
这需要改进

<%= f.fields_for :preference do |builder| %>

这同样适用于对
f.fields\u的其他调用


来源:

感谢您的关注。不过,我的创建操作没有问题——我只是尝试渲染新的。在我看来,当我使用:
时,隐藏字段没有显示出来。没问题。很抱歉,我也错过了=。接球不错