Papouche Guinslyzinho
Papouche Guinslyzinho

Reputation: 5448

rendering through ajax in rails

I would like to alert the success confirmation when I create a Recipient but I'm getting a missing template error:

ActionView::MissingTemplate - Missing partial recipients/_recipient 
  with {:locale=>[:fr], :formats=>[:js, :html],
   :variants=>[], :handlers=>[:erb, :builder, :raw, :ruby, :coffee, :jbuilder]}.

RecipientController.rb

respond_to :html

  def index
    @recipients = Recipient.all
    respond_with(@recipients)
  end

  def create
    @recipient = Recipient.new(recipient_params)

    if @recipient.save
      #RecipientMailer.confirmer_mon_inscription(@inscription).deliver
      #@recipient.destroy
      respond_to do |format|
        format.html { redirect_to @article, 
                        :notice => 'Thanks for your comment' }
        format.js { render 'success_create.js.erb'}
      end
    else
      respond_to do |format|
        format.html { redirect_to @article, 
                       :alert => 'Unable to add comment' }
        format.js { render 'fail_create.js.erb' }
      end
    end
  end

success_create.js.erb

alert("<%= @recipient.email %>");

fail_create.js.erb

alert("<%= @recipient.errors.full_messages.to_sentence %>");

there is something that I don't understand, why should I have to create a partial _recipient for a create action when I already specify which file to render

this is my form

<h1>S inscrire à la newsletter</h1>

<div class="newsletter">
  <div class="newsletter-contenu">
    <%= form_for(@recipient, :remote => true, :html => {:class => "form-group"}) do |f| %>
      <div class="field form-group">
       <%= f.label :email %><br />
        <%= f.text_field :email %>
      </div>
      <div class="actions form-group">
         <%= f.submit "S'inscrire à la newsletter", :class => "btn btn-info" %>
      </div>
    <% end %>
  </div>
</div>

Upvotes: 0

Views: 36

Answers (1)

rafb3
rafb3

Reputation: 1702

You are missing the :file option as described in the docs here. This should work

format.js { render file: 'fail_create.js.erb' }

Upvotes: 1

Related Questions