Note that there are some explanatory texts on larger screens.

plurals
  1. PORails : Using jquery tokeninput (railscast #258) to create new entries
    primarykey
    data
    text
    <p>I have successfully been able to implement the addition of new artist entries which was not included in Ryan Bates railscast #258 <a href="http://railscasts.com/episodes/258-token-fields" rel="nofollow noreferrer">http://railscasts.com/episodes/258-token-fields</a></p> <p>So in other words, a user can enter an artist name which will autocomplete using jquery tokinput. However, I'd like the autocomplete results to only display the artist names which were created by that individual user. </p> <p>Does this make sense? An better and more understandable example would be for a collection.rb, where users create posts and specify a 'collection' for the post to belong to, but they should only be able to add posts to the collections which they created themselves. </p> <p>This is the post form with artist_tokens as a virtual attribute:</p> <pre><code>&lt;%= form_for @post, :validate =&gt; true, :html =&gt; {:multipart =&gt; true} do |f| %&gt; &lt;%= render 'shared/error_messages', :object =&gt; f.object %&gt; &lt;div class="field"&gt; &lt;%= f.label :title, 'Title:' %&gt;&lt;br /&gt; &lt;%= f.text_field :title %&gt;&lt;br /&gt; &lt;%= f.label :artist_tokens, "Artists" %&gt;&lt;br /&gt; &lt;%= f.text_field :artist_tokens, "data-pre" =&gt; @post.artists.map(&amp;:attributes).to_json %&gt; &lt;/div&gt; &lt;div class="actions"&gt; &lt;%= f.submit "Submit" %&gt; &lt;/div&gt; &lt;% end %&gt; </code></pre> <p>This finds the artist by the value entered into the artist_tokens field on the post form, and I also added the option "Add {params[:q]}" to add new entries.</p> <pre><code>class ArtistsController &lt; ApplicationController def index @artists = Artist.where("name like ?", "%#{params[:q]}%") results = @artists.map(&amp;:attributes) results &lt;&lt; {:name =&gt; "Add: #{params[:q]}", :id =&gt; "CREATE_#{params[:q]}_END"} respond_to do |format| format.html format.json { render :json =&gt; results } end end </code></pre> <p>I added the additional code to parse the 'new' entries by id and then create a new artist with them. Then the artist_ids are assigned again. </p> <pre><code>post.rb def artist_tokens=(ids) ids.gsub!(/CREATE_(.+?)_END/) do Artist.create!(:name =&gt; $1).id end self.artist_ids = ids.split(",") end </code></pre> <p>Everything works great except the ability to narrow the json results by only the current_user's entries. How would I go about doing this? Do I need to store the user_id of the entries creator in the table? How can I do this?</p> <p>EDIT: associations for models</p> <pre><code># app/models/user.rb class User &lt; ActiveRecord::Base has_many :posts has_many :artists, :through =&gt; :posts end # app/models/post.rb class Post &lt; ActiveRecord::Base belongs_to :user has_many :artisanships has_many :artists, :through =&gt; :artisanships end # all/models/artist.rb class Artist &lt; ActiveRecord::Base has_many :artisanships has_many :users, :through =&gt; :artisanships has_many :posts, :through =&gt; :artisanships end # app/models/artisanship.rb class Artisanships &lt; ActiveRecord::Base belongs_to :post belongs_to :artist has_one :user, :through =&gt; :post end </code></pre> <p>EDIT: posts_controller.rb</p> <pre><code>class PostsController &lt; ApplicationController before_filter :authenticate_user!, :only =&gt; [:create, :edit, :update, :destroy] before_filter :authorized_user, :only =&gt; [: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 =&gt; "10", :page =&gt; params[:page]) render 'pages/home' end end def index @posts = Post.paginate(:page =&gt; 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 do |format| if @post.update_attributes(params[:post]) format.html { redirect_to(post_path(@post), :notice =&gt; 'Post was successfully updated.') } else format.html { render :action =&gt; "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 =&gt; params[:page]) render 'show_likers' end def search if params[:q] query = params[:q] @search = Post.search do 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 </code></pre> <p>Edit: Attempted alias_method_chain to set post's user_id attribute first. (didn't work to fix the NULL db entries) referneced from: <a href="https://stackoverflow.com/questions/3689736/rails-3-alias-method-chain-still-used">Rails 3: alias_method_chain still used?</a></p> <pre><code> def attributes_with_user_id_first=(attributes = {}) # Make sure not to accidentally blank out the important_attribute when none is 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 </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload