سؤال

I'm trying to render this form:

<form class="form-inline">
    <%= simple_form_for @prospect, 
    :url => url_for(:action => 'create', :controller => 'prospects'),
    :method => 'post' do |f| %>
    <%= f.error_notification %>
    <%= f.input :name,  placeholder: 'Name', label: false %>
    <%= f.input :email, placeholder: 'Email', label: false %>
    <%= f.input :interests, placeholder: 'Tell us what you were searching for', label: false, value: params[:search]  %>    
    <%= f.error :base %>
    <%= f.button :submit, "Submit", :class=> "btn" %>
<% end %>

Using this partial:

 <%= render partial: 'prospects/novideo_capture' %>

The partial is in a view controlled by Videos#index controller, and I keep getting this error: 'undefined method `model_name' for NilClass:Class'

This is my prospects controller:

class ProspectsController < ApplicationController
  def index
    @prospects = Prospect.all
  end

  def new
    @prospect = Prospect.new
  end

  def create
    @prospect = Prospect.new(params[:prospect])
    if @prospect.save
    render "thanks_for_interest"
    else
    render "novideo_capture"
  end
end

I'm not sure what I'm going wrong, although I'm pretty sure it's a simple solution. I've seen a lot of similar questions around SO and tried all their answers, but none of them seem to work for this situation.

Thanks for any help...

EDIT: Adding

@prospect = Prospect.new

to the videos index controller stops the error occurring, but I don't feel it's the right way to do this. It also doesn't actually make the form use the prospects controller.

EDIT2: I now have the partial rendering correctly (I think), and my videos#index calls the partial like this:

 <%= render partial: 'prospects/novideo_capture', :prospect => @prospect %>

Then simple_form in the partial looks like this:

<form class="form-inline">
    <%= simple_form_for :prospect, 
    :url => url_for(:action => 'create', :controller => 'prospects'),
    :method => 'post' do |f| %>
 ...
<% end %>

However it's not actually submitting the form with the prospects controller. Any ideas why?

هل كانت مفيدة؟

المحلول

Check your markup. You're wrapping a simple_form inside another form. Since the first form tag has no action associated with it (<form class="form-inline">), that form will submit against the current URL, which is the video#index.

You're going to want something like this:

<%= simple_form_for :prospect, :url => etc, :method => 'post', :class => "form-inline" do |f|
  ...
<% end %>

Losing the leading (redundant) form-inline form tag and you'll be fine.

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top