AJAX in Rails: show a new model of a new model after model update is complete #

I have a Rails account setup wizard in which I display a list of companies. When company data is updated through AJAX, everything works fine. However, after a successful upgrade, I would also like to replace the Update Client form with the New Client form.

It seems like the right thing is to use AJAX to update the form containing the element, with the answer of the JS format from # new companies. Is this possible due to a reaction to the new format of JS companies?

+2
source share
1 answer

Yes, using ajax is a good thing in this case. As reported, company data is successfully updated through ajax.

What you can do is do json in the update action if the current update is successful, and submit the new form as json.

def update @company = Company.find(params[:id]) respond_to do |format| if @company.update_attributes(params[:company]) format.json { render json: { success: true, html: render_to_string('_form.html.slim', layout: false) } } format.html { redirect_to @company, notice: 'Company was successfully updated.' } else ... 

You can access json in js and replace the current form element.

 $('#company_form').live 'ajax:success', (event,data) -> $('#current_company_form').html(data.html) if(data.success == true) 

Let me know if this helps.

+2
source

Source: https://habr.com/ru/post/909797/


All Articles