How to Save User ID With Rails & JQuery Tokeninput? -
i following along this railscast jquery tokeninput , working. problem need figure out how save user id category upon creation. how can save within code?
model
class category < activerecord::base belongs_to :user has_and_belongs_to_many :entries validates :name, :user_id, presence: true def self.tokens(query) categories = where("name ?", "%#{query}%") if categories.empty? [{id: "<<<#{query}>>>", name: "new: \"#{query}\""}] else categories end end def self.ids_from_tokens(tokens) tokens.gsub!(/<<<(.+?)>>>/) { create!(name: $1).id } tokens.split(',') end end
controller
def index @categories = category.order(:name) respond_to |format| format.html format.json { render json: @categories.tokens(params[:q]) } end end
as talked about, earlier. can use http://rails-bestpractices.com/posts/47-fetch-current-user-in-models access current_user inside of model.
Comments
Post a Comment