0

I'm trying to redirect to the index action of another controller once I have accomplished a create in the current controller. But the redirect fails, resulting in a url pointing to the index action in the current controller (which doesn't exist, resulting in a blank page)

Here is my code :

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

rake routes

   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
4

1 回答 1

0

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

我强烈建议用简单的英语命名你的班级。

Produit => Product
ProduitsController => ProductsController

Rails 使用了很多约定,但它们在法语处理 pluriels 和英语的方式之间存在一些差异。

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

配置/路由.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
于 2013-07-04T11:51:40.763 回答