Rails Cocoon添加字段链接无法正常工作

时间:2016-11-20 17:30:22

标签: ruby-on-rails ruby-on-rails-4 devise cocoon-gem

我正在尝试从名为addresses的表中动态添加一个名为URL的字段,以便使用简单形式将表格称为essentials表格。我正在使用茧宝石。我也没有嵌套表单的经验。我当前的错误是未定义的方法`input':address:Symbol 。我正在使用设计,基础6,轨道4和简单的形式。

的application.js

//= require jquery
//= require jquery_ujs
//= require foundation
$(document).foundation();
//= require_tree .
//= require cocoon
//= require global
//= require jquery-ui
//= require turbolinks

global.coffee

jQuery(document).on 'turbolinks:load', ->
  addresses = $('#addresses')
  count = addresses.find('.count > span')

  recount = -> count.text addresses.find('.nested-fields').size()

  addresses.on 'cocoon:before-insert', (e, el_to_add) ->
    el_to_add.fadeIn(1000)

  addresses.on 'cocoon:after-insert', (e, added_el) ->
    added_el.effect('highlight', {}, 500)
    recount()

  addresses.on 'cocoon:before-remove', (e, el_to_remove) ->
    $(this).data('remove-timeout', 1000)
    el_to_remove.fadeOut(1000)

  addresses.on 'cocoon:after-remove', (e, removed_el) ->
    recount()

address.rb

class Address < ActiveRecord::Base
    belongs_to :essential
end

essentials.rb

class Address < ActiveRecord::Base
belongs_to :user
has_many :addresses

accepts_nested_attributes_for :addresses, :reject_if => :all_blank, :allow_destroy => true
end

user.rb

class User < ActiveRecord::Base
  # Include default devise modules. Others available are:
  # :confirmable, :lockable, :timeoutable and :omniauthable
  devise :database_authenticatable, :registerable,
         :recoverable, :rememberable, :trackable, :validatable
  has_many :essentials
  has_many :addresses
end

_address_fields.html.erb

<div class='fields'>
    <%= f.input :url %>
    <%= link_to_remove_association "remove address", f %>
</div>

_form.html.erb

<%= simple_form_for @essential do |f| %>
    <%= f.input :band_name, placeholder: "Band Name" %> 
    <div>
        <%= f.input :years_active_start, placeholder: "Start" %>  <%= f.input :years_active_end, placeholder: "End" %>
    </div>
    <%= f.input :country, placeholder: "country" %>
    <h6>Studio Albums</h6>
    <%= f.select :studio_albums, options_for_select([ "1","2","3","4","5","6","7","8","9","10","11","12" ]){}, {} %>
    <%= f.input :band_members, placeholder: "Band Members" %>
    <%= f.select :genre, options_for_select([ 
        "Art Punk",
        "Alternative Rock",
        "College Rock",
        "Crossover Thrash",
        "Crust Punk",
        "Experimental Rock",
        "Folk Punk",
        "Goth/Gothic Rock",
        "Grunge",
        "Hardcore Punk",
        "Hard Rock",
        "Indie Rock",
        "Lo-fi",
        "New Wave",
        "Progressive Rock",
        "Punk",
        "Shoegaze",
        "Steampunk",
        "Anime",
        "Blues",
        "Acoustic Blues",
        "Chicago Blues",
        "Classic Blues",
        "Contemporary Blues",
        "Country Blues",
        "Delta Blues",
        "Electric Blues",
        "Ragtime Blues",
        "Children’s Music",
        "Lullabies",
        "Sing-Along",
        "Stories",
        "Classical",
        "Avant-Garde",
        "Baroque",
        "Chamber Music",
        "Chant",
        "Choral",
        "Classical Crossover",
        "Contemporary Classical",
        "Early Music",
        "Expressionist", 
        "High Classical",
        "Impressionist",
        "Medieval",
        "Minimalism",
        "Modern Composition",
        "Opera",
        "Orchestral",
        "Renaissance",
        "Romantic", 
        "Wedding Music",
        "Comedy",
        "Novelty",
        "Standup Comedy",
        "Vaudeville",
        "Commercial", 
        "Jingles",
        "TV Themes",
        "Country",
        "Alternative Country",
        "Americana",
        "Bluegrass",
        "Contemporary Bluegrass",
        "Contemporary Country",
        "Country Gospel",
        "Country Pop", 
        "Honky Tonk",
        "Outlaw Country",
        "Traditional Bluegrass",
        "Traditional Country",
        "Urban Cowboy",
        "Dance", 
        "Club / Club Dance", 
        "Breakcore",
        "Breakbeat/Breakstep",
        "Brostep", 
        "Chillstep", 
        "Deep House", 
        "Dubstep",
        "Electro House", 
        "Electroswing",
        "Exercise",
        "Future Garage", 
        "Garage",
        "Glitch Hop", 
        "Glitch Pop", 
        "Grime", 
        "Hardcore",
        "Hard Dance",
        "Hi-NRG/Eurodance",
        "Horrorcore",
        "House",
        "Jackin House",
        "Jungle/Drum’n’bass",
        "Liquid Dub",
        "Regstep", 
        "Speedcore", 
        "Techno",
        "Trance",
        "Trap",
        "Disney",
        "Easy Listening",
        "Bop",
        "Lounge",
        "Swing",
        "Electronic",
        "2-Step",
        "8-bit", 
        "Ambient",
        "Bassline", 
        "Chillwave",
        "Chiptune", 
        "Crunk",
        "Downtempo",
        "Drum & Bass",
        "Electro",
        "Electro-swing",
        "Electronica",
        "Electronic Rock",
        "Hardstyle",
        "IDM/Experimental",
        "Industrial",
        "Trip Hop",
        "Enka",
        "French Pop",
        "German Folk",
        "German Pop",
        "Fitness & Workout",
        "Hip-Hop/Rap",
        "Alternative Rap",
        "Bounce",
        "Dirty South",
        "East Coast Rap",
        "Gangsta Rap",
        "Hardcore Rap",
        "Hip-Hop",
        "Latin Rap",
        "Old School Rap",
        "Rap",
        "Turntablism",
        "Underground Rap",
        "West Coast Rap",
        "Holiday",
        "Chanukah",
        "Christmas",
        "Christmas: Children’s",
        "Christmas: Classic",
        "Christmas: Classical",
        "Christmas: Comedy",
        "Christmas: Jazz",
        "Christmas: Modern",
        "Christmas: Pop",
        "Christmas: R&B",
        "Christmas: Religious",
        "Christmas: Rock",
        "Easter",
        "Halloween",
        "Holiday: Other",
        "Thanksgiving",
        "Indie Pop",
        "Industrial",
        "Inspirational",
        "CCM",
        "Christian Metal",
        "Christian Pop",
        "Christian Rap",
        "Christian Rock",
        "Classic Christian",
        "Contemporary Gospel",
        "Gospel",
        "Christian & Gospel",
        "Praise & Worship",
        "Qawwali",
        "Southern Gospel",
        "Traditional Gospel",
        "Instrumental",
        "March",
        "J-Pop",
        "J-Rock",
        "J-Synth",
        "J-Ska",
        "J-Punk",
        "Jazz",
        "Acid Jazz",
        "Avant-Garde Jazz",
        "Bebop",
        "Big Band",
        "Blue Note",
        "Contemporary Jazz",
        "Cool",
        "Crossover Jazz",
        "Dixieland",
        "Ethio-jazz",
        "Fusion",
        "Gypsy Jazz",
        "Hard Bop",
        "Latin Jazz",
        "Mainstream Jazz",
        "Ragtime",
        "Smooth Jazz",
        "Trad Jazz",
        "K-Pop",
        "Karaoke",
        "Kayokyoku",
        "Latin",
        "Alternativo & Rock Latino",
        "Argentine tango",
        "Baladas y Boleros",
        "Bossa Nova",
        "Brazilian",
        "Contemporary Latin",
        "Cumbia",
        "Flamenco / Spanish Flamenco",
        "Latin Jazz",
        "Nuevo Flamenco",
        "Pop Latino",
        "Portuguese fado",
        "Raíces",
        "Reggaeton y Hip-Hop",
        "Regional Mexicano",
        "Salsa y Tropical",
        "New Age",
        "Environmental",
        "Healing",
        "Meditation",
        "Nature",
        "Relaxation",
        "Travel",
        "Opera",
        "Pop",
        "Adult Contemporary",
        "Britpop",
        "Bubblegum Pop",
        "Chamber Pop",
        "Dance Pop",
        "Dream Pop", 
        "Electro Pop",
        "Orchestral Pop",
        "Pop/Rock",
        "Pop Punk",
        "Power Pop",
        "Soft Rock",
        "Synthpop",
        "Teen Pop",
        "R&B/Soul",
        "Contemporary R&B",
        "Disco",
        "Doo Wop",
        "Funk",
        "Modern Soul",
        "Motown",
        "Neo-Soul",
        "Northern Soul",
        "Psychedelic Soul",
        "Quiet Storm",
        "Soul",
        "Soul Blues",
        "Southern Soul",
        "Reggae",
        "2-Tone",
        "Dancehall",
        "Dub",
        "Roots Reggae",
        "Ska",
        "Rock",
        "Acid Rock",
        "Adult-Oriented Rock",
        "Afro Punk",
        "Adult Alternative",
        "Alternative Rock",
        "American Trad Rock",
        "Anatolian Rock",
        "Arena Rock",
        "Art Rock",
        "Blues-Rock",
        "British Invasion",
        "Cock Rock",
        "Death Metal / Black Metal",
        "Doom Metal",
        "Glam Rock",
        "Gothic Metal",
        "Grind Core",
        "Hair Metal",
        "Hard Rock",
        "Math Metal",
        "Math Rock",
        "Metal",
        "Metal Core",
        "Noise Rock",
        "Jam Bands",
        "Post Punk",
        "Prog-Rock/Art Rock",
        "Progressive Metal",
        "Psychedelic",
        "Rock & Roll",
        "Rockabilly",
        "Roots Rock",
        "Singer/Songwriter",
        "Southern Rock",
        "Spazzcore",
        "Stoner Metal",
        "Surf",
        "Technical Death Metal",
        "Tex-Mex",
        "Time Lord Rock (Trock)",
        "Trash Metal",
        "Singer/Songwriter",
        "Alternative Folk",
        "Contemporary Folk",
        "Contemporary Singer/Songwriter",
        "Indie Folk",
        "Folk-Rock",
        "Love Song",
        "New Acoustic",
        "Traditional Folk",
        "Soundtrack",
        "Foreign Cinema",
        "Movie Soundtrack",
        "Musicals",
        "Original Score",
        "Soundtrack",
        "TV Soundtrack",
        "Spoken Word",
        "Tex-Mex / Tejano",
        "Chicano",
        "Classic",
        "Conjunto",
        "Conjunto Progressive",
        "New Mex",
        "Tex-Mex",
        "Vocal",
        "A cappella",
        "Barbershop",
        "Doo-wop",
        "Gregorian Chant",
        "Standards",
        "Traditional Pop",
        "Vocal Jazz",
        "Vocal Pop",
        "World",
        "Africa",
        "Afro-Beat",
        "Afro-Pop",
        "Asia",
        "Australia",
        "Cajun",
        "Calypso",
        "Caribbean",
        "Carnatic",
        "Celtic",
        "Celtic Folk",
        "Contemporary Celtic",
        "Coupé-décalé",
        "Dangdut",
        "Drinking Songs",
        "Drone",
        "Europe",
        "France",
        "Hawaii",
        "Hindustani",
        "Indian Ghazal",
        "Indian Pop",
        "Japan",
        "Japanese Pop",
        "Klezmer",
        "Mbalax",
        "Middle East",
        "North America",
        "Ode",
        "Piphat",
        "Polka",
        "Soca",
        "South",
        "South America",
        "Traditional Celtic",
        "Worldbeat",
        "Zydeco",
    ]){}, {} %>
    <h6>Description</h6>
    <%= f.text_area :description, :input_html => { class: 'test', :rows => 50 } %>
    <%= f.hidden_field :user_id %>

    <div id="addresses">
      <%= f.simple_fields_for :addresses do |address| %>
        <%= render 'address_fields', :f => address %>
      <% end %>

    <div class="links">
      <%= link_to_add_association 'add address', f, :addresses %>
    </div>
    </div>

</div>
 <% f.button :submit %>
<% end %>

essentials_controller.rb

class EssentialsController < ApplicationController


  # GET /essentials
  # GET /essentials.json
  def index
   @user_id = current_user.id
  @essentials = Essential.all

    respond_to do |format|
      format.html # index.html.erb
      format.xml  { render :xml => @essentials }
    end
  end

  # GET /essentials/1
  # GET /essentials/1.json
  def show
    @essential = Essential.find(params[:id])


  end

  # GET /essentials/new
  def new

    @essential = Essential.new
    @essential.addresses.build
 #  3.times { @essential.addresses.build}




  end

  # GET /essentials/1/edit
  def edit

      @essential = Essential.find_by(id: params[:id])
  end

  # POST /essentials
  # POST /essentials.json
  def create

      @essential = Essential.new(essential_params)
        if @essential.save
          redirect_to root_path
        else
          render :new
        end
     @essential.user_id = current_user.id
     @essential.save

  end

  # PATCH/PUT /essentials/1
  # PATCH/PUT /essentials/1.json
  def update

s
  end

  # DELETE /essentials/1
  # DELETE /essentials/1.json
  def destroy
    @essential = Essential.find(params[:id])
    if @essential.present?
      @essential.destroy
     redirect_to essentials_path, notice: "Post was deleted"

  end

  end

  private
    # Use callbacks to share common setup or constraints between actions.
    def set_essential
      @essential = Essential.find(params[:id])
    end


    # Never trust parameters from the scary internet, only allow the white list through.

    def essential_params
      params.require(:essential).permit(
      :band_name, :years_active_start, :country, :studio_albums, :band_members, :genre, :description, :user_id, :years_active_end, addresses_attributes: [:id, :_destroy, :url, :essential_id])
    end
end

schema.rb

ActiveRecord::Schema.define(version: 20161120025634) do

  create_table "addresses", force: :cascade do |t|
    t.datetime "created_at",                 null: false
    t.datetime "updated_at",                 null: false
    t.text     "url",          limit: 65535
    t.integer  "essential_id", limit: 4
  end

  create_table "essentials", force: :cascade do |t|
    t.datetime "created_at",                       null: false
    t.datetime "updated_at",                       null: false
    t.string   "band_members",       limit: 255
    t.integer  "years_active_end",   limit: 4
    t.integer  "years_active_start", limit: 4
    t.string   "band_name",          limit: 255
    t.string   "country",            limit: 255
    t.string   "studio_albums",      limit: 255
    t.string   "genre",              limit: 255
    t.integer  "user_id",            limit: 4
    t.text     "description",        limit: 65535
  end

  create_table "homes", force: :cascade do |t|
    t.string   "index",      limit: 255
    t.datetime "created_at",             null: false
    t.datetime "updated_at",             null: false
  end

  create_table "users", force: :cascade do |t|
    t.string   "email",                  limit: 255, default: "", null: false
    t.string   "encrypted_password",     limit: 255, default: "", null: false
    t.string   "reset_password_token",   limit: 255
    t.datetime "reset_password_sent_at"
    t.datetime "remember_created_at"
    t.integer  "sign_in_count",          limit: 4,   default: 0,  null: false
    t.datetime "current_sign_in_at"
    t.datetime "last_sign_in_at"
    t.string   "current_sign_in_ip",     limit: 255
    t.string   "last_sign_in_ip",        limit: 255
    t.datetime "created_at",                                      null: false
    t.datetime "updated_at",                                      null: false
    t.string   "first_name",             limit: 255
    t.string   "last_name",              limit: 255
  end

  add_index "users", ["email"], name: "index_users_on_email", unique: true, using: :btree
  add_index "users", ["reset_password_token"], name: "index_users_on_reset_password_token", unique: true, using: :btree

end

1 个答案:

答案 0 :(得分:0)

你写

<%= render 'address_fields', :f => :address %>

它应该只是

<%= render 'address_fields', :f => address %>

换句话说:使用给定周围块的变量(不是恰好具有相同名称的符号)。