未允许的参数:songs_attributes

时间:2016-12-31 20:33:40

标签: javascript ruby-on-rails coffeescript

我的嵌套属性有问题。我希望有人能够访问我的网站并为活动添加歌曲。如果他们为该事件提供派对代码,他们只能添加这些歌曲。我已经正确设置了嵌套属性,但我得到了一个不允许的参数错误。

事件控制器:

class EventsController < ApplicationController
    def new
        @event = Event.new
        @event.songs.build
    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, :partycode])
      end
end

application_controller:

class ApplicationController < ActionController::Base
  protect_from_forgery with: :exception
  include SessionsHelper
end

以下是歌曲视图中的new.html.erb文件,其中包含向活动添加歌曲的代码,请注意用户在添加歌曲时未登录:

<br>
<br>
<div class ="container">
  <div class="jumbotron">
  <%= form_for Event.new do |f| %>
    <h3>Enter a song:</h3>
    <%= f.fields_for :songs, Song.new do |song_form| %>

      <%= song_form.collection_select(:event_id, Event.all, :id, :name) %>
      <%= song_form.text_field :artist, placeholder: "Artist" %>
      <%= song_form.text_field :title,  placeholder: "Title" %>
      <%= song_form.text_field :genre,  placeholder: "Genre" %>
    <% end %>
    <%= link_to_add_fields "Add Song", f, :songs %>
    <%= f.text_field :partycode %>
    <%= f.submit "Submit", class: "btn btn-primary" %>
  <% end %>
  </div>
</div>

link_to_add_fields方法位于application_helper.rb文件中:

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("songs_fields", f: builder)
    end
    link_to(name, '#', class: "add_fields", data: {id: id, fields: fields.gsub("\n", "")})
  end
end

songs_field partial定义如下:

<fieldset>  
  <%= f.text_field :artist, placeholder: "Artist" %>
  <%= f.text_field :title,  placeholder: "Title" %>
  <%= f.text_field :genre,  placeholder: "Genre" %>
</fieldset>

添加字段的咖啡脚本:

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

来自new.html.erb页面的人将能够从下拉菜单中选择一个事件,然后根据需要添加更多歌曲的字段,然后输入他们选择的特定事件的聚会代码。对此有任何帮助都太棒了!感谢

编辑错误:

undefined method `events' for nil:NilClass

1 个答案:

答案 0 :(得分:1)

在您的EventsController中,您的嵌套参数拼写为song_attributes方法中的event_params

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

但您的错误是unpermitted parameter: songs_attributes

您拼写错误。在event_params中更改EventsController方法,如下所示

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