0

I am new to Rails but I am stuck. I would like to create a form that will add data to two tables (books and books_authors). My view looks like:

<%= form_for :book, url: book_path do |f| %>
<p>
  <%= f.label :title %> :<br/>
  <%= f.text_field :title %><br/>
<ul>
  <% @authors.each do |x| %>
    <li> <%= f.check_box {...}  %> <%= x.name + " " + x.surname%></li>
  <% end %>
</ul>
</p>
<p><%= f.submit %></p>

and my create method in controller books_controller.rb looks like:

def create
  @book = Book.new(params.require(:book).permit(:title))
  @book.save
  params[:authors].each do |author_id|
    book.authors << Author.find(author_id)
  end
  redirect_to root_path
end

and my schema:

ActiveRecord::Schema.define(version: 20150709110928) do

create_table "author_books", id: false, force: :cascade do |t|
  t.integer  "author_id"
  t.integer  "book_id"
  t.datetime "created_at", null: false
  t.datetime "updated_at", null: false
end

add_index "author_books", ["author_id"], name:  
          "index_author_books_on_author_id"
add_index "author_books", ["book_id"],    
           name:"index_author_books_on_book_id"

create_table "authors", force: :cascade do |t|
  t.string   "name"
  t.string   "surname"
  t.datetime "created_at", null: false
  t.datetime "updated_at", null: false
end

create_table "books", force: :cascade do |t|
  t.string   "title"
  t.datetime "created_at", null: false
  t.datetime "updated_at", null: false
end

end

I need params for :authors but I dont know how to nest it inside the form

mike927
  • 682
  • 1
  • 8
  • 25

1 Answers1

4

I n your _form

<% @authors.each do |author| %>
  <%= check_box_tag "author_ids[]", author.id %>
  <%= author.name %> // I assume that your author has a name which can be used as a label
<% end %>

in your controller,

def create
  // you will get the authors in params[:author_ids]
  @book = Book.new(params.require(:book).permit(:title, :author_ids => [])) // its a dirty code
  // skip other codes
end

fresh code for controller

define a private method and put

def book_params
 params.require(:book).permit(:title, :author_ids => [])
end

and in

def create
  @book = Book.new(book_params)
end
Arslan Ali
  • 17,418
  • 8
  • 58
  • 76
Emu
  • 5,763
  • 3
  • 31
  • 51
  • I've just added my schema, you can look at that – mike927 Jul 10 '15 at 10:22
  • @mike927, as I can see from your question, you're facing trouble for getting the `authors` in controller. My answer is the solution for your trouble. Now use the `params[:author_ids]` to save. if you associated both the `Book` & `Author` model correctly then its easy to store the values. – Emu Jul 10 '15 at 10:27
  • authors_id filed in params doesn't have key - value structure. it stores only ids and looks like: "author_ids"=>["2", "3"], – mike927 Jul 10 '15 at 10:59
  • I got "undefined method `each' for nil:NilClass" error in my controller file – mike927 Jul 10 '15 at 11:01
  • @mike927, glad to hear that you figured out. Just close the question by accepting an answer. – Emu Jul 10 '15 at 11:10