Rails form associations for simple_form

Hacktacus

Hi I am having trouble with creating association using simple_form. Model Chapter belongs to Subject:

class Subject < ActiveRecord::Base

  validates :name, :presence => true,
                                    :length => {:maximum => 30},
                                    :uniqueness => true  

  has_many :chapters    

end

Model for Chapter:

class Chapter < ActiveRecord::Base

  validates :name, :presence => true,
                                    :length => {:maximum => 80}

  validates :subject_id, :presence => true

  belongs_to :subject

end

Controller for Chapter

  def new
    @chapter = Chapter.new
  end

  def create
    @chapter = Chapter.new(chapter_params)
    if @chapter.save
      flash[:notice] = "Chapter created successfully."
      redirect_to(:action => 'index')
    else
      render('new')
    end
  end

  private

  def chapter_params
    params.require(:chapter).permit(:name, :permalink, :subject_id, 
                                  :introduction, :free, :active, :position, :semester)
end

Form for new Chapter

  <%= simple_form_for(:chapter, :url => {:action => 'create'} ) do |f| %>

    <%= f.input :name %>
    <%= f.input :permalink} %>


    <%= f.association :subject %>


    <%= f.input :introduction %>
    <%= f.input :free, as: :radio_buttons%>
    <%= f.input :active, as: :radio_buttons %>
    <%= f.input :position %>
    <%= f.input :semester %>

    <%= f.button :submit, value: 'Save Chapter' %>

I get following error:

"Association cannot be used in forms not associated with an object."

Is there anything I need to add to my controller or model? When I don't use association and simply input the ID of subject, everything works.

nunopolonia

You need to add to the Chapter model the following code

accepts_nested_attributes_for :subject

Update

Since the Subject Model is the parent of the Chapter model, the solution described before won't work. accepts_nested_attributes_for only works in the "has_many" model and not in the "belongs_to" model. I'm leaving it here for reference.

In order to make the form builder know how about the association, you need to add to your controller "new" method the following code:

@chapter.build_subject

You will also need to change the simple_form_for call from:

simple_form_for(:chapter, :url => {:action => 'create'} ) do |f|

to:

simple_form_for @chapter do |f|

Because you need to pass the object you created to your form, and you're not doing that using a symbol in the simple_form_for call.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Error with simple_form on Rails

From Dev

Rails if statement in form with simple_form and HAML

From Dev

Rails: form to build deep associations

From Dev

Rails 4: form_for with associations

From Dev

Associations with simple_form an input field (not checkboxes, selects. or radios)

From Dev

Rails Simple_Form Association Not Working

From Dev

rails simple_form using virtual attributes

From Dev

Rails simple_form remember password input

From Dev

rails gem Simple_form installation is not working

From Dev

Rails 4 with Reform GEM and simple_form

From Dev

Rails Simple_Form Multiple Select Checkboxes

From Dev

ruby, simple_form Rails, collection

From Dev

Rails simple_form nested attributes

From Dev

simple_form validations with Bootstrap in Rails

From Dev

Associated model and simple_form in Rails 4

From Dev

Rails Simple_Form Multiple Select Checkboxes

From Dev

Rails simple_form: passing params on submit

From Dev

Rails 4, Bootstrap 3, simple_form - form styling not working

From Dev

Rails simple_form - Nested Form NoMethodError in "show" page

From Dev

rails : generate a form with simple_form without the design

From Dev

Rails simple_form - Nested Form NoMethodError in "show" page

From Dev

Rails polymorphic posts associations and form_for in views

From Dev

Rails Simple_Form Gem -remove required (*) prepend

From Dev

Nested Attributes for a Rich Join Table, using simple_form Rails

From Dev

Rails simple_form - Align radio buttons vertically

From Dev

How to put a glyphicon in a simple_form submit in Rails?

From Dev

Integrating hstore column with simple_form in Rails 4

From Dev

Rails Simple_Form Add Years to Date Field

From Dev

jQuery Tags-Input with Rails4 / Simple_form

Related Related

  1. 1

    Error with simple_form on Rails

  2. 2

    Rails if statement in form with simple_form and HAML

  3. 3

    Rails: form to build deep associations

  4. 4

    Rails 4: form_for with associations

  5. 5

    Associations with simple_form an input field (not checkboxes, selects. or radios)

  6. 6

    Rails Simple_Form Association Not Working

  7. 7

    rails simple_form using virtual attributes

  8. 8

    Rails simple_form remember password input

  9. 9

    rails gem Simple_form installation is not working

  10. 10

    Rails 4 with Reform GEM and simple_form

  11. 11

    Rails Simple_Form Multiple Select Checkboxes

  12. 12

    ruby, simple_form Rails, collection

  13. 13

    Rails simple_form nested attributes

  14. 14

    simple_form validations with Bootstrap in Rails

  15. 15

    Associated model and simple_form in Rails 4

  16. 16

    Rails Simple_Form Multiple Select Checkboxes

  17. 17

    Rails simple_form: passing params on submit

  18. 18

    Rails 4, Bootstrap 3, simple_form - form styling not working

  19. 19

    Rails simple_form - Nested Form NoMethodError in "show" page

  20. 20

    rails : generate a form with simple_form without the design

  21. 21

    Rails simple_form - Nested Form NoMethodError in "show" page

  22. 22

    Rails polymorphic posts associations and form_for in views

  23. 23

    Rails Simple_Form Gem -remove required (*) prepend

  24. 24

    Nested Attributes for a Rich Join Table, using simple_form Rails

  25. 25

    Rails simple_form - Align radio buttons vertically

  26. 26

    How to put a glyphicon in a simple_form submit in Rails?

  27. 27

    Integrating hstore column with simple_form in Rails 4

  28. 28

    Rails Simple_Form Add Years to Date Field

  29. 29

    jQuery Tags-Input with Rails4 / Simple_form

HotTag

Archive