Create and edit with has_and_belongs_to_many

Hi you all,

I have the common model of “pages” and “subjects”, where a “page” can
have many “subjects” and a “subject” can be related with many “pages”,
so I set the “has_and_belongs_to_many” method in both models, that’s
all.

In SQL, abbreviated, I have:
CREATE TABLE pages (
id int(10) unsigned

CREATE TABLE subjects (
id smallint(5) unsigned NOT NULL auto_increment,
subject varchar(20) NOT NULL,

CREATE TABLE pages_subjects (
page_id int not null,
subject_id int not null,

In the views (_form.rhtml), when creating or editing a page, I have the
following text box:
Subjects

<%= text_field ‘page’, ‘subjects’ %>

Where I will include several subjects but, just to try, now I just write
one, (the only one I know is on the data base), and the following error
appears when accept:
ActiveRecord::AssociationTypeMismatch in WikiController#create
Subject expected, got String

So, what am I doing wrong, or not doing? Should I specify more things in
the controller when creating or editing this, or in the view?

Thank you a lot.

What is your controller code that processes the form and the response
from the text field?

Damaris F. wrote:

subject_id int not null,
Okay, so here we have a field called ‘subject’ in the ‘subjects’
table…

In the views (_form.rhtml), when creating or editing a page, I have the
following text box:
Subjects

<%= text_field ‘page’, ‘subjects’ %>

And what you have asked for here is a text field for the page object.
The value of which is to be saved into page.subjects. If you look at
the HTML source rails will render you will see the field is called
“page[subjects]”. If you simply go page.create(params[:page]) in your
controller it is going to try and place the value of that text field
(which will always be a string) into page.subjects which rails expects
to be a collection of Subject objects. The string value actually needs
to be added to a new Subject object, and that object then added to the
page.subjects collection. So maybe something like:

<%= text_field_tag “subject”, “subject” %> or <%= text_field_tag
“subject[subject]” %>

and then in the controller:

@subject.create(params[:subject])
@page.create(params[:page])
@page.subjects << @subject

I’ll leave you to work out a more elegant solution to preventing
duplicate subjects from being created, but hopefully that explains what
is happening for you.

<%= text_field_tag “subject”, “subject” %> or <%= text_field_tag
“subject[subject]” %>

and then in the controller:

@subject.create(params[:subject])
@page.create(params[:page])
@page.subjects << @subject

With the recommendation aobve, I wrote this code
def create
@page = Page.new_page(@params[:id])
if @request.post? and and @page.save
@subject = Subject.new(params[:subject])
@subject.save
@page.subjects << @subject
flash[:notice] = ‘Page was successfully created.’
redirect_to :action => ‘show’, :id => @page.title
end
end
And it worked…THANKS!

However, I would like to reuse somehow the “create” method I have in the
SubjectsController:
def create
@subject = Subject.new(params[:subject])
if @subject.save
flash[:notice] = ‘Subject was successfully created.’
redirect_to :action => ‘list’
else
render :action => ‘new’
end
end

So as not to write “subject.new…”, “subject.save…” in the “create” of
the PagesController (there I could do the validations and all the
stuff).

Any ideas? (I’m still a new new newbie :frowning: )

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs