So I have a nested resource with
resources :albums do
resources :elements
end
I can update, delete and view those elements. What I can not really do is to create a new element. So it is actually created in the database but not in the mapping table. Models:
class Album:
has_many :elements, :through => :albums_elements
has_many :albums_elements
class Element:
has_many :albums_elements
has_one :album, :through => :albums_elements
class AlbumsElement:
belongs_to :album
belongs_to :element
In the element Controller I have:
def create
@element = Element.new(params[:element])
@album = Album.find(params[:album_id])
respond_to do |format|
if @element.save
format.html { redirect_to album_elements_path(@album), :notice => 'Element was successfully created.' }
format.json { render :json => @element, :status => :created, :location => @element }
else
format.html { render :action => "new" }
format.json { render :json => @element.errors, :status => :unprocessable_entity }
end
end
end
So as I said, when I press the create button in the form, the element is being created correctly in the table "elements", but not inserted into "albums_elements". I saw a similar post here, where the author was told to fix his dependencies. But I don't see an error in mines? How do I tell rails to insert into both tables? elements AND albums_elements?