0

안녕 나는 railscasts 사용하는 '작가'나 '사용하고 어디 # 258 http://railscasts.com/episodes/258-token-fieldsRailscast 번호 238 JQuery와 토큰 입력 - 내 JSON 요청은 잘못된 컨트롤러

railscasts에 따라 JQuery와 토큰 입력을 구현하기 위해 노력하고있어 가고있다 artist '

artist_token 필드에 입력을 시작하면 내 json 검색 요청이 ArtistsController # index 작업 대신 UsersController # show 액션에 의해 처리됩니다.

이후로는 JSON이 PagesController 번호 집에서 처리 할 수 ​​있지만 몇 가지 이유로 UsersController # 내가 잘못하고있는 무슨

어떤 아이디어를 보여줄 생각했을 PagesController # 홈 뷰에서 렌더링되는 부분 내 _post_form?

## working example from railscasts #258 

Started GET "/authors.json?q=d" for 127.0.0.1 at 2011-07-03 16:27:31 -0700 
    Processing by AuthorsController#index as JSON 
    Parameters: {"q"=>"d"} 
    Author Load (0.7ms) SELECT "authors".* FROM "authors" WHERE (name like '%d%') 
Completed 200 OK in 17ms (Views: 2.3ms | ActiveRecord: 0.7ms) 


## my log with UsersController#show instead of ArtistsController#index ??? 

Started GET "/artists.json?q=d" for 127.0.0.1 at 2011-07-03 16:12:44 -0700 
    Processing by UsersController#show as JSON 
    Parameters: {"q"=>"d", "id"=>"artists.json"} 
    User Load (0.3ms) SELECT "users".* FROM "users" WHERE "users"."cached_slug" = 'artists.json' LIMIT 1 
    SQL (0.1ms) SELECT sluggable_id FROM slugs WHERE ((slugs.sluggable_type = 'User' AND slugs.name = 'artists.json' AND slugs.sequence = 1)) 
    User Load (0.1ms) SELECT "users".* FROM "users" WHERE "users"."id" = 0 LIMIT 1 
Redirected to 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} do |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 like ?", "%#{params[:q]}%") 
    respond_to do |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 do 
     member do 
     get :likers 
     end 
     collection do 
     get :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 do 
    member do 
    get :following, :followers, :likes 
    end 
    resources :collections 
end 

    # This is a legacy wild controller route that's not recommended for RESTful applications. 
    # Note: This route will make all actions in every controller accessible via GET requests. 
    # match ':controller(/:action(/:id(.:format)))' 
    match '/:id' => 'users#show', :constraints => {:id => /[^\/]+/}, :as => :global_user 
    root :to => "pages#home" 
end 

답변

0

보통 당신이 뭔가가 '잘못'컨트롤러에 의해 처리되는 경우이 같은 문제가있을 때, 먼저 routes.rb 파일을 확인해야합니다. 해당 형식의 모든 요청이 users#show 행동에 라우팅되는 것처럼

# This is a legacy wild controller route that's not recommended for RESTful applications. 
# Note: This route will make all actions in every controller accessible via GET requests. 
# match ':controller(/:action(/:id(.:format)))' 
match '/:id' => 'users#show', :constraints => {:id => /[^\/]+/}, :as => :global_user 

기본적으로 보인다 :

지금 당장 나는이났습니다. 그 match 지시문을 주석 처리하고 지금 작동하는지 확인하십시오. 다른 것들이 깨진다면 그것은 왜 당신이 거기에 필요하고 어떻게 수용 할 수 있는지를 보는 것이 당신에게 달려 있습니다.