Problems with routes.rb

只谈情不闲聊 提交于 2019-12-25 01:36:47

问题


I have a problem with routes.rb

In my web application i have two pages with the login form

"/home/index" and "/users/index"

This is my routes file

  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

the problem is that if i use login with my "home/index" it executes the right action "create" of the home controller, while if i login in "users/index" page it executes again the "create" instead of "post_login", why?

The code i used for my form in "/home/index" and "/users/index" is the same...

This is the simple form

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

the users controller code:

class UsersController < ApplicationController


      def index

      end  

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

    end

回答1:


I would have users, only as resources:

 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"

In the only array, just add the actions that u are using...



来源:https://stackoverflow.com/questions/10824412/problems-with-routes-rb

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!