ruby on rails 3 - Railscast #238 jquery token input - My json request is going to the wrong controller -
hi i'm trying implement jquery token input according railscasts #258 http://railscasts.com/episodes/258-token-fields
where railscasts uses 'author' using 'artist'
when start typing in artist_token field json search request processed userscontroller#show action instead of artistscontroller#index action.
since _post_form partial rendered on pagescontroller#home view have thought json might processed pagescontroller#home reason userscontroller#show
any ideas i'm doing wrong?
## working example railscasts #258 started "/authors.json?q=d" 127.0.0.1 @ 2011-07-03 16:27:31 -0700 processing authorscontroller#index json parameters: {"q"=>"d"} author load (0.7ms) select "authors".* "authors" (name '%d%') completed 200 ok in 17ms (views: 2.3ms | activerecord: 0.7ms) ## log userscontroller#show instead of artistscontroller#index ??? started "/artists.json?q=d" 127.0.0.1 @ 2011-07-03 16:12:44 -0700 processing userscontroller#show json parameters: {"q"=>"d", "id"=>"artists.json"} user load (0.3ms) select "users".* "users" "users"."cached_slug" = 'artists.json' limit 1 sql (0.1ms) select sluggable_id slugs ((slugs.sluggable_type = 'user' , slugs.name = 'artists.json' , slugs.sequence = 1)) user load (0.1ms) select "users".* "users" "users"."id" = 0 limit 1 redirected http://localhost:3000/ completed 302 found in 133ms ## pages controller class pagescontroller < applicationcontroller def home @title = "home" @featured_posts = post.featured.limit(10) if user_signed_in? @user = current_user @post = current_user.posts.build @feed_items = current_user.feed.paginate(:per_page => "10", :page => params[:page]) else #render :layout => 'special_layout' end end ## views/pages/home.html.erb <% if user_signed_in? %> <%= render 'shared/post_form'%> <%= render 'shared/user_info' %> <%= render 'shared/stats' %> ## views/shared/_post form.html.erb <%= 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 %> ## artists controller class artistscontroller < applicationcontroller def index @artists = artist.where("name ?", "%#{params[:q]}%") respond_to |format| format.html format.json { render :json => @artists.map(&:attributes) } end end def show @artist = artist.find(params[:id]) end def new @artist = artist.new end def create @artist = artist.new(params[:author]) if @artist.save redirect_to @artist, :notice => "successfully created artist." else render :action => 'new' end end def edit @artist = artist.find(params[:id]) end def update @artist = artist.find(params[:id]) if @artist.update_attributes(params[:artist]) redirect_to @artist, :notice => "successfully updated artist." else render :action => 'edit' end end def destroy @artist = artist.find(params[:id]) @artist.destroy redirect_to authors_url, :notice => "successfully destroyed artist." end end ## artist model class artist < activerecord::base attr_accessible :name has_many :artistizations has_many :posts, :through => :artistizations end ## artistization model class artistization < activerecord::base attr_accessible :post_id, :artist_id belongs_to :artist belongs_to :post end ## post model attr_accessible :title, :artist_tokens has_many :artistizations has_many :artists, :through => :artistizations attr_reader :artist_tokens def artist_tokens=(ids) self.artist_ids = ids.split(",") end ## application.js $(function() { $("#post_artist_tokens").tokeninput("/artists.json", { crossdomain: false, prepopulate: $("#post_artist_tokens").data("pre"), theme: "facebook" }); }); ## routes match 'auth/:provider/callback' => 'authentications#create' resources :authentications devise_for :admins match '/admin' => 'railsadmin/main#index' devise_for :users, :controllers => {:registrations => 'registrations'} resources :posts member :likers end collection :search end end resources :relationships, :only => [:create, :destroy] resources :appreciations, :only => [:create, :destroy] match '/contact', :to => 'pages#contact' match '/about', :to => 'pages#about' match '/help', :to => 'pages#help' match '/blog', :to => 'pages#blog' resources :users member :following, :followers, :likes end resources :collections end # legacy wild controller route that's not recommended restful applications. # note: route make actions in every controller accessible via requests. # match ':controller(/:action(/:id(.:format)))' match '/:id' => 'users#show', :constraints => {:id => /[^\/]+/}, :as => :global_user root :to => "pages#home" end
usually when have problems these, being processed 'wrong' controller, should first check routes.rb file.
right away noticed this:
# legacy wild controller route that's not recommended restful applications. # note: route make actions in every controller accessible via requests. # match ':controller(/:action(/:id(.:format)))' match '/:id' => 'users#show', :constraints => {:id => /[^\/]+/}, :as => :global_user basically seems every request in format being routed users#show action. try commenting out match directive , see if works now. if other things break it's see why need there , how can accommodate accordingly.
Comments
Post a Comment