jQuery input_token + Rails4 +简单形式的gem

时间:2015-06-07 03:25:58

标签: javascript jquery ruby-on-rails ruby ruby-on-rails-3

我有2个模型sprofileskills

sprofile.rb

has_and_belongs_to_many :skills ,dependent: :destroy

def skill_tokens=(tokens)
    self.skill_ids  = Skill.ids_from_tokens(tokens)         
end

sprofiles_controller.rb

def sprofile_params
  params.require(:sprofile).permit(:first_name, :last_name,{:skill_ids => []})
end

skill.rb

class Skill < ActiveRecord::Base
 has_and_belongs_to_many :sprofiles


 def self.tokens(query)

    skills = where("name like ?" , "%#{query}%")
    if skills.empty?
        [{id: "<<<#{query}>>>" , name: "New: \"#{query}\""}]
    else
        skills
    end
 end

 def self.ids_from_tokens(tokens)
    tokens.gsub!(/<<<(.+?)>>>/) { create!(name: $1).id  }
    tokens.split(',')
 end
end

skills_controller.rb

class SkillsController < ApplicationController
 def index
    @skills = Skill.order(:name)
    respond_to do |format|
    format.html
    format.json {render json: @skills.tokens(params[:q])}
    end
 end

 def show
    @skill = Skill.find_by(:id)
 end
end

application.js

//= require jquery
//= require jquery_ujs
//= require bootstrap-sprockets
//= require jquery.purr
//= require jquery.tokeninput
//= require jquery-ui
//= require autocomplete-rails
//= require jquery.raty
//= require ratyrate
//= require best_in_place
//= require turbolinks
//= require_tree .

sprofile views

<%= simple_form_for(@sprofile) do |f| %>
 <%= f.collection_select :level_id, Level.all,  :id, :name , {prompt: "choose a level"} %>
 <%= f.input :document, as: :file %>
 <%= f.input :avatar, as: :file %>
 <%= f.input :contact_no , :placeholder => "123-456-7899" %>

 <%= f.association :skills, input_html: { class: 'token' }%>
     <div class="form-actions">
 <%= f.button :submit %>
  </div>
 <% end %>
sprofiles.js.coffee

中的

jQuery ->
   $('.token').tokenInput "/skills.json"

工作正常,但不会将数据保存到skills_ids

Logs

 Parameters: {"utf8"=>"✓", "authenticity_token"=>"WovPcbFdJ7e2755nwapZCG9gPXcBIouInBp6XwYDQio=", "sprofile"=>{"level_id"=>"1", "contact_no"=>"484-767-9853", "skill_ids"=>[""]}, "commit"=>"Update Sprofile", "id"=>"1"}

0 个答案:

没有答案