管理多個非嵌套模型我有兩個表(副詞,文字)和兩個控制器(副詞,文字)。我希望有一個窗體上添加和編輯兩個表格的記錄。 副詞控制器:如何在導軌形式
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
我該如何解決我的問題? 感謝您的幫助
如果你的問題是形式,錯誤說,也許你可以在表單中添加的問題 – lucapette