0

I have created a modal form that submits values via remote => true (AJAX) and stores it in the database. This functionality works as records are saved in the database. Here is my controller code:

respond_to :js, only: [:create_agent_confirmation]  

def create_agent_confirmation
 if @agent_confirm.validate(params[:agent_confirm])
    @agent_confirm.save
    respond_with(@agent_confirm, :location => root_path)
  else
    respond_with(@agent_confirm, :location => contact_path)
  end
end

And in my Modal (View):

#agent_confirm_modal.small.reveal-modal{"data-reveal" => ""}
= simple_form_for  [:customer, @agent_confirm], :url => create_agent_confirm_customers_path, :remote => true do |f|
    .row
        .sub-headline-secondary
            .padding-16-balanced-bottom
                = "Agent Confirmation"

        .col-1-1-1.padding-16-all

            .padding-12-bottom
                = f.input :detail_verification, as: :boolean, label: "I have confirmed/updated the customer's details"

            .padding-12-bottom
                = f.input :document_verification, as: :boolean, label: "I have confirmed/updated the customer's documentation"

            %a.close-reveal-modal= "×".html_safe

        .padding-16-all.margin-gutter-bottom
            = f.button :submit, "Submit", class: "button right tiny radius"

The questions that I have:

  1. How do I populate the form with relevant validation errors, using js?
  2. After saving the record successfully, I want to redirect to an entirely different page. The controller code listed above returns an error in Firebug stating that the relevant template for "create_agent_confirmation" could not be found (even though I indicate that I need to return to root_path).

PS* I only need to be redirected if the record has been successfully created.

Any ideas?

HermannHH
  • 1,732
  • 1
  • 27
  • 57
  • 2
    i would suggest you to create a `render_root_path.js.haml` and add a javascript `window.location('root_path')`. So, in controller when successfull creation is done `render render_root_path.js` – pkrawat1 Jan 24 '15 at 15:12
  • 1
    Yes, you should [render js view](http://guides.rubyonrails.org/working_with_javascript_in_rails.html#server-side-concerns) – zishe Jan 24 '15 at 15:16
  • same is for the sowing validation errors, just create a js file for that view and add code like this `$('body').html("#{escape_javascript(render template 'template')}");` – pkrawat1 Jan 24 '15 at 15:24

0 Answers0