Ruby on Rails post path变为get path

时间:2016-12-24 23:08:58

标签: ruby-on-rails

我正在使用Ruby on Rails 5进行个人项目,我似乎无法将链接路由转到正确的控制器操作和客户端操作。

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

我有一个链接操作,可以发布请求以在两个用户之间创建绑定。

Rails.application.routes.draw do

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

我使用资源路由让事情变得更简单,不幸的是,当我点击关注用户时,我的帖子请求变成了获取请求,我无法将任何用户添加到我的以下列表中。

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

当我点击Follow User按钮时,它会将我引导到上面的url,这很好,但是在看完控制台后我发现它是一个GET请求。

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)

以下是以下控制器:

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

无法理解为什么会这样做。

1 个答案:

答案 0 :(得分:0)

我不确定,但我认为这与你为&#34;关注&#34;设置的路线有关。以下内容不是复数,因此不遵守Rails命名约定,因此无法正确生成路由。我会把这个资源切换到&#34;粉丝&#34; (因为关注者是多个关注者)或者设置自己的自定义路由而不是使用资源方法。你可以这样做:

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

相关问题