Search code examples
ruby-on-railsobjectpartial

Render partial with object, rails


I am stuck in basic stuff.

I have subscribes controller and I created form partial, so I could make new subscribtion from contact page.

My partial looks like this :

<%= form_for(@subscribe) do |f| %>
  <% if @subscribe.errors.any? %>
    <div id="error_explanation">
      <h2><%= pluralize(@subscribe.errors.count, "error") %> prohibited this subscribe from being saved:</h2>

      <ul>
      <% @subscribe.errors.full_messages.each do |msg| %>
        <li><%= msg %></li>
      <% end %>
      </ul>
    </div>
  <% end %>

  <div class="field">
    <%= f.label :name %><br />
    <%= f.text_field :name %>
  </div>
  <div class="field">
    <%= f.label :subject %><br />
    <%= f.text_field :subject %>
  </div>
  <div class="field">
    <%= f.label :message %><br />
    <%= f.text_area :message %>
  </div>
  <div class="actions">
    <%= f.submit %>
  </div>
<% end %>

I am rendering partial using this code inside contact/index page :

<%= render 'subscribes/form', :locals => { :subscribe => @subscribe} %>

After this I get such error :

undefined method `model_name' for NilClass:Class

I tried to change object names , some syntax from similair sugestions in stuck overflow.

Like :

render "subscribes/form", collection: @subscribe
render :partial => 'subscribes/form', :object => Subscribe.new

I guess I am doing something completely wrong or some damm simple mistake. I am doing. :)


Solution

  • Well, first of all you are not using passed argument at all in your view - it would be subscribe, not @subscribe. It does not change the fact it should work as instance variable should be set. It looks like it is set to nil - you need to check your controller action and find out why is that.