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

  • Home
  • SEARCH
  • 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 8774375
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T18:33:06+00:00 2026-06-13T18:33:06+00:00

I’m trying to learn Rails by creating a very simple application which just creates

  • 0

I’m trying to learn Rails by creating a very simple application which just creates a website where someone can create a list of authors and books with an association that the book is written by an author. I was hoping this would be simple and DRY, but I’ve been having an unexpected amount of trouble with it.

First looking at my models, I’ve set up the association and made every data point required (author.name, book.title, and book.author). I do not want to add :author or :author_id to the attr_accessible lists because I want to use the appropriate Rails conventions.

app/models/author.rb:

class Author < ActiveRecord::Base
  attr_accessible :name
  validates_presence_of :name
  has_many :books
end

app/models/book.rb:

class Book < ActiveRecord::Base
  attr_accessible :title
  validates_presence_of :title
  belongs_to :author
  validates_associated :author
end

The books controller and view I think is exactly from the scaffolding and very uninteresting. What is interesting is the books controller. Looking at the new method, all I did was add a collector which gets the array of author names with ids to pass to the view. (Honestly, I think I would prefer to not pass the id at all.)

app/controllers/books_controller.rb

  # GET /books/new
  # GET /books/new.json
  def new
    @book = Book.new
    @authors = Author.all.collect {|a| [a.name, a.id]}

    respond_to do |format|
      format.html # new.html.erb
      format.json { render json: @book }
    end
  end

Now over to the views, I used the default new.html.haml, but made changes to _form.html.haml. I added a select field using the values in @authors.

app/views/books/_form.html.haml

= form_for @book do |f|
  - if @book.errors.any?
    #error_explanation
      %h2= "#{pluralize(@book.errors.count, "error")} prohibited this book from being saved:"
      %ul
        - @book.errors.full_messages.each do |msg|
          %li= msg

  .field
    = f.label :name
    = f.text_field :name
  .field
    = f.label :author
    = f.select(:author, @authors, {:include_blank => ""})
 .actions
    = f.submit 'Save'

Lastly, back to my controller for the create method. I try to save the basic parameters and create an author association from the selected author.

app/controllers/books_controller.rb

  # POST /books
  # POST /books.json
  def create
    @book = Book.new(params[:book])
    @book.author = Author.find_by_id(params[:author])

    respond_to do |format|
      if @book.save
        format.html { redirect_to @book, notice: 'Book was successfully created.' }
        format.json { render json: @book, status: :created, location: @book }
      else
        format.html { render action: "new" }
        format.json { render json: @book.errors, status: :unprocessable_entity }
      end
    end
  end

When I click ‘Save’ I get the following error:

Can't mass-assign protected attributes: author

I understand that this is because the value I selected was put in params[:book] instead of params[:author]. So I have two questions.

1) How do I fix my select statement to have it send in params[:author] instead of params[:book]?

2) Is there an even better way to do this that completely hides the id association?

  • 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-06-13T18:33:07+00:00Added an answer on June 13, 2026 at 6:33 pm

    I think I’ve mostly figured it out, and as I suspected, I didn’t need to change my model code at all.

    I changed my controllers new method definition of @authors to only return author names with the following:

    @authors = Author.pluck(:name)
    

    This accomplished my goal of hiding the id though it’s probably a tad slower when I need to search by name instead of id in the controller create method (below).

    Next, I fixed my views to set params[:author] instead of params[:book][:author].

    = label :author, :name, 'Author'
    = select_tag(:author, options_for_select(@authors), {:include_blank => ""})
    

    Finally, I changed the new methods creation of the @book:

    @book = Author.find_by_name(params[:author]).books.create(params[:book])
    

    I’m fairly happy with this. The only thing I don’t really like is that the label creates an “author_name” label instead of simply “author”.

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

Sidebar

Related Questions

I am trying to understand how to use SyndicationItem to display feed which is
Basically, what I'm trying to create is a page of div tags, each has
I have just tried to save a simple *.rtf file with some websites and
I used javascript for loading a picture on my website depending on which small
I'm trying to select an H1 element which is the second-child in its group
I'm trying to create an if statement in PHP that prevents a single post
I'm new to using the Perl treebuilder module for HTML parsing and can't figure
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I am trying to render a haml file in a javascript response like so:

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.