Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6217213
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T07:23:12+00:00 2026-05-24T07:23:12+00:00

This has been completed all code below is working. If your familiar with the

  • 0

This has been completed all code below is working.

If your familiar with the R.B Rails Cast that was done on this i am instead trying to work with the “@authorships” form instead of the Book-to-Author itself.

I am using Jquery TokenInput and this is my setup in more detail:


Application.js

$(function() {
  $("#user_product_product_token").tokenInput("/products.json", {
    prePopulate: $("#user_product_product_token").data("pre"),
    tokenLimit: 1
  });
});

class UserProduct
    attr_accessible :product_token, :price
    belongs_to :user
    belongs_to :product
    attr_reader :product_token

    def product_token=(id)
        self.product_id = id
    end
end

class Product
    has_many :user_products
    has_many :users, :through => :user_products
end

This gets my /products.json route to work and list all of my products with ID and Name.

class ProductsController

  def index
    @products = Product.where("name like ?", "%#{params[:q]}%")

    respond_to do |format|
      format.json { render :json => @products.map(&:attributes) } # This here
    end
  end

This is my form:

<%= form_for(@user_product) do |f| %>
    <%= f.text_field :product_token, "data-pre" => @products.map(&:attributes).to_json %>
    <%= f.label :price %><br />
    <%= f.text_field :price %>
    <%= f.submit %>
<% end %>

class UserProductsController

def new
    @user_product = current_user.user_products.build
    # This just initializes the @products variable for the @products.map and ProductsController.
    @products = []
 end

def edit
    @user_product = UserProduct.find(params[:id])
    # Without this line below it cant find the correct product.
    @products = [@user_product.product]
end

Here’s my gist on it: https://gist.github.com/1116092

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-24T07:23:13+00:00Added an answer on May 24, 2026 at 7:23 am

    Your @products instance variable is not set, as already pointed out by Luke, but but his solution to it is not probably what you want. You want to pre-populate the product if some is already selected in UserProduct. which would not be set as your are creating a new UserProduct in the action, but if you use same form with edit also, then it might be used. I would suggest to modify your action on following lines in both new and edit action:

    def new
      @user_product = current_user.user_products.build
      @products = [@user_product.product]
    
      respond_to do |format|
        format.html # new.html.erb
        format.xml  { render :xml => @user_product }
      end
    end
    

    One other thing you need to take care is, set the max option while initializing jquery-token-input to 1, as you can have one user_product belong to only one product.

    One other thing I noticed is your method to save product_token, you misssed the actual assignment there:

    def product_token=(id)
        self.product_id = id # assign it to product_id
    end
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

This has been a problem that I haven't been able to figure out for
This has been asked before (question no. 308581) , but that particular question and
I put together some VBA code for Outlook 2007 which has been working predominantly
Im sure this has been asked 1000 times before, basically all I want to
This has been an adventure. I started with the looping duplicate query located in
This has been driving me crazy for a few days. Why doesn't the following
This has been troubling me for a few years, and was recently exacerbated by
This has been confusing me for some time. With the advent of UTF-8 as
This has been a rather problematic issue on numerous occasions. We have alot of
This has been driving me crazy for the past few minutes I have a

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.