Weired render action question! help!

Weired render action question! help!
In a topic controller

def edit
@topic = Topic.find(params[:id])
end
def update
@topic = Topic.find(params[:id])
@topic.subject = params[:topic][:subject]
@topic.body = params[:topic][:body]
@topic.modificationDate = Time.now
if @topic.update_attributes(params[:topic])
flash[:notice] = ‘Topic was successfully updated.’
redirect_to :action => ‘show’, :id => @topic.root_id
else
flash[:notice] = ‘Failed to update the Topic.’
render :action => ‘edit’
end
end

edit.rhtml forum action --> update, if user input error, such as subject
is null,then will render back edit.rhtml, and display the error message!
it works!

def newTopic
@topic = Topic.new
end
def createTopic
@topic = Topic.new(params[‘topic’])
@topic.creationDate = Time.now
@topic.modificationDate = Time.now
if @topic.save
flash[:notice] = ‘Topic was successfully created.’
redirect_to :action => ‘index’
else
flash[:notice] = ‘Failed to create a Topic’
render :action => ‘newTopic’
end
end

if you want to post a new root topic, if subject is null, then render
back to newTopic.rhtml, display error message!

but…

def reply
@parent = Topic.find(params[:id])
end
def replyTopic
parent = Topic.find(params[:parentID])
@topic = Topic.new(params[‘topic’])
if parent.root?
root_id = params[:parentID]
else
root_id = parent.root_id
end
@topic.root_id = root_id
@topic.creationDate = Time.now
@topic.modificationDate = Time.now
if @topic.save
flash[:notice] = ‘Topic was successfully created.’
parent.add_child(@topic)
redirect_to :action => ‘show’, :id => root_id
else
flash[:notice] = ‘Oops, there was a problem!’
# redirect_to :action => ‘new’, :id => params[:parentID]
render :action => ‘reply’
end
end

if you reply a topic, reply.rhtml will display parent topic information,
then forum action to replyTopic, if user input error, it could’nt render
back to reply.rhtml, display @parent nil object error! why?

newTopic --> createTopic, render works!
input parameter: id --> edit --> update, render works!
input parameter: id --> reply --> replyTopic, render dosen’t work!

why???

in your “reply” action, you defined @parent:

def reply
@parent = Topic.find(params[:id])
end

but in your replyTopic action, you define “parent”, which is a local
variable, and not @parent:
def replyTopic
parent = Topic.find(params[:parentID])
@topic = Topic.new(params[‘topic’])


end

therefore Rails has no @prent variable in the “reply.rhtml” view to
fill the forms, and throws an error.

the problem should be simply fixed by changing “parent” into “@parent
in the replyTopic action

On 3 Apr., 06:34, Chengcai He [email protected]