Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6248339
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T13:02:22+00:00 2026-05-24T13:02:22+00:00

I’m new to RoR and having an issue when trying to save from multiple

  • 0

I’m new to RoR and having an issue when trying to save from multiple dropdowns. I have three objects – books, genres, and authors. A book object has a genre and author associated to it, but the issue is I can only manage to save either a genre or author to my book object, and not both. Here’s where I’m at:

class Author < ActiveRecord::Base
validates :name,  :presence => true
validates :biography,  :presence => true
has_many :books
end

class Genre < ActiveRecord::Base
validates :description,  :presence => true
has_many :books
end

class Book < ActiveRecord::Base
 belongs_to :genre
 belongs_to :author
 has_many :cartitems

validates :name,  :presence => true
validates :price,  :presence => true
validates :description,  :presence => true

end

Controller:

def create
#@book = Book.new(params[:book])
@author = Author.find(params[:author].values[0])
@genre = Genre.find(params[:genre].values[0])
@book = @author.books.create(params[:book])
#one or the other saves, but not both
#@book = @genre.books.create(params[:book])
respond_to do |format|
  if @book.save
    format.html { redirect_to(@book, :notice => 'Book was successfully created.') }
    format.xml  { render :xml => @book, :status => :created, :location => @book }
  else
    format.html { render :action => "new" }
    format.xml  { render :xml => @book.errors, :status => :unprocessable_entity }
  end
end

end

Not sure if this will help out or not, but here’s what the dropdowns look like in the View:

<div class="field">
<%= f.label :genre %><br />
<%= @items = Genre.find(:all)
select("genre", "description",  @items.map {|u| [u.description,u.id]}, {:include_blank => true})%>

Appreciate any help with this.

EDIT – Here’s my full form.

    <%= form_for(@book) do |f| %>
  <% if @book.errors.any? %>
    <div id="error_explanation">
      <h2><%= pluralize(@book.errors.count, "error") %> prohibited this book from being saved:</h2>

      <ul>
      <% @book.errors.full_messages.each do |msg| %>
        <li><%= msg %></li>
      <% end %>
      </ul>
    </div>
  <% end %>

  <div class="field">
    <%= f.label :name %><br />
    <%= f.text_field :name %>
  </div>
  <div class="field">
    <%= f.label :price %><br />
    <%= f.text_field :price %>
  </div>
  <div class="field">
    <%= f.label :description %><br />
    <%= f.text_area :description %>
  </div>
  <div class="field">
    <%= f.label :genre %><br />
    <%= @items = Genre.find(:all)
    select("genre", "description",  @items.map {|u| [u.description,u.id]}, {:include_blank => true})%>
  </div>
  <div class="field">
    <%= f.label :author %><br />
    <%=@items = Author.find(:all)
    select("author", "name",  @items.map {|u| [u.name,u.id]}, {:include_blank => true}) %>
  </div>
  <div class="actions">
    <%= f.submit %>
  </div>
<% end %>
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-24T13:02:23+00:00Added an answer on May 24, 2026 at 1:02 pm

    Update your select fields to be defined like this:

    <div class="field">
      <%= f.label :genre %><br />
      <%= f.select( :genre_id,  Genre.all.map {|u| [u.description,u.id]}, {:include_blank => true}) %>
    </div>
    <div class="field">
      <%= f.label :author %><br />
      <%= f.select( :author_id, Author.all.map {|u| [u.name,u.id]}, {:include_blank => true}) %>
    </div>
    

    And your controller action should be like this:

    def create
      @book = Book.new(params[:book])
      respond_to do |format|
        if @book.save
          format.html { redirect_to(@book, :notice => 'Book was successfully created.') }
          format.xml  { render :xml => @book, :status => :created, :location => @book }
        else
          format.html { render :action => "new" }
          format.xml  { render :xml => @book.errors, :status => :unprocessable_entity }
        end
    end
    

    Also, remove the format.xml calls if you don’t need them, they’re just cluttering your controller action.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

link Im having trouble converting the html entites into html characters, (&# 8217;) i
I'm trying to decode HTML entries from here NYTimes.com and I cannot figure out
I have a bunch of posts stored in text files formatted in yaml/textile (from
I am trying to loop through a bunch of documents I have to put
I want use html5's new tag to play a wav file (currently only supported
I'm new to using the Perl treebuilder module for HTML parsing and can't figure
I have a jquery bug and I've been looking for hours now, I can't
Does anyone know how can I replace this 2 symbol below from the string
this is what i have right now Drawing an RSS feed into the php,
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.