Search code examples
ruby-on-railsrubyrenderpartial

Render form from partial when object invalid


On my landing page i have a contact form

<%= render 'contacts/form' %>

Everything works great when object is valid. But i have a problem with render this contact form on my landing page with information whats attributes are wrong.

This is my controller:

class ContactsController < ApplicationController

  def new
  end

  def create
    @contact = Contact.new(contact_params)
    if @contact.save
      redirect_to root_url
      flash[:success] = "Dziękuję za wysłanie wiadomości"
    else
      render 'homes/home'
    end
  end

  private

  def contact_params
    params.require(:contact).permit(:podpis, :wiadomosc, :email, :nickname)
  end
end

My homes/home template:

  <div id="page-content">

  <!-- O mnie -->
  <%= render 'o_mnie' %>         

  <!-- Resume -->
  <%= render 'resume' %>

  <!-- Blog -->
  <%= render 'blog' %>

  <!-- Portfolio -->
  <%#= render 'projekty' %>  

  <!-- Contact -->
  <%= render 'kontakt' %>

</div>

This is my application layout

<body>
     <%= render 'layouts/header' %>

    <div id="page-content">
        <%= render 'shared/flashes', flash: flash %> 
    </div>

    <%= yield %>
</body>

When i check on stack similar problems, now i know that the problem laying in render 'homes/home'. This gives me errors

Missing partial contacts/_o_mnie, application/_o_mnie with {:locale=>[:pl], :formats=>[:html], :variants=>[], :handlers=>[:erb, :builder, :raw, :ruby, :jbuilder, :coffee]}. Searched in:

When i try render root_url, i have the same problem. What is wrong with my code?


Solution

  • If the template is used by other controller, you have to specify the prefix.

    Change to:

    <div id="page-content">
    
      <!-- O mnie -->
      <%= render 'homes/o_mnie' %>         
    
      <!-- Resume -->
      <%= render 'homes/resume' %>
    
      <!-- Blog -->
      <%= render 'homes/blog' %>
    
      <!-- Portfolio -->
      <%#= render 'homes/projekty' %>  
    
      <!-- Contact -->
      <%= render 'homes/kontakt' %>
    
    </div>