【问题标题】:Problems with routes.rbroutes.rb 的问题
【发布时间】:2012-05-30 21:17:07
【问题描述】:

我的 routes.rb 有问题

在我的网络应用程序中,我有两个带有登录表单的页面

“/home/index”和“/users/index”

这是我的路线文件

  controller :home do
    get     'login'   => :index
    post    'login'   => :create
  end

  controller :users do
    get     'login'   => :index
    post    'login'   => :post_login
  end

  get "home/index"

  get "home/create"

  get "home/show"

  get "private/index"

  get "users/index"

  get "users/get_login"

  get "users/post_login"

  resources :users do
    collection do
      get  'get_login'
      post 'post_login'
    end
  end

问题是,如果我在“home/index”中使用登录,它会执行主控制器的正确操作“create”,而如果我在“users/index”页面登录,它会再次执行“create” “post_login”,为什么?

我在“/home/index”和“/users/index”中用于表单的代码是一样的...

这是简单的形式

<%= form_tag do %>

    <table>
        <tr>
            <td>
                <%= text_field_tag :name, params[:name] %>
            </td>
        </tr>
        <tr>            
            <td>
                <%= password_field_tag :password, params[:password] %>
            </td>
        </tr>
        <tr>
            <td>
                <%= submit_tag "Login" %>           
            </td>
        </tr>
<% end %>
        <tr>            
            <td>
                <%= link_to 'Registrazione', users_index_url %>             
            </td>
        </tr>

    </table>

用户控制器代码:

class UsersController < ApplicationController


      def index

      end  

      def post_login
        redirect_to "http://www.google.it"
      end

    end

【问题讨论】:

  • 能否请您发布罪魁祸首表单中的代码,我怀疑这两个操作都指向home/index 路径

标签: ruby-on-rails ruby-on-rails-3


【解决方案1】:

我会有用户,只是作为资源:

 controller :home do
    get     'login'   => :index
    post    'login'   => :create
  end

  get "/users" => 'users#index'
  post "/users" => 'users#post_login'


  get "home/index"

  get "home/create"

  get "home/show"

  get "private/index"

在唯一的数组中,只需添加您正在使用的操作...

【讨论】:

  • 以这种方式给出了这个错误:Unknown action The action 'show' could not be found for UsersController,但我不使用这个动作
  • 然后在资源中使用 :only => [] 。看看我的回复,我已经更新了
  • 现在当我从“/users/index”页面单击登录按钮时,它给了我这个错误:没有路由匹配 [POST]“/users”
  • 我更好地组织了 routes.rb 文件,试试那个。如果它不起作用,请从您的用户控制器发布代码。
  • 我在第一篇文章中添加了用户控制器,但它非常简单
猜你喜欢
  • 2010-10-12
  • 2012-09-14
  • 1970-01-01
  • 2014-09-04
  • 2023-03-29
  • 2017-07-13
  • 1970-01-01
  • 1970-01-01
  • 2018-12-03
相关资源
最近更新 更多