未捕获错误:路由参数丢失:id-Javascript控制台
这是我第一次尝试使用ajax和javascript,我遇到了以下问题: 未捕获错误:缺少路由参数:id 我不知道如何修复它/在哪里查找,因为我不明白错误告诉了我什么。“路线参数缺失:id”是什么意思 我不确定哪些代码与此问题相关,因此我提出了一些可能很重要的问题: 我的浏览器显示: 我认为这与用户变量的传递方式有关,但我不确定这种想法是否正确 用户/控制器:未捕获错误:路由参数丢失:id-Javascript控制台,javascript,ruby-on-rails,ajax,routes,Javascript,Ruby On Rails,Ajax,Routes,这是我第一次尝试使用ajax和javascript,我遇到了以下问题: 未捕获错误:缺少路由参数:id 我不知道如何修复它/在哪里查找,因为我不明白错误告诉了我什么。“路线参数缺失:id”是什么意思 我不确定哪些代码与此问题相关,因此我提出了一些可能很重要的问题: 我的浏览器显示: 我认为这与用户变量的传递方式有关,但我不确定这种想法是否正确 用户/控制器: def index @users = User.search(params[:search]) end def cre
def index
@users = User.search(params[:search])
end
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
查看/用户/索引:
<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.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>")
}
});
});
});
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
诚然,我没有使用JS路由,但我认为您的问题是因为您使用的是
路由.relationship\u path
,而不是路由.relationships\u path
。请注意,前者是单数关系(这就是为什么路由需要关系的id),而relationships\u path
应该链接到集合
Routes.relationships_path() // => "/relationships"
Routes.relationships_path(1) // => "/relationships/1"
$(文档).ready(函数(){
$(“#添加关系”)。单击(函数(事件){
event.preventDefault();
var addrelationshipptn=$(此项);
$.ajax({
//注意关系-而不是关系
url:Routes.relationships_path(),
数据:{
关系:{followed\u id:addRelationshipTn.data('followedId')}
},
数据类型:“json”,
键入:“POST”,
成功:职能(e){
addRelationshipptn.hide();
$(“#关系状态”).html(“”)
}
});
});
});
另一个问题是,您试图在查询参数中传递
后跟的\u id
,而不是POST请求正文。您正在使用吗?您可能还想添加路由.rb
文件。是的,我正在使用js路由!添加了我能想到的路线和任何相关的js路线。谢谢你的评论,我想你说得对。。事实上,是的,它检查了所有,这似乎已经做了把戏!非常感谢您的帮助@papirtiger!我现在添加了一个编辑,您可能也想查看一下。刚才看到了您的编辑-您所说的“您在哪里尝试在查询参数中而不是在POST请求正文中传递后面的\u id”是什么意思?在将路径更改为“relationships”后,我最初遇到了一个POST问题,但随后将users/index文件中的4x@user更改为user.id,这似乎有效。在GET请求中,您在URL中传递参数,例如foo.com?bar=baz。过帐表单时,您会在请求正文中发送数据。例如,这使得窥探公共wifi流量的人更难危害您的用户。然而,Rails将解析查询字符串和请求体,并将它们全部合并到params散列中——这就是代码工作的原因。HTTP PUT&PATCH谓词也是如此。
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
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>")
}
});
});
});