fields_for for nested model

868 views
Skip to first unread message

rgoytacaz

unread,
Jan 26, 2011, 7:01:22 PM1/26/11
to SimpleForm
Hello there,

I got this model and I'm trying to get a form around it.

class User
belongs_to :mode
has_many :questions, :through => :mode
has_many :answers

end

class Question
belongs_to :mode
has_many :options
end

class Answer
belongs_to :user
belongs_to :question
belongs_to :option
end

I got this form going on;


<%= simple_form_for @user do |f| %>
<%= f.error_notification %>

<%= f.simple_fields_for :answers do |af| %>

<% @user.questions.each do |q| %>
<%= af.label :question, q.text %>
<%= af.hidden :question, :value => q.id %>
<%= af.input :option, :collection => q.options, :as => :radio
<% end %>

<% end %>
<% end %>

What I want with this is that the label contains the question text and
its id in a hidden input, and its available options for the user to
choose.

But this isn't working for me, I clearly don't know how to use this
properly.

Can any1 help me? Thanks

Carlos Antonio da Silva

unread,
Jan 26, 2011, 7:21:49 PM1/26/11
to plataformate...@googlegroups.com
I believe your question is more Rails related than SimpleForm.
I have a post from some time ago which explains the basic of nested attributes: http://carlosantoniodasilva.wordpress.com/2009/03/28/rails-23-nested-attributes-atributos-aninhados/

Maybe this can help you more than an explanation here =).
--
At.
Carlos A. da Silva

rgoytacaz

unread,
Jan 26, 2011, 7:50:31 PM1/26/11
to SimpleForm
Carlos,

But hidden doesn't work, as simple form doesn't have that method.

is there anything wrong with it?

how do I get the collection to display the model.text instead of
object_id?

Thanks

On Jan 26, 10:21 pm, Carlos Antonio da Silva
<carlosantoniodasi...@gmail.com> wrote:
> I believe your question is more Rails related than SimpleForm.
> I have a post from some time ago which explains the basic of nested
> attributes:http://carlosantoniodasilva.wordpress.com/2009/03/28/rails-23-nested-...

Carlos Antonio da Silva

unread,
Jan 26, 2011, 8:38:07 PM1/26/11
to plataformate...@googlegroups.com
SimpleForm has all Rails helpers, it inherits from default Rails FormBuilder.
You can use f.hidden_field, as well as SimpleForm method, f.input :foo, :as => :hidden.

About the model.text, I guess I didn't get what you want.

rgoytacaz

unread,
Jan 26, 2011, 8:45:36 PM1/26/11
to SimpleForm
Ah I see, :as => :hidden.

In the readme of simple_form, it says you can use f.hidden.

For model.text, I refer to this;

<%= af.input :option, :collection => q.options, :as => :radio %>

This displays radio buttons with #<Option:0xafa24a2> etc..

How do I get this to display option.text? or option.name? or any other
model attribute?

[]'s
Rodrigo Dellacqua

On Jan 26, 11:38 pm, Carlos Antonio da Silva

Carlos Antonio da Silva

unread,
Jan 26, 2011, 9:00:11 PM1/26/11
to plataformate...@googlegroups.com
Ahhh got it =).
SimpleForm has a configuration where it lists all attributes to lookup when creating the collection:

You can add the :text attribute to this configuration, or you can use the :label_method and :value_method options when calling input.

<%= af.input :option, :collection => q.options, :as => :radio, :label_method => :text %>

Take a look at the docs for these options if you have any doubt
Reply all
Reply to author
Forward
0 new messages