【问题标题】:Rails Routing Redirects to wrong pathRails 路由重定向到错误的路径
【发布时间】:2012-06-18 00:03:12
【问题描述】:

我有一个名为“clients”的视图,它显示来自呼叫数据库的呼叫列表。这很好用。但是,当我添加一个带有表单的新按钮时,将创建调用,但它会重定向到调用路径而不是客户端路径。

我不知道它为什么会这样做,我唯一的理论是我正在使用涉及 clients_controller 之外的数据的操作,并且 Rails 以某种方式默认为 calls_path。我的删除操作也发生了同样的事情。有人可以帮助我理解这一点吗?

calls_controller
def new
    @call = Call.new :call_status => "open"

    respond_with @call
  end

  def create
    @call = Call.new(params[:call])

     if @call.save
        redirect_to clients_path, notice: "Call was successfully created."
      else
        render :new
     end
  end 

  def destroy
      @call = Call.find(params[:id])
      @call.destroy

      respond_to do |format|
        format.html { redirect_to clients_index_path }
        format.json { head :no_content }
      end
    end

_form.html.erb
<%= form_for(@call) do |f| %>

  <%= f.label :caller_name %>
  <%= f.text_field :caller_name %>
  <%= f.label :caller_phone %>
  <%= f.text_field :caller_phone, :placeholder => 'xxx-xxx-xxxx' %>
  <%= f.label :caller_email %>
  <%= f.text_field :caller_email %>

  <%= f.button :submit %>

<% end %>

routes.rb
devise_for :users
  match 'mdt' => 'mdt#index'
  get "home/index"
  resources :medics
  resources :clients
  resources :users
  resources :units
  resources :mdt do
    collection do
      put "in_service"
      put "en_route"
      put "to_hospital"
      put "at_hospital"
      put "on_scene" 
      put "out_of_service" 
      put "at_station"
      put "staging"
      put "at_post"
      put "man_down"
  end
  end
  resources :calls do
    member do
      post 'close'
    end
  end
  root :to => 'home#index'
  devise_scope :user do
    get "/login" => "devise/sessions#new"
    delete "/logout" => "devise/sessions#destroy"
  end

【问题讨论】:

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


    【解决方案1】:

    我遇到的问题是路线。我需要一个新呼叫的 post 方法。在创建了两个动作(一个用于创建,一个用于销毁)以及它们的路线之后,一切都开始工作了。看起来它正在尝试使用默认路由和操作,这些路由和操作会出于我的目的重新路由到错误的 URL。

    resources :clients do
        collection do
          post "calls"
        end
        member do
          delete "cancel"
        end
      end
    
    
    
     def calls
        @call = Call.new(params[:call])
    
         if @call.save
            redirect_to clients_path, notice: "Call was successfully created."
          else
            render :new
         end
      end 
    
      def cancel
          @call = Call.find(params[:id])
          @call.destroy
    
          respond_to do |format|
            format.html { redirect_to clients_path }
            format.json { head :no_content }
          end
        end
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2014-04-27
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-07-01
      相关资源
      最近更新 更多