html - 改进表单外观后出现 HashWithIndifferentAccess 错误

标签 html css ruby-on-rails forms twitter-bootstrap

我有一个完美的注册过程(以及应用程序中的许多其他表单),最近决定使用一系列 bootstrap 3 类让它看起来更好。不幸的是,我现在为了看起来更好而更新的所有表格都拒绝工作 - 它们只是在我的调试转储中抛出以下内容:

--- !ruby/hash:ActionController::Parameters
utf8: "✓"
authenticity_token: fKx7bdWtC7bmBKMRF3ivwBlmJXzrcWJ16dYYOfNLBC0=
organization: !ruby/hash:ActiveSupport::HashWithIndifferentAccess
organization_name: W
users: !ruby/hash:ActiveSupport::HashWithIndifferentAccess
name: K
email: w@k.com
password: password
password_confirmation: password
organization_id: ''
commit: Create my account
controller: organizations
action: new

在此之前,同一组信息会设置一个名为“W”的组织,用户名为“K”,密码设置为“password”——然后将新用户重新路由到他们的组织页面。我一辈子都弄不明白为什么会出现这个 HashWithIndifferentAccess 问题(这是我更新表单 html/css 的所有 Controller 上的同一个问题)。

无论如何,这是相关的组织 Controller :

def new
 @organization = Organization.new
 @organization.users.build
end

组织模型代码:

class Organization < ActiveRecord::Base
 validates :organization_name, presence: true, length: { maximum: 50 }, uniqueness: true 
 has_many :users, :inverse_of => :organization
 has_many :companies, :inverse_of => :organization
 has_many :products, :inverse_of => :organization
 has_many :competitors, :inverse_of => :organization
 accepts_nested_attributes_for :users
 after_create :set_admin

这是 form.html.erb - 我怀疑错误所在,因为这是我今天所做的全部更改:

<div class="container-fluid">
<h1>Sign up!</h1>
<div class="row">
</div>
    <div class="row">
        <div class="col-md-6 col-md-offset-3">
            <div class="well">
                <form class="form-horizontal" role="form">
                        <%= form_for(@organization) do |f| %>
                            <%= render 'shared/org_error_messages' %>
                             <div class="form-group">
                                <%= f.label :organization_name, :class => "col-md-4 control-label" %>
                                <div class ="col-md-8">
                                <%= f.text_field :organization_name, :placeholder => "Who do you work for?" %>
                                </div>
                            </div>
                                <%= f.fields_for :users, @organization.users_for_form do |user| %>
                                 <div class="form-group"> 
                                    <%= user.label :name, :class => "col-md-4 control-label" %>
                                    <div class ="col-md-8">
                                    <%= user.text_field :name, :placeholder => "What's your name?" %>
                                    </div>
                                </div>
                                 <div class="form-group">
                                    <%= user.label :email, :class => "col-md-4 control-label" %>
                                    <div class ="col-md-8">
                                    <%= user.text_field :email, :placeholder => "Email" %>
                                    </div>
                                </div>
                                 <div class="form-group">
                                    <%= user.label :password, :class => "col-md-4 control-label" %>
                                    <div class ="col-md-8">
                                    <%= user.password_field :password, :placeholder => "Enter password - minimum 6 characters" %>
                                    </div>
                                </div>
                                 <div class="form-group">
                                    <%= user.label :password_confirmation, "Repeat Password", :class => "col-md-4 control-label" %>
                                    <div class ="col-md-8">
                                    <%= user.password_field :password_confirmation, :placeholder => "Repeat password" %>
                                    </div>
                            </div>

                                <%= user.hidden_field :organization_id, input_html: {value: @organization.id} %>
                            <% end %>
                            <%= f.submit "Create my account", class: "btn btn-large btn-success" %>
                            <% end %>
                    </form>
            </div>
        </div>
    </div>

在这里阅读了关于此的另一个答案后,我已经尝试将 form_for(@organization) 行更改为“form_for(:organization)”,它具有相同的功能。

FWIW 我的一些 rspec 测试失败并向我显示此错误,而其他人则没有。组织/用户注册测试似乎仍然建议我可以通过此屏幕创建用户 - 我也将它们包括在下面,以防万一它可以帮助别人理解错误。

描述“注册”做

before { visit signup_path }

let(:submit) { "Create my account" }

describe "with invalid information" do
    it "should not create a user"  do
        expect { click_button submit }.not_to change(User, :count)
    end
  it "should not create an Organization" do
    expect { click_button submit }.not_to change(Organization, :count)
  end
  describe "after submission" do
    before { click_button submit }

    it { should have_title('Sign up') }
    it { should have_content('error') }
  end
end

describe "with valid information" do
    before do

        fill_in "Organization name", with: "Example Org"
        fill_in "Name",             with: "Example User"
        fill_in "Email",            with: "user@example.com"
        fill_in "Password",         with: "foobar"
        fill_in "Repeat Password",  with: "foobar"
    end

    it "should create a user" do
        expect { click_button submit }.to change(User, :count).by(1)
        end

  it "should create an Organization" do
  expect { click_button submit }.to change(Organization, :count).by(1) 
end

  describe "after saving the user" do
    before { click_button submit }
    let(:user) { User.find_by(email: 'user@example.com') }

    it { should have_link('Sign out') }
    it { should have_title(user.name) }
    it {should have_selector('div.alert.alert-success', text: 'Thanks') }
    it { should have_content(user.name) }
    it { should have_title(user.name) }
    end 
    end
end

如有任何帮助,我们将不胜感激。

最佳答案

form_for已经负责创建 <form>标记,所以您实际上是在将一个表单嵌套在另一个表单中。

如果要将 Bootstrap 类应用到表单,请使用:

<%= form_for(@organization, :html => {:class => "form-horizontal"}) do |f| %> 

并删除 <form> html 标签。

关于html - 改进表单外观后出现 HashWithIndifferentAccess 错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25806262/

相关文章:

javascript - 如何在 Angular 2 中动态添加样式表?

javascript - DOM level 2 相对于 DOM level 0 的优势是什么?

javascript - OpenLayers 添加标记和弹出窗口

html - 对齐字段集中的元素

javascript - 使用 z-index 在父 <div class> 上方显示 <ul class>?

ruby-on-rails - 在云环境 (heroku) 上的 ruby​​ on rails 中启动套接字服务器

html - 跨度内的 anchor 标记

javascript - 为什么下面的样式没有为这些元素分配设置值?

javascript - 在 Rails 中禁用 Javascript 变量重命名

ruby-on-rails - 如何在 JRuby on Rails 的开发模式下启用 View 文件的自动重新加载?