0

I have an Activity and Type Model.

Activities has_many Types, Types has_many Activities

I would like to have checkboxes for all types when I create an activity so the ones that are checked, are the types of the new activity.

in my controller I did send all the types(@types) and in my view i did this

<div class = "field">
   Choose types:
   <% for type in @types %>
      <%= f.check_box :type %>
   <% end %>
</div>
  1. I get an error undefined method 'type'
  2. I don't know what to do in the create action to get this to work
1
  • 1
    type is a reserve keyword for single table inheritance Commented Dec 22, 2013 at 12:34

2 Answers 2

1

try this

<%= check_box_tag 'type_ids[]', type.id %><%= type.name %>
Sign up to request clarification or add additional context in comments.

2 Comments

I tried this and saw the html generated and it works. My problem is, how do I access this information in the controller?
params[:type_ids]=["and all checked type ids will come here"] #["1", "2"]
1
   <% @types.each do |type| %>
      <%= check_box_tag 'type_ids[]', type.id %> <%= type.id %>
   <% end %>

3 Comments

Thanks, but now in my controller should i expect a params[:type]?
What is in @types? View generated html source.
@types =Types.all. So I want to be able to choose the type of the activity using this checkbox. I saw the html source and notices all the checkbox had the same value but I want the value for the checkbox to be the id of type

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.