改善了我的表单外观后出现HashWithIndifferentAccess错误

时间:2014-09-12 10:32:55

标签: 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"并将密码设置为"密码" - 然后将新用户重新路由到其“组织”页面。我不能为我的生活解决为什么会出现这个HashWithIndifferentAccess问题(在我已经更新了html / css格式的所有控制器上都是同样的问题)。

无论如何,这里是相关的组织控制员:

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)行改为&#34; form_for(:organization)&#34;它具有相同的功能。

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

描述&#34;注册&#34;做

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

感谢任何帮助。

1 个答案:

答案 0 :(得分:1)

form_for已经负责创建<form>代码,因此您实际上将表单嵌套在另一个表单中。

如果要将引导类应用于表单,请使用:

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

并删除<form> html标记。