2011-09-02 41 views
1

我有一個部分_new_user_form.html.erb不能通過當地人parital Rails的

<%= form_for(@user, :remote => true, :html => {:id => 'new_user_form'}) do |f|%> 
    <strong><%= :form_text %></strong> 
    <%= f.text_field :email, :placeholder => get_placeholder_text(@board), :size => "30" %> 
    <%= hidden_field_tag :role, role %> 
    <%=f.submit "SAVE", :class => "button-small" %> 
<% end %> 

在show.rb我想使用它,並通過在一些局部變量如下:

<%= render 'users/new_user_form', :locals=> {:role => "Celebrant" } %> 

然而我得到這個錯誤:

undefined local variable or method `role' for #<#<Class:0x00000103d5e8b0>:0x00000103d5b930> 

我讀了關於通過當地人的文件,這似乎是正確的。我究竟做錯了什麼?

回答

4

你正在結合短和長的形式。其中任何一個都是正確的(相同):

render 'my_partial', :foo => 'bar' 

render :partial => 'my_partial', :locals => { :foo => 'bar' } 
+0

感謝Coreyward回來如此之快。它使用第一行。 – chell

3

我認爲你錯誤地致電render。從fine manual

If no options hash is passed or :update specified, the default is to render a partial and use the second parameter as the locals hash.

所以,你最終在源走這分支:

view_renderer.render_partial(self, :partial => options, :locals => locals) 

,這讓你的電話與此相同:

render :partial => 'users/new_user_form', :locals => { :locals => { :role => 'Celebrant } } 

注意額外的嵌套級別爲:locals。試試這個:

render 'users/new_user_form', { :role => 'Celebrant' } 

我正在查看(和使用)3.1所以你的版本可能會有點不同。

+0

謝謝穆太短了。解釋太棒了。 – chell