Hi,
I am sorry for maybe stupid question. I am new in rails.
I am trying to create very simple weblog systeme. I know threre are
plenty of them, but It''s good to study. I create following rule in
models/post.rb
validates_length_of :header, :in => 3..50, :allow_nil => false,
:message => "At least 3 characters"
It''s working fine with default scaffolt add form.
Then I tryed to create own new.rhtml
<?xml version="1.0" encoding="ISO-8859-2"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="cs"
lang="cs">
<head>
<title>Test</title>
</head>
<body>
<form action="/post/create"
method="post">
<table>
<tr>
<td>Nadpis</td>
<td><%= text_field
"post",
"header", "size" => 50, "maxlength" => 40
%></td>
</tr>
<tr>
<td>Kategorie</td>
<td>
<select
id="post_category_id" name="post[category_id]">
<%options_from_collection_for_select(@categories, ''id'',
''category'') %>
</select>
</td>
</tr>
<tr>
<td
colspan="2">Obsah</td>
</tr>
<tr>
<td
colspan="2"><%text_area("post", "value")
%></td>
</tr>
<tr>
<td
colspan="2"><%submit_tag("Save") %></td>
</tr>
</table>
</form>
</body>
</html>
when I insert correct datas it''s ok. But in case of mistake, I get
following screen
NoMethodError in Post#create
Showing /post/new.rhtml where line #18 raised undefined method
`inject'' for nil:NilClass
15: <td>Kategorie</td>
16: <td>
17: <select id="post_category_id"
name="post[category_id]">
18: <%= options_from_collection_for_select(@categories,
''id'',
''category'') %>
19: </select>
20: </td>
21: </tr>
How can I set up correct error message?
Thank you,
Michael
Hi Michael, Michal Koudelka wrote:>I am trying to create very simple weblog systeme. I know threre are >plenty of them, but It''s good to study. I create following rule in >models/post.rb > > >[snip]>when I insert correct datas it''s ok. But in case of mistake, I get >following screen > > NoMethodError in Post#create > >Showing /post/new.rhtml where line #18 raised undefined method >`inject'' for nil:NilClass > >15: <td>Kategorie</td> >16: <td> >17: <select id="post_category_id" name="post[category_id]"> >18: <%= options_from_collection_for_select(@categories, ''id'', >''category'') %> >19: </select> >20: </td> >21: </tr> > > >I think the problem is that in case the page is re-displayed because of an error, you don''t populate the @categories instance variable in your controller. You didn''t post your controller-code, so I can''t verify this, but I assume that you are only setting @categories in your Post#new method, but not in Post#create. Sebastian
I think it''s definitely like Sebastian said, and writting just for see czech here. Vitej :-) Just in case there some other czechs, tomorow we''ll have "battle" ruby/rails vs squeak(smalltalk)/seaside2 on Agriculture university. If you are interested, don''t wait and mail me for details. Pepe On 10.3.2005, at 19:06, Sebastian Kanthak wrote:> Hi Michael, > > Michal Koudelka wrote: > >> I am trying to create very simple weblog systeme. I know threre are >> plenty of them, but It''s good to study. I create following rule in >> models/post.rb >> >> > [snip]