我有两个表(副词,单词)和两个控制器(副词,单词)。我希望有一个表单来添加和编辑两个表的记录。 副词控制器:
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
我使用Partial form
<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>
和我的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 :(得分:0)
我认为在获取单词controller的新动作时会引发此异常。在_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,这就是您看到此错误的原因。