I'm trying to reorganize some controller code into .js.erb jquery files. (Warning, this is messy!)
I have this code in my create action:
def create
# do stuff
if @adverse_event.save
format.js{
template_id =@study.template_id
@template_adverse_event_columns = AdverseEventColumn.find(:all, :conditions => ["template_id is ? OR study_id is ?", template_id, @study.id])
@adverse_events = AdverseEvent.find(:all, :conditions=>['study_id=?',@study.id])
@adverse_event_result = AdverseEventResult.new
render :update do |page|
# replace some partials
end
}
format.html { redirect_to(@adverse_event, :notice => 'Adverse event was successfully created.') }
format.xml { render :xml => @adverse_event, :status => :created, :location 开发者_StackOverflow中文版=> @adverse_event }
else
problem_html = "Error message"
format.js{
render :update do |page|
page.replace_html 'adverse_event_validation_message', problem_html
end
}
format.html { render :action => "new" }
format.xml { render :xml => @adverse_event.errors, :status => :unprocessable_entity }
end
end
I'm trying to convert it to jquery so I can do all the div/html replacing, etc in a create.js.erb file. How do I do this with the conditional based on whether it saves? do I need two create.js.erb files, one for format.js if save is successful, and one if not? how would I call them? Am I on the right track, even?
Thanks so much and sorry if this isn't making a lot of sense - feel free to ask me to clarify and I will edit accordingly.
A tutorial worth plenty explanations:
http://www.stjhimy.com/posts/7-creating-a-100-ajax-crud-using-rails-3-and-unobtrusive-javascript
Bonus, a second one:
http://blog.bernatfarrero.com/jquery-and-rails-3-mini-tutorial/
精彩评论