Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

Ruby

Unrelated Models: Nested Model attributes not saving to database RAILS 4

Hi, I have been at this for a while. I have a Model form, call it Cover, with a nested Model, call it Logo, where the form accepts attributes for both models. I want to save their respective data to their models upon pressing the ssubmit button, but only the parent data gets saved.

Many online problems have an association of has_many and belongs_to, but this is not the case for these two, a Cover can has_and_belongs_to_many logos and Logo has_and_belongs_to_many covers. I think my problem is in the create method of cover_controllers, I do not know how to access the inner logo hash to save it to the database. Id appreciate tremendously any help!

The parameters lists a logo hash inside the cover hash but I havent been able to access it and pass it so that it can be saved to @logo and into the Logo db

I have tried def create @logo = Logo.new(:logo) @logo = Logo.new(@cover.logo) @logo = Logo.new(logo) @logo = Logo.new(logos_attributes)

UPDATE:

So I was able to use @logo = Logo.new(params[:logo]) and it did not throw an error, BUT the db is saving the data as nill into every attr of logo.

COVER VIEW <%= simple_form_for @cover, html: {class:'f'} do |f| %>

    <div id="cover">
        <%= render "cover", :locals => {:f => f}%>

<%=  f.simple_fields_for @logo do |logopart| %>

        <%= render "logo_fields", :iconpart=> iconpart%>

        <% end %>

COVER CONTROLLER '''def create @cover = Cover.new(cover_params) @logo = Logo.new(WHAT GOES HERE? I have tried params[:id] but Logo hates the attributes of Cover) if @Logo.save and @Cover.save redirect_to Covers_path write_json else render 'new' end ''' ''' def new

@cover = Cover.new
@logo = Logo.new 

end

def cover_params params.require(:cover).permit(:id, :the rest of cover's attr's, logos_attributes:[:id, etc..])

end'''

params {"utf8"=>"✓", "authenticity_token"=>"eEawInQkZwgmtptT+8FnsHw+14QkTS+69nLWReODMPS7axTpWG5yaJ8LJPSZ0/h/theI/ot9MH8nMOl0yNhS7A==", "cover"=>{"name"=>"9", etc. "logo"=>{"type"=>"9", "width"=>"9", "img_url"=>"9", "url"=>"9", "height"=>"9", "alt"=>""}}, "commit"=>"Create Title"}