当我尝试在ajax调用中渲染部分时遇到路由错误:
Routing Error
No route matches {:action=>"destroy", :controller=>"relationships", :user_id=>#<User id: 2, username: .....
在我的应用程序中,我有一个显示在个人资料页面上的用户的关注者列表。我想尝试通过AJAX从服务器返回下一个关注者的偏移量,而不是对关注者进行分页。我的视图已经使用partials来显示这些关注者的列表(限制为5条记录)。
我的目标是使用一个AJAX调用来返回这个部分与下一个格式化记录的偏移量(我还没有实现返回偏移记录的功能 - 我只是想让ajax首先工作)。当我在浏览器中访问配置文件页面(并查看前5条记录)时,部分工作正常,当我进行AJAX调用时发生错误。
以下是ajax调用发起的视图中的表单:
<%= form_tag user_relationships_path(@user), method: :get, remote: true do %>
<%= submit_tag 'load more...' %>
<% end %>
这是路线:
resources :users, only: [:index, :show, :new, :create, :edit, :update, :destroy] do
resources :relationships, only: [:create, :destroy, :index]
end
这是我的控制器操作(relationships#index
),它响应请求:
def index
@user = User.find_by_username(params[:user_id])
respond_to do |format|
format.js { render 'load_followers' }
end
end
load_followers.js.erb partial:
$('ul#followers').append("<%= render 'users/following_items', users: @user.followers %>")
users / following_items.html.erb partial:
<% users.each do |user| %>
<li class="clearfix">
<div class="box-gravatar pull-left">
<%= link_to user do %>
<%= gravatar_for user, 40 %>
<% end %>
</div>
<div class="pull-right">
<%= render 'relationships/follow', user: user %>
</div>
<%= link_to user.username, user %>
<div class="box-author">joined <%= join_date_for user %></div>
</li>
<% end %>
最后关系/ follow.html.erb partial:
<% unless current_user?(user) %>
<% if current_user.following? user %>
<p><%= link_to 'unfollow', user_relationship_path(user), method: :delete, class: "btn" %></p>
<% else %>
<p><%= link_to 'follow', user_relationships_path(user), method: :post, class: "btn btn-primary" %></p>
<% end %>
<% end %>
我已将有问题的代码跟踪到关系/ follow.html.erb partial。当删除它时,ajax调用工作正常,部分被附加到ul
的末尾。显然,它与使用link_to
到relationships#destroy
方法存在问题的rails有关 - 但是,我尝试过的任何内容似乎都无法工作。
修改:以下是运行rake routes
的结果:
root / posts#index
posts_test /posts/test(.:format) posts#test
submit /submit(.:format) posts#new
signup /signup(.:format) users#new
login /login(.:format) sessions#new
logout DELETE /logout(.:format) sessions#destroy
about /about(.:format) about#index
search /search(.:format) search#index
sessions POST /sessions(.:format) sessions#create
new_session GET /sessions/new(.:format) sessions#new
session DELETE /sessions/:id(.:format) sessions#destroy
post_comments POST /posts/:post_id/comments(.:format) comments#create
post_votes POST /posts/:post_id/votes(.:format) votes#create
posts GET /posts(.:format) posts#index
POST /posts(.:format) posts#create
new_post GET /posts/new(.:format) posts#new
post GET /posts/:id(.:format) posts#show
user_relationships GET /users/:user_id/relationships(.:format) relationships#index
POST /users/:user_id/relationships(.:format) relationships#create
new_user_relationship GET /users/:user_id/relationships/new(.:format) relationships#new
edit_user_relationship GET /users/:user_id/relationships/:id/edit(.:format) relationships#edit
user_relationship GET /users/:user_id/relationships/:id(.:format) relationships#show
PUT /users/:user_id/relationships/:id(.:format) relationships#update
DELETE /users/:user_id/relationships/:id(.:format) relationships#destroy
users GET /users(.:format) users#index
POST /users(.:format) users#create
new_user GET /users/new(.:format) users#new
edit_user GET /users/:id/edit(.:format) users#edit
user GET /users/:id(.:format) users#show
PUT /users/:id(.:format) users#update
DELETE /users/:id(.:format) users#destroy
categories GET /categories(.:format) categories#index
POST /categories(.:format) categories#create
new_category GET /categories/new(.:format) categories#new
category GET /categories/:id(.:format) categories#show
/:category(.:format) posts#index
谢谢!
答案 0 :(得分:1)
注意您在此行输出的佣金路线:
DELETE /users/:user_id/relationships/:id(.:format)
这意味着您的指定路由user_relationship需要user
和relationship
个ID。原因是,relationship
是user
的嵌套资源。
例如,您目前在链接中有此内容:
= link_to 'unfollow', user_relationship_path(user), method: :delete, class: "btn"
相反它应该是这样的:
= link_to 'unfollow', user_relationship_path(user, relationship), method: :delete, class: "btn"