ruby on rails 4 - has_many :through association and form_for -
ruby on rails 4 - has_many :through association and form_for -
i trying first time build form including has_many through association these 3 models:
class book < activerecord::base has_many :talks has_many :authors, through: :talks accepts_nested_attributes_for :talks accepts_nested_attributes_for :authors validates :title, presence: true end class author < activerecord::base has_many :talks has_many :books, through: :talks validates :l_name ,presence: true end class talk < activerecord::base belongs_to :book belongs_to :author accepts_nested_attributes_for :author end
my book/new.html.erb
<h1>add new book</h1> <%= form_for (@book) |f| %> <div> <%= f.label :title %><br> <%= f.text_field :title %> </div> <div> <%= f.fields_for :talks, talk.new |t| %> <div> <%t.fields_for :author, author.new |a| %> <%= a.label :f_name %> <%= a.collection_select :f_name, @authors, :f_name, :f_name %> <%end %> </div> <% end%> </div> <div> <%= f.submit %> </div> <%end%>
my books_controller.rb
def new @book =book.new end def create @book= book.new(book_params) if @book.save flash[:notice]='goood' redirect_to admin_manage_path else flash[:alert]='ouups' redirect_to root_url end private def book_params params.require(:book).permit(:title, :pages, :resume, authors_attributes: [talks_attributes: []]) end end
i got error:
undefined method `map' nil:nilclass
and tried follow post: http://howilearnedrails.wordpress.com/2013/12/18/rails-4-dropdown-menu-with-collection_select/
do add together below snipped under 'new' action of 'books_controller.rb'
@authors = author.all
ruby-on-rails-4 nested-form-for
Comments
Post a Comment