sql - Rails : Using jquery tokeninput (railscast #258) to create new entries -



sql - Rails : Using jquery tokeninput (railscast #258) to create new entries -

i have been able implement add-on of new artist entries not included in ryan bates railscast #258 http://railscasts.com/episodes/258-token-fields

so in other words, user can come in artist name autocomplete using jquery tokinput. however, i'd autocomplete results display artist names created individual user.

does create sense? improve , more understandable illustration collection.rb, users create posts , specify 'collection' post belong to, should able add together posts collections created themselves.

this post form artist_tokens virtual attribute:

<%= form_for @post, :validate => true, :html => {:multipart => true} |f| %> <%= render 'shared/error_messages', :object => f.object %> <div class="field"> <%= f.label :title, 'title:' %><br /> <%= f.text_field :title %><br /> <%= f.label :artist_tokens, "artists" %><br /> <%= f.text_field :artist_tokens, "data-pre" => @post.artists.map(&:attributes).to_json %> </div> <div class="actions"> <%= f.submit "submit" %> </div> <% end %>

this finds artist value entered artist_tokens field on post form, , added alternative "add {params[:q]}" add together new entries.

class artistscontroller < applicationcontroller def index @artists = artist.where("name ?", "%#{params[:q]}%") results = @artists.map(&:attributes) results << {:name => "add: #{params[:q]}", :id => "create_#{params[:q]}_end"} respond_to |format| format.html format.json { render :json => results } end end

i added additional code parse 'new' entries id , create new artist them. artist_ids assigned again.

post.rb def artist_tokens=(ids) ids.gsub!(/create_(.+?)_end/) artist.create!(:name => $1).id end self.artist_ids = ids.split(",") end

everything works great except ability narrow json results current_user's entries. how go doing this? need store user_id of entries creator in table? how can this?

edit: associations models

# app/models/user.rb class user < activerecord::base has_many :posts has_many :artists, :through => :posts end # app/models/post.rb class post < activerecord::base belongs_to :user has_many :artisanships has_many :artists, :through => :artisanships end # all/models/artist.rb class artist < activerecord::base has_many :artisanships has_many :users, :through => :artisanships has_many :posts, :through => :artisanships end # app/models/artisanship.rb class artisanships < activerecord::base belongs_to :post belongs_to :artist has_one :user, :through => :post end

edit: posts_controller.rb

class postscontroller < applicationcontroller before_filter :authenticate_user!, :only => [:create, :edit, :update, :destroy] before_filter :authorized_user, :only => [:destroy, :edit, :update] def create @user = current_user @post = current_user.posts.build(params[:post]) if @post.save flash[:success] = "post created!" redirect_to root_path else @feed_items = current_user.feed.paginate(:per_page => "10", :page => params[:page]) render 'pages/home' end end def index @posts = post.paginate(:page => params[:page]) end def show @post = post.find(params[:id]) end def edit @post = post.find(params[:id]) end def update @post = post.find(params[:id]) respond_to |format| if @post.update_attributes(params[:post]) format.html { redirect_to(post_path(@post), :notice => 'post updated.') } else format.html { render :action => "edit" } end end end def destroy @post.destroy redirect_to root_path end def likers @title = "likers" @post = post.find(params[:id]) @likers = @post.likers.paginate(:page => params[:page]) render 'show_likers' end def search if params[:q] query = params[:q] @search = post.search keywords query end @posts = @search.results end end private def authorized_user @post = post.find(params[:id]) redirect_to root_path unless current_user?(@post.user) end

edit: attempted alias_method_chain set post's user_id attribute first. (didn't work prepare null db entries) referneced from: rails 3: alias_method_chain still used?

def attributes_with_user_id_first=(attributes = {}) # create sure not accidentally blank out important_attribute when none passed in if attributes.include?(:user_id) self.user_id = attributes.delete(:user_id) end self.attributes_without_user_id_first = attributes end alias_method_chain :attributes=, :user_id_first

if don't want modify in models, can this:

def index @artists = current_user.posts.join("artisianships").join("artists"). where("artisianships.post_id = posts.id"). where("artists.name ?", "#{params[:q]}"). select("artists.name name, artists.id id") results = @artists.map(&:attributes) results << {:name => "add: #{params[:q]}", :id => "create_#{params[:q]}_end"} respond_to |format| format.html format.json { render :json => results } end end

notice there lot of joins going on here, not recommended.

to debug why artist.create!(:name => $1, :user_id => self.user_id) not work, great if can see more code, particularly action create post

update: did seek changing postcontroller#create following, although sense curent code should work is,

@post = current_user.posts.build if @post.update_attributes(params[:post]) # else # else end

i not rails pro , don't understand alias_method_chain can't comment on why didn't work.

sql ruby-on-rails ruby-on-rails-3 jquery-plugins railscasts

Comments

Popular posts from this blog

iphone - Dismissing a UIAlertView -

c# - Can ProtoBuf-Net deserialize to a flat class? -

javascript - Change element in each JQuery tab to dynamically generated colors -