在RoR 3.2.13中,重定向到另一个控制器失败

时间:2013-07-04 09:18:16

标签: redirect ruby-on-rails-3.2 controller

在我在当前控制器中完成创建后,我正在尝试重定向到另一个控制器的索引操作。但重定向失败,导致指向当前控制器中的索引操作的URL(不存在,导致空白页)

这是我的代码:

clients_controller.rb

def create
  @client = Client.new(params[:client])

  respond_to do |format|
    if @client.save
      session[:client_id] = @client.id
      redirect_to produits_url
    else
      format.html { render action: "new" }
    end
  end
end

produits_controller.rb

def index
  @produits = current_client.produits

  respond_to do |format|
    format.html # index.html.erb
    format.json { render json: @produits }
  end
end

的routes.rb

  get "home/index"

  get "log_out" => "sessions#destroy", :as => "log_out"
  get "log_in" => "sessions#new", :as => "log_in"
  get "inscription" => "clients#new", :as => "inscription"
  match '/a_propos', to: 'application#a_propos'

  resources :sessions

  namespace "admin" do
    resources :clients, only: [:index]
  end
    resources :clients, only: [:create, :edit, :update, :destroy]

  resources :produits

佣金路线

   home_index GET    /home/index(.:format)        home#index
      log_out GET    /log_out(.:format)           sessions#destroy
       log_in GET    /log_in(.:format)            sessions#new
  inscription GET    /inscription(.:format)       clients#new
     a_propos        /a_propos(.:format)          application#a_propos
     sessions GET    /sessions(.:format)          sessions#index
              POST   /sessions(.:format)          sessions#create
  new_session GET    /sessions/new(.:format)      sessions#new
 edit_session GET    /sessions/:id/edit(.:format) sessions#edit
      session GET    /sessions/:id(.:format)      sessions#show
              PUT    /sessions/:id(.:format)      sessions#update
              DELETE /sessions/:id(.:format)      sessions#destroy
admin_clients GET    /admin/clients(.:format)     admin/clients#index
      clients POST   /clients(.:format)           clients#create
  edit_client GET    /clients/:id/edit(.:format)  clients#edit
       client PUT    /clients/:id(.:format)       clients#update
              DELETE /clients/:id(.:format)       clients#destroy
     produits GET    /produits(.:format)          produits#index
              POST   /produits(.:format)          produits#create
  new_produit GET    /produits/new(.:format)      produits#new
 edit_produit GET    /produits/:id/edit(.:format) produits#edit
      produit GET    /produits/:id(.:format)      produits#show
              PUT    /produits/:id(.:format)      produits#update
              DELETE /produits/:id(.:format)      produits#destroy
         root        /                            home#index

1 个答案:

答案 0 :(得分:0)

对于您的问题,您需要调用redirect_to块内的format.html。但是这可能非常难看,我建议使用respond_to。见下文:

我强烈建议用简单的英语命名你的课程。

Produit => Product
ProduitsController => ProductsController

Rails正在使用很多惯例,但它们在法语处理pluriels和英语之间存在一些差异。

然后你应该能够实现你想做的事情:

<强>配置/ routes.rb中

resources :products

应用/控制器/ products_controller.rb

class ProductsController
  def index
    # your code
  end
end

应用/控制器/ clients_controller.rb

class ClientsController
  respond_to :html

  def new
    # your code
  end

  def create
    @client = Client.new(params[:client])
    if @client.save
      session[:client_id] = @client.id
      redirect_to products_path
    else
      render :new
    end
  end
end