jeudi 1 septembre 2016

Rails - f.collection_select showing but not selecting the value

Hello StackOverflow Users, I am currently creating a search form,
and I would like to have a collection_select with the list of books available.

Users got books and an Join table is linking them

2 models : User / Book
A join table : Book_User and a has many relationship for both sides

User Model :

has_many :games, :through => :book_users
  has_many :book_users
  accepts_nested_attributes_for :books

Book Model has_many :users, :through => :book_users has_many :book_users accepts_nested_attributes_for :users

The form is working with a standard field the Text_field_tag and the results are correct (I need the book_name to make it work) :

<%= form_tag search_path, :class => "webdesigntuts-workshop", method: :get do %>
  .... 
 <%= label :book_name, "" %>
      <%# text_field_tag :book_name, params[:book_name], placeholder: 'books' %>
      <%= collection_select(:book, :book_name, Book.all, :id, :name, prompt: true) %>
      <%= submit_tag "Search" %>
<% end %>

The collection_Select is displaying the list of the books but when I select the name of the book , and click on the submit button ,the book is not taken into account in the request. I tried the option :selected => params[book_name] as well.

Is the collection select correctly written ?

I think i should do a f.collection_select , im in a form but i struggle with the attributes. I hope somenone could help me on that part :)

Many thanks in advance,

Martin

Aucun commentaire:

Enregistrer un commentaire