Here is the premise:
I have a model called Rules, which has a corresponding controller and view. I'd like the user to be able to create a new rule from their Dashboard, which has a separate view and controller.
The dashboard has three tabs called "Rules", "Rulesets, and "Games." Each tab has its own partial, so for Rules I'm using the _rules.erb partial which looks like this
<div id="dashboard-rules" class="dashboard-panel ui-nav-panel">
<%= button_to'Create A Rule', '#', id: "create-rule-btn", class: "btn btn-primary" %>
<div id="create-rule-form-container">
</div>
...
</div>
Using jQuery, I'm loading the contents of the New Rules page which is controlled by the Rules controller, like this...
$('#create-rule-btn').click (e) ->
e.preventDefault()
$('#create-rule-form-container').load('/rules/new.html .form-horizontal')
This loads just the form from rules/new.html. My problem is that when I click the "Save Rule" button, it redirects to the /rules URL. And then either displays errors or says "Rule Saved Successfully." I'd like for it not to redirect and have the errors show in the dashboard...again, not redirect to /rules URL.
This is my Rules create action, which I think needs to be modified, I'm just not sure how:
def create
@rule = Rule.new(params[:rule])
respond_to do |format|
if @rule.save
format.html { redirect_to @rule, notice: 'Rule was successfully created.' }
format.json { render json: @rule, status: :created, location: @rule }
else
format.html { redirect_to :controler => "dashboard"}
format.json { render json: @rule.errors, status: :unprocessable_entity }
end
end
end
I know I have to fiddle with how respond_to is working, I just don't know how.