javascript - 不允许的参数 : songs_attributes

标签 javascript ruby-on-rails coffeescript

我的嵌套属性有问题。我希望有人能够访问我的网站并向事件添加歌曲。他们只能在为该事件提供派对代码的情况下添加这些歌曲。我认为我的嵌套属性设置正确,但我收到了未经允许的参数错误。

事件 Controller :

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

应用程序 Controller :

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

这里是歌曲 View 中的 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 部分定义如下:

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

添加字段的 CoffeeScript :

$(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

最佳答案

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

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

但是您的错误显示未经允许的参数:song_attributes

您拼写错误。按如下方式更改 EventsController 中的 event_params 方法

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

关于javascript - 不允许的参数 : songs_attributes,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41411536/

相关文章:

javascript - Box2D 引擎 javascript 端口 Prismatic Joint 的奇怪行为

ruby-on-rails - 在给定半径内查找附近的位置

javascript - Coffeescript 类 mixin 不适用于构造函数

coffeescript - Coffeescript 中的静态方法和继承

javascript - 通过 SSL 从 Google/Microsoft CDN 获取 javascript 库

javascript - jQuery .html() 没有正确设置内容?

javascript - 使用 iimPlay 时 iMacro/JS RuntimeError 933

ruby-on-rails - rails 字段上的唯一性约束 ruby

ruby-on-rails - Ruby On Rails - 无法通过报复在 has_many 中执行 where 语句

javascript - Facebook React 根据 url hash 渲染不同的组件