Ruby on rails 通过使用Rails重构来删除重复的表单代码

Ruby on rails 通过使用Rails重构来删除重复的表单代码,ruby-on-rails,ruby-on-rails-3,refactoring,ruby-on-rails-3.2,code-duplication,Ruby On Rails,Ruby On Rails 3,Refactoring,Ruby On Rails 3.2,Code Duplication,失败的示例: Finished in 15.53 seconds ←[31m86 examples, 4 failures←[0m 我不明白为什么我会犯这个错误。我按照ruby指南问题5中的说明进行操作 app/views/users/new.html.erb ←[31mrspec ./spec/requests/user_pages_spec.rb:95←[0m ←[36m# User pages signup wi th valid information should create a

失败的示例:

Finished in 15.53 seconds
←[31m86 examples, 4 failures←[0m
我不明白为什么我会犯这个错误。我按照ruby指南问题5中的说明进行操作

app/views/users/new.html.erb

←[31mrspec ./spec/requests/user_pages_spec.rb:95←[0m ←[36m# User pages signup wi
th valid information should create a user←[0m
←[31mrspec ./spec/requests/user_pages_spec.rb:104←[0m ←[36m# User pages signup w
ith valid information after saving the user ←[0m
←[31mrspec ./spec/requests/user_pages_spec.rb:105←[0m ←[36m# User pages signup w
ith valid information after saving the user ←[0m
←[31mrspec ./spec/requests/user_pages_spec.rb:106←[0m ←[36m# User pages signup w
ith valid information after saving the user ←[0m

注册
app/views/users/edit.html.erb

<%= provide(:title, 'Sign up') %>
<h1>Sign up</h1>

<div class="row">
<div class="span6 offset3">
<%= form_for(@user) do |f| %>
<%= render 'fields', f: f %>
<%= f.submit "Create my account", class: "btn btn-large btn-primary" %>
<% end %>
</div>
</div>

更新您的个人资料
“_new”%>
app/views/users/_fields.html.erb

<%= provide(:title, 'Edit user') %>
<h1>Update your profile</h1>

<div class="row">
<div class="span6 offset3">
<%= form_for(@user) do |f| %>
<%= render 'fields', f: f %>
<%= f.submit "Save changes", class: "btn btn-large btn-primary" %>
<% end %>

<%= gravatar_for @user %>
<%= link_to "change", "http://gravatar.com/emails", :target => "_new"%>

</div>
</div>

当我不打算重构这段代码,并让它重复时,我不会得到任何错误。。所以我认为/spec/requests/user\u pages\u spec.rb实际上没有问题。。如有任何建议,将不胜感激

这很奇怪,因为这个应用程序似乎没有任何问题(或者说它似乎没有问题),但是是的,它工作得非常好。。正是那个错误困扰着我


-Marc

我不是专家,但我不认识将表单生成器传递给partial(
f:f
)的语法。我知道这是从你链接到的网站上得到的,但是试着这样做:
{:f=>f}%>
我觉得你的视图代码很好。您能否从/spec/requests/user\u pages\u spec.rb文件中发布相关代码?
<%= render 'shared/error_messages' %>

<%= f.label :name %>
<%= f.text_field :name %>

<%= f.label :email %>
<%= f.text_field :email %>

<%= f.label :password %>
<%= f.password_field :password %>

<%= f.label :password_confirmation, "Confirm Password" %>
<%= f.password_field :password_confirmation %>