2011-09-04 99 views
0

管理多個非嵌套模型我有兩個表(副詞,文字)和兩個控制器(副詞,文字)。我希望有一個窗體上添加和編輯兩個表格的記錄。 副詞控制器:如何在導軌形式

class AdverbsController < ApplicationController 
    def new 
     @adverb=Adverb.new 
    end 
    def create 
    @adverb=Adverb.create(params[:adverb]) 

    if @adverb.save 
     redirect_to :action => 'index' 
    else 
     render :action => 'new' 
    end 
    end 
    def index 
     @adverb =Adverb.find(:all) 
    end 
    def edit 
     @adverb =Adverb.find(params[:id]) 
    end 
    def update 

    if @adverb.update_attributes(params[:adverb]) 
     redirect_to :action => 'index' 
    else 
     redirect_to :action => 'edit' 
    end 
    end 
end 

和字控制器:

class WordsController < ApplicationController 
    def new 
     @word=Word.new 

    end 
    def create 
    @word=Word.create(params[:word]) 

    if @word.save 
     redirect_to :action => 'index' 
    else 
     render :action => 'new' 
    end 
    end 
    def index 
     @word=Word.find(:all) 
    end 
    def edit 
     @word=Word.find(params[:id]) 
    end 
    def update 

    if @word.update_attributes(params[:word]) 
     redirect_to :action => 'index' 
    else 
     redirect_to :action => 'edit' 
    end 
    end 
end 

我使用部分表格

<html> 


<div id="tabs"> 
    <ul> 
     <li><a href="#tabs-1">word</a> </li> 
     <li><a href="#tabs-2">Adverb</a> </li> 
    </ul> 
    <div id="tabs-1"> 
     <table > 
     <tr> 
      <td align="right"> 
      Artikel : 
      </td> 
      <td align="center"> 
      <%= w.label :Artikel ,'der' %><%= w.radio_button :Artikel,'der',:checked =>false%> 
      <%= w.label :Artikel ,'die' %><%= w.radio_button :Artikel,'die',:checked =>true %> 
      <%= w.label :Artikel ,'das' %><%= w.radio_button :Artikel,'das',:checked =>false %> 
      </td> 
     </tr> 
     <tr> 
      <td align="right"> 
      Neues Wort: 
      </td> 
      <td> 
      <%= w.text_field :Name ,:size => '31*45'%> 
      </td> 
     </tr> 

     <tr> 
      <td align="right"> 
      Synonome : 
      </td> 
      <td> 
      <%= w.text_field :syn ,:size => '31*45'%> 
      </td> 
     </tr> 

     <tr> 
      <td align="right"> 
      Bedeutung : 
      </td> 
      <td> 
      <%= w.text_area :bedeutung , :size =>'30*45' %> 
      </td> 
     </tr> 
     <tr> 
      <td align="right"> 
       Beispiel : 
      </td> 
      <td> 
      <%= w.text_field :beispiel ,:size => '31*45' %> 
      </td> 
     </tr> 
     <tr> 
      <td> 
      Kommentar: 
      </td> 
      <td> 
      <%= w.text_area :kommentar ,:size =>'30*45' %> 
      </td> 
     </tr> 
     <tr> 
      <td align="center"> 
       <%= w.submit submit_lable %> 

      </td> 
     </tr> 
     </table> 
    </div> 

     <div id="tabs-2"> 
       <%= fields_for (@adverb) do |av| %> 
       <table> 
       <tr> 
        <td align="right"> 
        Adverb : 
        </td> 
        <td> 
         <%= av.text_field :name %> 
        </td> 
       </tr> 
       <tr> 
        <td align="right"> 
        Bedeutung : 
        </td> 
        <td> 
        <%= av.text_field :bedeutung %> 
        </td> 
       </tr> 
       <tr> 
        <td align="right"> 
        Synonome : 
        </td> 
        <td> 
         <%= av.text_field :syn %> 
        </td> 
       </tr> 
       <tr> 
        <td> 
         <%= w.submit submit_lable %> 
        </td> 
       </tr> 
       </table> 

       <% end %> 
      </div> 
    </div> 
</html> 

和IM我new.html.erb:

<html> 
<body> 
<%= form_for(@word) do |w| %> 
<%= render :partial => 'form',:locals => {:w =>w,:submit_lable =>'mmmm'} %> 
<% end %> 
</body> 
</html> 

但發生錯誤:

NoMethodError in Words#new 

Showing /home/babak/kurs/app/views/words/_form.erb where line #197 raised: 

undefined method `model_name' for NilClass:Class 

我該如何解決我的問題? 感謝您的幫助

+0

如果你的問題是形式,錯誤說,也許你可以在表單中添加的問題 – lucapette

回答

0

我假設得到的話控制器的新動作時,這個異常。在你_form.html.erb,你有下面的代碼:

<%= fields_for (@adverb) do |av| %> 
    <table> 
    <tr> 
     <td align="right"> 
     Adverb : 
     </td> 
     <td> 
     <%= av.text_field :name %> 
     </td> 
    </tr> 
    <tr> 
     <td align="right"> 
     Bedeutung : 
     </td> 
     <td> 
     <%= av.text_field :bedeutung %> 
     </td> 
    ... 
    </table> 
<%= end %> 

我見無處在新的地方@adverb聲明的話控制器的作用。實例變量默認爲nil,這就是您看到此錯誤的原因。

+0

感謝的,我知道,但我如何從一個控制器管理多個非相關的模型?它是否可行? –

+0

好一兩件事,得到這個工作是簡單地用Adverb.new取代@adverb。你也可以拆分的形式分解成更小的諧音,而做這樣的事情:'渲染:部分=>「adverb_form如果@ adverb''。祝你好運。 – Max