undefined method `valid_options' for nil:NilClass with simple_form and Mongoid

610 views
Skip to first unread message

hyperrjas

unread,
May 4, 2012, 12:25:42 PM5/4/12
to SimpleForm
I have two models, Category and Post.

**Category.rb**

class Category
include Mongoid::Document

field :title, :type => String
has_many :posts, :autosave => true, dependent: :destroy

end

**Post.rb**

class Post
include Mongoid::Document

field :title, :type => String
belongs_to :category
end

I'm using **[simple_form gem][1]**

If I write in my post form the next:

<%= simple_form_for(@post) do |f| %>
<%= f.collection_select :category,
Category.all, :id, :title, :prompt => "Choose a Category"%>
<%= f.input :title %>
<%= f.button :submit %>
<% end %>

The form does works fine :).

but if I use the next form with simple_form format:

<%= simple_form_for(@post) do |f| %>
<%= f.association :category, :prompt => "Choose a Category" %>
<%= f.input :title %>
<%= f.button :submit %>
<% end %>

I get the next error:

Completed 500 Internal Server Error in 23ms
ActionView::Template::Error (undefined method `valid_options' for
nil:NilClass):

**How can I fix it?
Thank you!**
[1]: https://github.com/plataformatec/simple_form

Carlos Antonio da Silva

unread,
May 4, 2012, 1:02:49 PM5/4/12
to plataformate...@googlegroups.com
SimpleForm associations do not work 100% ok with MongoId associations. That said, when using f.association, you just need to always add the :collection option to it, otherwise SimpleForm will try to get the association by itself, and it'll blow up.

-- 
At.
Carlos Antonio

hyperrjas

unread,
May 6, 2012, 8:31:28 AM5/6/12
to SimpleForm

Thank you Carlos Antonio, can you write a code example?

Thank you very much its a wonderfull gem :D.

On May 4, 10:02 am, Carlos Antonio da Silva

Carlos Antonio da Silva

unread,
May 6, 2012, 9:23:19 AM5/6/12
to plataformate...@googlegroups.com
Your old example is:

   f.collection_select :category, Category.all, :id, :title, :prompt => "Choose a Category"

With SimpleForm you're using:

    f.association :category, :prompt => "Choose a Category"

For MongoId, you need to give the collection, like this:

    f.association :category, :collection => Category.all, :prompt => "Choose a Category"

I believe that should work. And thanks for the appreciation :)

-- 
At.
Carlos Antonio

hyperrjas

unread,
May 6, 2012, 10:40:16 AM5/6/12
to SimpleForm
Thank you Antonio :D.

With f.association :category, :collection => Category.all, :prompt =>
"Choose a Category"

I get the error:

undefined method `category_ids' for #<Post:0xe3be0bc>

Thank you very much again :)

On May 6, 6:23 am, Carlos Antonio da Silva
<carlosantoniodasi...@gmail.com> wrote:
> Your old example is:
>
>    f.collection_select :category, Category.all, :id, :title, :prompt => "Choose a Category"
>
> With SimpleForm you're using:
>
>     f.association :category, :prompt => "Choose a Category"
>
> For MongoId, you need to give the collection, like this:
>
>     f.association :category, :collection => Category.all, :prompt => "Choose a Category"
>
> I believe that should work. And thanks for the appreciation :)
>
> --
> At.
> Carlos Antonio
>
>
>
>
>
>
>
> On Sunday, May 6, 2012 at 9:31 AM, hyperrjas wrote:
>
> > Thank you Carlos Antonio, can you write a code example?
>
> > Thank you very much its a wonderfull gem :D.
>
> > On May 4, 10:02 am, Carlos Antonio da Silva

Carlos Antonio da Silva

unread,
May 6, 2012, 11:16:06 AM5/6/12
to plataformate...@googlegroups.com
Hm that's weird.. well, try to go with input so (forget about association):

    f.input :category, :collection => Category.all, :prompt => "Choose a Category"

-- 
At.
Carlos Antonio

hyperrjas

unread,
May 6, 2012, 11:39:10 AM5/6/12
to SimpleForm
wooow Thank you very much Carlos Antonio the fix does works very fine
now :D.

Thank you very much again :D.

On May 6, 8:16 am, Carlos Antonio da Silva
<carlosantoniodasi...@gmail.com> wrote:
> Hm that's weird.. well, try to go with input so (forget about association):
>
>     f.input :category, :collection => Category.all, :prompt => "Choose a Category"
>
> --
> At.
> Carlos Antonio
>
>
>
>
>
>
>
> On Sunday, May 6, 2012 at 11:40 AM, hyperrjas wrote:
> > Thank you Antonio :D.
>
> > With f.association :category, :collection => Category.all, :prompt =>
> > "Choose a Category"
>
> > I get the error:
>
> > undefined method `category_ids' for #<Post:0xe3be0bc>
>
> > Thank you very much again :)
>
> > On May 6, 6:23 am, Carlos Antonio da Silva

Carlos Antonio da Silva

unread,
May 6, 2012, 12:08:18 PM5/6/12
to plataformate...@googlegroups.com
Great, no problem :)

-- 
At.
Carlos Antonio

Reply all
Reply to author
Forward
0 new messages