2014-11-04 32 views
0

這是我第一次嘗試使用Ajax和JavaScript和我碰到過這樣的問題:未捕獲的錯誤:Route參數丟失:ID - JavaScript控制檯]

未捕獲的錯誤:Route參數丟失:ID

我不知道如何解決它/在哪裏看,因爲我不明白錯誤告訴我什麼。 '路由參數丟失:id'是什麼意思?

我不知道什麼樣的代碼是有關這個問題,所以我已經有裂紋,搭建了可能是重要的:

我的瀏覽器顯示:

javascript console in chrome

我認爲這與用戶變量如何傳遞有關,但我不確定這個想法是否在正確的軌道上。

用戶/控制器:

def index 
    @users = User.search(params[:search]) 
    end 

視圖/用戶/索引:

<ul> 
    <% @users.each do |user| %> 
     <li> 
      <%= user.name %> 
      <% if logged_in? %> 
       <div id="relationship-status"> 
        <% if current_user.following.include?(@user) %> 
         <%= link_to "Edit Relationship", edit_relationship_path(followed_id: @user), class: "btn btn-primary" %> 
        <% else %> 
         <%= link_to "Add Relationship", new_relationship_path(followed_id: @user), class: "btn btn-primary", id: 'add-relationship', data: { followed_id: @user.to_param } %> 
        <% end %> 
       </div> 
      <% end %> 
     </li> 
    <% end %> 
</ul> 

relationship_controller:

def create 
    if params[:relationship] && params[:relationship].has_key?(:followed_id) 
     @followed = User.find(params[:relationship][:followed_id]) 
     # @followed = User.where(name: params[:relationship][:followed_id]).first 
     @relationship = Relationship.request(current_user, @followed) 
     respond_to do |format| 
     if @relationship.new_record? 
      format.html do 
      flash[:danger] = "There was a problem creating that relationship request" 
      redirect_to followed_path(@followed) 
      end 
      format.json { render json: @relationship.to_json, status: :precondition_failed } 
     else 
      format.html do 
      flash[:success] = "Friend request sent" 
      redirect_to followed_path(@followed) 
      end 
      format.json { render json: @relationship.to_json } 
     end 

     end 
    else 
     flash[:danger] = "Friend Required" 
     redirect_to users_path 
    end 
    end 

relationship.js:

$(document).ready(function() { 

    $('#add-relationship').click(function(event) { 
     event.preventDefault(); 
     var addRelationshipBtn = $(this); 
     $.ajax({ 
      url: Routes.relationship_path({relationship: { followed_id: addRelationshipBtn.data('followedId') }}), 
      dataType: 'json', 
      type: 'POST', 
      success: function(e) { 
       addRelationshipBtn.hide(); 
       $('#relationship-status').html("<a href='#' class='btn btn-success'>Relationship Requested</a>") 
      } 
     }); 
    }); 
}); 

編輯:

:在加入周

的application.js:

// This is a manifest file that'll be compiled into application.js, which will include all the files 
// listed below. 
// 
// Any JavaScript/Coffee file within this directory, lib/assets/javascripts, vendor/assets/javascripts, 
// or vendor/assets/javascripts of plugins, if any, can be referenced here using a relative path. 
// 
// It's not advisable to add code directly here, but if you do, it'll appear at the bottom of the 
// compiled file. 
// 
// Read Sprockets README (https://github.com/sstephenson/sprockets#sprockets-directives) for details 
// about supported directives. 
// 
//= require jquery 
//= require jquery_ujs 
//= require bootstrap 
//= require turbolinks 
//= require_tree . 
//= require js-routes 

的config/application.rb中:

class Application < Rails::Application 
    # Settings in config/environments/* take precedence over those specified here. 
    # Application configuration should go into files in config/initializers 
    # -- all .rb files in that directory are automatically loaded. 

    # Set Time.zone default to the specified zone and make Active Record auto-convert to this zone. 
    # Run "rake -D time" for a list of tasks for finding time zone names. Default is UTC. 
    # config.time_zone = 'Central Time (US & Canada)' 

    # The default locale is :en and all translations from config/locales/*.rb,yml are auto loaded. 
    # config.i18n.load_path += Dir[Rails.root.join('my', 'locales', '*.{rb,yml}').to_s] 
    # config.i18n.default_locale = :de 
    config.assets.initialize_on_precompile = true 
    end 
end 

路線:

Rails.application.routes.draw做

root      'static_pages#home' 
    get  'help'  =>  'static_pages#help' 
    get  'about' =>  'static_pages#about' 
    get  'contact' =>  'static_pages#contact' 
    get  'signup' =>  'users#new' 
    get  'login' =>  'sessions#new' 
    delete 'logout' =>  'sessions#destroy' 
    get  'search' =>  'users#index' 
    get  'followed' =>  'users#show_followed' 
    resources :users do 
    member do 
     get :following, :followers 
    end 
    end 
    resources :sessions,   only: [:new, :create, :destroy] 
    resources :account_activations, only: [:edit] 
    resources :password_resets,  only: [:new, :create, :edit, :update] 
    resources :profiles,   only: [:edit, :update, :destroy] do 
    put :email, on: :member 
    end 
    resources :relationships,  only: [:new, :create, :index, :edit, :accept, :destroy] do 
    member do 
     put :accept 
    end 
    end 
+0

你在使用https://github.com/railsware/js-routes嗎? – max 2014-11-04 00:54:39

+0

你也可以添加你的'routes.rb'文件。 – max 2014-11-04 00:56:03

+0

是的,我正在使用js-routes!添加了我能想到的路線和任何相關的js-routes。感謝評論@papirtiger – sss333 2014-11-04 01:57:52

回答

1

誠然,我的天堂沒有使用JS路線,但我認爲你的問題是由於你正在使用Routes.relationship_path而不是Routes.relationships_path。請注意,前者是單數(這就是爲什麼路線期望id與關係)關係,而relationships_path應該鏈接到該集合。

Routes.relationships_path() // => "/relationships" 
Routes.relationships_path(1) // => "/relationships/1" 

$(document).ready(function() { 

    $('#add-relationship').click(function(event) { 
     event.preventDefault(); 
     var addRelationshipBtn = $(this); 
     $.ajax({ 
      // note relationships - not relationship 
      url: Routes.relationships_path(), 
      data: { 
       relationship: { followed_id: addRelationshipBtn.data('followedId') } 
      }, 
      dataType: 'json', 
      type: 'POST', 
      success: function(e) { 
       addRelationshipBtn.hide(); 
       $('#relationship-status').html("<a href='#' class='btn btn-success'>Relationship Requested</a>") 
      } 
     }); 
    }); 
}); 

另一個問題是,你在哪裏試圖通過查詢參數,而不是POST請求的身體followed_id

+0

我認爲你就在那裏!事實上,它檢查了一切,這似乎已經做到了!非常感謝您的幫助@papirtiger! – sss333 2014-11-04 04:30:04

+0

我現在添加了一個編輯,你可能也想看看 – max 2014-11-04 04:30:44

+0

..剛纔看到了你的編輯 - 你試圖傳遞查詢參數中的followed_id而不是POST請求體的意思是什麼? 我在將路徑更改爲'關係'後最初得到POST問題,但之後將用戶/索引文件中的4x @user更改爲user.id,這似乎工作。 – sss333 2014-11-04 04:35:40

相關問題