NoMethod error fields Nested Attributes

28 views Asked by At

I've been following this railscast: https://www.youtube.com/watch?v=t_FNKR7jahM for learning how to implement nested attributes into my application. When I implement his code in the certain areas necessary I always get the same NoMethod error on the fields. enter image description here

On the events show page where you can add songs, I am trying to add a 'Add Field' link that will add another group of songs(a field for artist, title, and genre).

Here is my EventController:

class EventsController < ApplicationController

    def new
        @event = Event.new
        @event.songs.build
    end

    def index
        @songs = Song.all
    end

    def show
      @event = Event.find(params[:id])
      @songs = @event.songs.paginate(page: params[:page])
    end

    def create
        @event = current_user.events.build(event_params)
        if @event.save
            flash[:success] = "Event Created!"
            redirect_to user_path(@event.user)
        else
            render 'welcome#index'
        end
    end

    def destroy
    end

    private 

      def event_params
        params.require(:event).permit(:name, :partycode, song_attributes: [:artist, :title, :genre])
      end
end

Here is my Event model:

class Event < ApplicationRecord
  belongs_to :user
  has_many :songs, dependent: :destroy  
  accepts_nested_attributes_for :songs, allow_destroy: true

  validates :name, presence: true
  validates :partycode, presence: true, length: {minimum: 5}
end

Here is the show page in which the songs are added:

<section class="song_form">
      <%= render 'shared/song_form' %>
      <%= form_for @event do |f| %>
        <%= f.fields_for :fields do |builder| %>
          <%= render 'events/songs_fields', f: builder %>
        <% end %>
        <%= link_to_add_fields "Add Field", f, :fields %>
      <% end %>
</section>

Here is the song_fields file:

<fieldset>
  <%= f.select :field_type, %w[text_field check_box] %>
  <%= f.text_field :artist, placeholder: "Artist" %>
</fieldset>

Here is the ApplicationHelper file :

module ApplicationHelper
  def link_to_add_fields(name, f, association)
    new_object = f.object.send(association).klass.new
    id = new_object.object_id
    fields = f.fields_for(association, new_object, child_index: id) do |builder|
      render(association.to_s.singularize + "_fields", f: builder)
    end
    link_to(name, '#', class: "add_fields", data: {id: id, fields: fields.gsub("\n", "")})
  end
end

Finally, here is my events coffee script file;

$(document).on 'click', 'form .add_fields', (event) ->
  time = new Date().getTime()
  regexp = new RegExp($(this).data('id'), 'g')
  $(this).before($(this).data('fields').replace(regexp, time))
  event.preventDefault()

Sorry for the lengthy post, an answer to my question would be GREATLY appreciated. Let me know if there is anything else you need from me(note I am using rails 5). Cheers :)

1

There are 1 answers

6
Eyeslandic On BEST ANSWER

Should be

f.fields_for :songs do |builder|