Rails:呈现AJAX部分时路由错误



当我尝试在ajax调用中呈现部分时,我遇到了路由错误:

Routing Error
No route matches {:action=>"destroy", :controller=>"relationships", :user_id=>#<User id: 2, username: .....

在我的应用程序中,我有一个用户在个人资料页面上显示的关注者列表。我想尝试通过AJAX从服务器返回追随者的下一个偏移量,而不是对追随者进行分页。我的视图已经使用部分来显示这些追随者的列表(限制为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部分:

$('ul#followers').append("<%= render 'users/following_items', users: @user.followers %>")

用户/following_items.html。erb部分:

<% 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 %>

最后是relationships/follow.html。erb部分:

<% 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 %>

我已经追踪到违规代码到relationships/follow.html。erb部分。当它被删除后,ajax调用工作正常,部分被附加到ul的末尾。显然,它与轨道有link_torelationships#destroy方法的问题有关-然而,我所尝试的一切似乎都不起作用。

编辑:下面是运行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

谢谢!

注意您的rake路由输出如下:

DELETE /users/:user_id/relationships/:id(.:format)

这意味着您的命名路由user_relationship期望userrelationship id。原因是,relationshipuser的嵌套资源。

比如你现在的链接是:

= link_to 'unfollow', user_relationship_path(user), method: :delete, class: "btn"

应该是这样的:

= link_to 'unfollow', user_relationship_path(user, relationship), method: :delete, class: "btn"

相关内容

  • 没有找到相关文章