2016-12-24 13 views
0

個人的なプロジェクトでRuby on Rails 5を使用していますが、正しいコントローラアクションとクライアントアクションへのリンクルートを取得できません。Ruby on Railsの投稿パスがget pathに変わります

<div class="admin-users index"> 
    <h2>Admin Users</h2> 
    <%= image_tag('plus_sign.png', :size => '11x11', :alt => '+') %> 
    <%= link_to("Add New Admin User", new_admin_user_path, :class => 'action new') %> 
    <table class="listing" summary="Admin user list"> 
     <tr class="header"> 
      <th>Username</th> 
      <th>Name</th> 
      <th>Email</th> 
      <th>Actions</th> 
     </tr> 
     <% @admin_users.each do |admin_user| %> 
     <tr> 
      <td><%= admin_user.username %></td> 
      <td><%= admin_user.name %></td> 
      <td><%= mail_to(admin_user.email) %></td> 
      <td class="actions"> 
       <%= link_to("Edit", edit_admin_user_path(admin_user), :class => 'action edit') %> 
       <%= link_to("Delete", delete_admin_user_path(admin_user), :class => 'action delete') %> 
       <%= link_to("Follow User", followings_path(:followed_id => admin_user), :method => :post)%> 


      </td> 
     </tr> 
     <% end %> 
    </table> 
</div> 

私は、2人のユーザー間でバインディングを作成するためのポストリクエストを作成するリンクアクションを持っています。私がフォローユーザをクリックすると

Rails.application.routes.draw do 

    resources :followings, :except => [:show, :update, :delete, :new] do 
    end 

私は私のために物事を簡単にするためのリソースのルーティングを使用し、残念ながら、私のポスト要求は、GETリクエストに変身し、私は私の次のリストにすべてのユーザーを追加することはできません。

http://localhost:3000/followings?followed_id=5 

私がフォローユーザーボタンにクリックすると、それは結構です上記URLに私をリードしたが、その後コンソールを見た後、私はそれがGETリクエストです参照してください。それがこれをやっている理由

class FollowingsController < ApplicationController 
    layout 'admin' 
    def index 
    admin_user = AdminUser.find(session[:user_id]) 
    @user = admin_user 
    end 

    def create 
     current_user = AdminUser.find(session[:user_id]) 
     @following = current_user.following.build(:followed_id => params[:followed_id]) 
     if @following.save 
     flash[:notice] = "Added user." 
     redirect_to root_url 
     else 
     flash[:notice] = "Cannot add user." 
     redirect_to(admin_users_path) 
     end 
    end 

    def destroy 
     current_user = AdminUser.find(session[:user_id]) 
     @following = current_user.followings.find(params[:id]) 
     @following.destroy 
     flash[:notice] = "Removed user." 
     redirect_to root_url 
    end 
end 

を把握することはできません:

Started GET "/followings?followed_id=5" for ::1 at 2016-12-24 18:16:26 -0400 
    ActiveRecord::SchemaMigration Load (1.0ms) SELECT `schema_migrations`.* FROM `schema_migrations` 
Processing by FollowingsController#index as HTML 
    Parameters: {"followed_id"=>"5"} 
    AdminUser Load (0.0ms) SELECT `admin_users`.* FROM `admin_users` WHERE `admin_users`.`id` = 1 LIMIT 1 
    Rendering followings/index.html.erb within layouts/admin 
    Following Load (0.0ms) SELECT `followings`.* FROM `followings` WHERE `followings`.`admin_user_id` = 1 
    AdminUser Load (1.0ms) SELECT `admin_users`.* FROM `admin_users` WHERE `admin_users`.`id` = 3 LIMIT 1 
    Rendered followings/index.html.erb within layouts/admin (64.0ms) 
Completed 200 OK in 542ms (Views: 448.4ms | ActiveRecord: 10.0ms) 

ここでは、次のコントローラです。

+0

生成されたhtmlのリンクに 'data-method = post'がありますか? – Iceman

+0

はい、持っています。 – user3435009

+0

'次のコントローラを表示できますか? –

答えて

0

私には分かりませんが、「フォローイング」のために設定したルートとは関係があると思います。以下は複数形ではないので、Railsの命名規則に従わないので、正しく経路を生成することができません。そのリソースを「フォロワー」(フォロワーはフォロワーの複数であるため)に切り替えるか、リソースメソッドを使用する代わりに独自のカスタムルートを設定します。あなたが好きな何かができる:

post 'follow_user/:followed_id' => 'followings#create', as: :follow_user 
get 'followings' => 'followings#index', as: :followings 

その後、あなたのフォローユーザリンクは次のようになります。

<%= link_to("Follow User", follow_user_path(followed_id: admin_user), method: :post) %> 

私はまた、レコードを更新または作成するときに、あなたのコントローラに強いパラメータを使用してお勧めします。 http://api.rubyonrails.org/classes/ActionController/StrongParameters.html

関連する問題