這是我第一次嘗試使用Ajax和JavaScript和我碰到過這樣的問題:未捕獲的錯誤:Route參數丟失:ID - JavaScript控制檯]
未捕獲的錯誤:Route參數丟失:ID
我不知道如何解決它/在哪裏看,因爲我不明白錯誤告訴我什麼。 '路由參數丟失:id'是什麼意思?
我不知道什麼樣的代碼是有關這個問題,所以我已經有裂紋,搭建了可能是重要的:
我的瀏覽器顯示:
我認爲這與用戶變量如何傳遞有關,但我不確定這個想法是否在正確的軌道上。
用戶/控制器:
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
你在使用https://github.com/railsware/js-routes嗎? – max 2014-11-04 00:54:39
你也可以添加你的'routes.rb'文件。 – max 2014-11-04 00:56:03
是的,我正在使用js-routes!添加了我能想到的路線和任何相關的js-routes。感謝評論@papirtiger – sss333 2014-11-04 01:57:52