I've recently ran into a little for when an object fails to save due to serving content based of the current_page? method. I'm using a near scaffolding controller:
def create
@topic = Topics.new(params[:topic])
respond_to do |format|
if @topic.save
format.html { redirect_to(topic_links_path(@topic), :notice => "New Topic Created") }
else
format.html { render "new" }
format.xml { render :xml => @topic.errors, :status => :unprocessable_entity }
end
end
end
The problem is, if validation fails, it gets caught and then simply renders my new template. You are not on the new_topic_path route location causing my view helpers to serve the wrong content.
If my @topic does indeed fail to save, I could do a redirect_to, but I'm not sure how to preserve and display errors after a redirect?
Anyway, looking for some help for an elegant way to do this.
Edit for Clarifications:
Basically, what I'm doing is serving a different toolbar based of the current_page. So dev.app:3000/topics/new has a different toolbar than dev.app:3000/topics.
For my controller, if a @topic fails to save, it renders the new action, but still goes to the dev.app:3000/topics path rather than staying on the /topics/new path therefore displaying the wrong toolbar.
You should reconsider how your custom toolbar display thing works, instead of using current_page?
you should use action_name
and check whether it's equal to new
or to create
Instead of a redirect call:
render :action => :new
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.