Render new.js.erb in rails create controller (Rails 6)

Viewed 459

I am trying to render new.js.erb incase a validation fails. I have this in my rails create action

  def create
    @talent = Talent.new(talent_params)
    @job =  params[:job_id]
    @talent.job_id = @job
    respond_to do |format|
      if @talent.save
        flash[:notice] = "You have successfully completed your job application!"
        redirect_to jobs_path
      else
        format.html
        format.js { render "new" }
      end
    end
  end

In new.js.erb, I have

$('<%= j render "form" %>').modal();

in _form.html.erb (the form partial rendered in new.js.erb), I have

<div id="contact-modal" class="modal fade">
  <div class="modal-dialog">
    <div class="modal-content">
      <%= simple_form_for @talent, :url => job_talents_path(@job, @talent), :method => :post do |f| %>
        <div class="modal-body">
          <%= f.input :full_name, label: "Full Name", :autofocus => true %>
          <%= f.input :email, label: "Email" %>
          <%= f.input :mobile_number, label: "Mobile Number" %>
          <%= f.input :resume %>
          <%= f.input :cover_letter %>
          <%= f.submit 'Submit Application', class: "btn btn-success btn-lg btn-block upload-margin create-style"%>
        </div>
        <div class="modal-footer">
          <span style="float: left; color: #5DB1D1;">Goodluck with your application<i class="fas fa-hand-peace" aria-hidden="true"></i></span>
        </div>
      <% end %>
    </div>
  </div>
</div>

Incase of a validation failure, the controller fails to render new.js.erb. Any help to solve this will be appreciated

1 Answers

One of the problems may be that you can't implicitly pass local rails variables to a partial. This syntax for explicitly passing vars to a partial may help you:

Rails 4 - passing variable to partial

Related