Commenting in new.erb template file

app/views/category/new.erb

In this file , i have two line like this

But, if these two lines are present , i get an error saying
“you have an unexpected nil error”


here is the full file:
app/views/category# more new*

Add new Category

<%= form_tag :action => 'create'%>

Category Title: <%= text_field 'category', 'category_title' %>

Category Short Description: <%= text_field 'category', 'category_short_desc' %>

Category Long Description: <%= text_area 'category', 'category_long_desc' %>

<%= submit_tag “Create” %>
<%= form_tag %>
<%= link_to ‘Back’, {:action => ‘list’} %>


If i remove these two lines

then new.erb works fine.

Why so?
Why can’t comment these two lines?

I’m sure there is some problem. But, i couldn’t see what it is.

please let me know if u could what the problem is in cmmenting these two
lines.

thanks,
radha

well its strange :))
you are Doing exactly ok for comments as i see

You should post this with new username
RailsConfusing Radhathat may works…:slight_smile:

On Sun, Jun 27, 2010 at 10:05 AM, RailsFan R.
[email protected]wrote:

Why can’t comment these two lines?
radha
For more options, visit this group at
http://groups.google.com/group/rubyonrails-talk?hl=en.


Thanks:
Rajeev sharma

please look at this also

On Sun, Jun 27, 2010 at 10:13 AM, kannav rajeev
[email protected]wrote:

“you have an unexpected nil error”
<%= text_field ‘category’, ‘category_short_desc’ %>

If i remove these two lines
I’m sure there is some problem. But, i couldn’t see what it is.
Posted via http://www.ruby-forum.com/.


Thanks:
Rajeev sharma


Thanks:
Rajeev sharma

Hi,

I am looking for Matz and Keiju’s book is first published, the first
Ruby
book.Please mail if any one have,

Cheers,
Sai

On Sun, Jun 27, 2010 at 11:02 AM, Hassan S. <

On Sat, Jun 26, 2010 at 9:35 PM, RailsFan R. [email protected]
wrote:

But, if these two lines are present , i get an error saying
“you have an unexpected nil error”

So find where your Ruby error is – this certainly has nothing to do
with HTML commenting.


Hassan S. ------------------------ [email protected]
twitter: @hassan

Ugis Ozols wrote:

Can you post categories controller new action content?

here is this:
though the erb is called as “new.erb”. the action in the controller is
named as “create”

def create
@category = Category.new(params[:category])
if @category.save
redirect_to :action => ‘list’
else
# @subjects = Subject.find(:all)
render :action => ‘new’
end
end

Can you post categories controller new action content?

here is the full content of category_controller.rb

class CategoryController < ApplicationController
def index

“List all categories”

list
render:action=>‘list’
end

#— List–
def list
# @categories=Category.find_recent_categories
@categories=Category.find_all_categories
end
#---- Show —
def show
# @category=Category.find(params[:category_id])
@category=Category.find(params[:category_id])
end

#-------------edit -----------
def edit
@category=Category.find(params[:id])
# @book = Book.find(params[:id])
# @subjects = Subject.find(:all)
end

#-----create ----------
def create
@category = Category.new(params[:category])
if @category.save
redirect_to :action => ‘list’
else
# @subjects = Subject.find(:all)
render :action => ‘new’
end
end

— Update —

def update
@category = Category.find(params[:category_id])
if @category.update_attributes(params[:category])
flash[:notice]=‘Successfully updated’
redirect_to:action=> ‘show’, :category_id => @category
else
render:action => ‘edit’
end
end

#-----------Delete ------------
def delete
Category.find(params[:id]).destroy
redirect_to :action => ‘list’
end

end

On Sun, Jun 27, 2010 at 5:26 AM, RailsFan R. [email protected]
wrote:

here is the exact error message:

You have a nil object when you didn’t expect it!
You might have expected an instance of Array.

Extracted source (around line #14):

14: <!-- <%= collection_select(:book,:subject_id,@subjects,:id,:name)

Hint: where on line 14 do you see something that should be an Array?

And where is the value of that Array object being set in your code?


Hassan S. ------------------------ [email protected]
twitter: @hassan

Just thought of giving a bit moe info:
So, i added these two lines back into new.erb and reproduced this error.

here is the exact error message:


NoMethodError in Category#new

Showing app/views/category/new.erb where line #14 raised:

You have a nil object when you didn’t expect it!
You might have expected an instance of Array.
The error occurred while evaluating nil.map

Extracted source (around line #14):

11:
12:
13:
14:
15:
16:
17:


And when i remove these two lines, new.erb works fine.

thanks,
radha

On 27 June 2010 05:35, RailsFan R. [email protected] wrote:

“you have an unexpected nil error”
Regardless of the HTML comments in your view file, the Ruby inside the
<% %> tags will get evaluated - if it’s invalid it will error (as it
is); if it’s valid, you’d end up with a commented-out html select
tag…

if you just want it commented, comment the Ruby:

If you want a select tag, read the documents on collection_select and
ensure @subjects is correctly populated with objects (or is an empty
array).

hi,
you can not just comment these lines because the second line having
Ruby code inside<%= …%>
for this line you may use # like as written below.
<%#= collection_select(:book,:subject_id,@subjects,:id,:name) %>
Now it is commented out. And code will not read this line…

Anything else, You are most welcome.

Santosh

change

to <%#= collection_select(:book,:subject_id,@subjects,:id,:name) %>

I am new in ruby but i will try to help you.
I think that in your controller should have the new def
def new

@subjects = Subject.find(:all)

end

this will give information that you need in your view file.

ssmithstone wrote:

change

to <%#= collection_select(:book,:subject_id,@subjects,:id,:name) %>

thanks for all . thanks rajeev and hassan and michael.
thanks MicahelPavling, santosh, axelsef and ssmithstone.

special thanks to michael, santosh and ssmithstone, I commented those
two lines as per your explanation as ruby evaluates them first before
generating the html and it works fine.

thanks again!

-radha