4rlekin
4rlekin

Reputation: 758

Controller's variables aren't working with form placed inside partial

I have - probably stupid - problem with render my form. I have two controllers: ContentController - responsible for content views of tabbed page and ParticipantsController scaffold generated for handling database model. I want to make a form for sign in new participants and put it in partial _new.html.erb which then will be embedded in "schedule" view of my ContentController. I'm really new to rails, so maybe I missed something out but when i put form like this:

<%= form_for @participant do |f| %> 
    <%= f.label :name %>
    <%= f.text_field :name %>
    ...

I got an error:

First argument in form cannot contain nil or be empty

In my controller I have:

def new
    @participant = Participant.new
end

So I guess everything should be alright.

When I use Participant.new instead of @participant in my form, everything is rendering correct; but im not sure if that will be working with database, I mean I want the new participant record to be created in db after clicking submit button.

I hope I pass enough info for You guys to help me out. Thanks in advance

Upvotes: 0

Views: 87

Answers (1)

LHH
LHH

Reputation: 3323

The error message is telling you that you can't have the following:

<%= form_for nil do |f| %>
<%= form_for [] do |f| %>

My guess here is that your @participant is set to nil and that it doesn't come from your Participantst#new action.

it would simply work if you do this:

<%= form_for Participant.new do |f| %>

Though it is not recommended.

You need to check that the view containing your form is actually rendered by the new action of your ParticipantsController. For more info see https://github.com/plataformatec/devise/issues/2451

Upvotes: 1

Related Questions