Rails 3.2.8 - 渲染调用后实例变量消失

时间:2012-10-21 16:51:20

标签: ruby-on-rails-3 cancan

在users_controller中,create的代码如下所示

@user = User.new(params[:user])
...
@client = the client this user belongs to
@account = the account this user belongs to
if @user.save
 ...
else
 render :new
end

当验证失败时,它开始渲染new.html.erb,但@client和@account在渲染调用中不存在且为零。奇怪的是@user存活得很好。这里发生了什么?这是康康的副作用吗?

这里是new.html.erb视图。变量@client和@account用于部分。输入新表格后,@ client和@account为零。

<% puts "***TRACE NEW.HTML.ERB @user:#{@user.inspect}"%>
<% puts "***TRACE NEW.HTML.ERB @client:#{@client.inspect}"%>
<% puts "***TRACE NEW.HTML.ERB @account:#{@account.inspect}"%>
<%= simple_form_for(@user, :url => {:action => 'create', :id => @user.id}, :html => {:class => 'form-horizontal'}) do |f| %>
  <%= render(:partial => 'form', :locals => {:f => f, :legend => "Add a New User", :label => "Create", :ph => "set this user's password"}) %>
<% end %>

您可以查看完整跟踪here

0 个答案:

没有答案