Appending a partial in rails using AJAX

I have a section on my page where users can add a ‘project’. After they submit the ‘project’ form, a new project form field will appear beside the old project.

Basically I have a ‘project’ form in a partial erb in rails. On the button click, if the form fields are valid, I want to append a ‘project’ partial to parent div. Trying the below code, but instead of actually rendering the view - I get the literal HTML code showing up on the page (<%= render partial: ‘projects/project’) %>)

 if (projectUrl && projectName && projectDescription) {
      $.ajax({
          type: "POST",
          url: '/projects',
          data: { project: { name: projectName, description: projectDescription, url: projectUrl, user_id: projectUser } },
          success: function(data) {
            $('.projects').append("<%= render partial: 'projects/project') %>").html_safe;
            return false;
          },
          error: function(xhr, textStatus, error){
          console.log(xhr.statusText);
          console.log(textStatus);
          console.log(error);
          return false;
          }
       });
    }

I don’t know anything about Ruby or partials, but it looks like your calling a property .html_safe instead of a method .html_safe(). Try this:

$('.projects').append("<%= render partial: 'projects/project') %>").html_safe()

Hmmm… says error undefined function when I add ( )

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.