2011-09-26 350 views
2

嗨希望在这里得到一些答案。我一遍又一遍地想到,它正在杀死我。现在想要得到答案,现在我发现的所有事情都比我的小问题系统更复杂。问题在于以下信息。管理产品路由

首先我的路线文件:

get 'admin' => 'admin#index' 
    namespace "admin" do 
    resources :products 
end 

我联系产品控制器如下:

class Admin::ProductsController < ApplicationController 
    # GET /products 
    # GET /products.json 
    def index 
    @products = Product.all 

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

# GET /products/1 
# GET /products/1.json 
def show 
    @product = Product.find(params[:id]) 

    respond_to do |format| 
    format.html # show.html.erb 
    format.json { render json: @product } 
    end 
end 

# GET /products/new 
# GET /products/new.json 
    def new 
    @product = Product.new 

    respond_to do |format| 
     format.html # new.html.erb 
     format.json { render json: @product } 
    end 
end 

# GET /products/1/edit 
def edit 
    @product = Product.find(params[:id]) 
end 

    # POST /products 
    # POST /products.json 
    def create 
    @product = Product.new(params[:product]) 

    respond_to do |format| 
    if @product.save 
     format.html { redirect_to @product, notice: 'Product was successfully created.' } 
     format.json { render json: @product, status: :created, location: @product } 
    else 
     format.html { render action: "new" } 
     format.json { render json: @product.errors, status: :unprocessable_entity } 
    end 
    end 
    end 

    # PUT /products/1 
    # PUT /products/1.json 
    def update 
    @product = Product.find(params[:id]) 

    respond_to do |format| 
     if @product.update_attributes(params[:product]) 
     format.html { redirect_to @product, notice: 'Product was successfully updated.' } 
     format.json { head :ok } 
    else 
     format.html { render action: "edit" } 
     format.json { render json: @product.errors, status: :unprocessable_entity } 
     end 
    end 
    end 

    # DELETE /products/1 
    # DELETE /products/1.json 
    def destroy 
     @product = Product.find(params[:id]) 
     @product.destroy 

    respond_to do |format| 
     format.html { redirect_to products_url } 
     format.json { head :ok } 
    end 
    end 
    end 

我联系产品查看文件是标准的,这里是_form,新的索引文件:

New: 


    <%= render 'form' %> 

     <%= link_to 'Back', admin_products_path %> 

    _form: 
     <%= form_for [:admin, @product] do |f| %> 
     <% if @product.errors.any? %> 
     <div id="error_explanation"> 
      <h2><%= pluralize(@product.errors.count, "error") %> prohibited this product  from being saved:</h2> 

      <ul> 
      <% @product.errors.full_messages.each do |msg| %> 
      <li><%= msg %></li> 
      <% end %> 
      </ul> 
      </div> 
      <% end %> 

     <div class="field"> 
      <%= f.label :title %><br /> 
      <%= f.text_field :title %> 
     </div> 
     <div class="field"> 
      <%= f.label :description %><br /> 
      <%= f.text_area :description, rows: 6 %> 
      </div> 
      <div class="field"> 
      <%= f.label :image_url %><br /> 
      <%= f.text_field :image_url %> 
      </div> 
      <div class="field"> 
       <%= f.label :price %><br /> 
       <%= f.text_field :price %> 
      </div> 
      <div class="actions"> 
      <%= f.submit %> 
      </div> 
      <% end %> 

    Index: 
    <h1>Admin Listing products</h1> 
     <table> 
    <% @products.each do |product| %> 
    <tr class="<%= cycle('list_line_odd', 'list_line_even') %>"> 

    <td> 
     <%= image_tag(product.image_url, class: 'list_image') %> 
    </td> 

    <td class="list_description"> 
    <dl> 
     <dt><%= product.title %></dt> 
     <dd><%= truncate(strip_tags(product.description), 
       length: 80) %></dd> 
    </dl> 
    </td> 

    <td class="list_actions"> 
     <%= link_to 'Edit', edit_admin_product_path(product) %><br/> 
     <%= link_to 'Destroy', admin_product_path(product), 
      confirm: 'Are you sure?', 
      method: :delete %> 
    </td> 
    </tr> 
<% end %> 
</table> 
<br /> 
<%= link_to 'New product', new_admin_product_path %> 

好的我希望这是所有需要帮助我的信息。

这是一个问题:如果我去localhost:3000 /管理员/产品/新 我到形式来创建一个新产品。但是,如果我完成表单,它会将我带到以下localhost:3000/product /:id。我想它重定向到管理员/产品。

我一直告诉自己,它必须是在管理产品的控制器上的“创造”过程中的redirect_to的,但尝试了一切,这是不工作.....请帮它杀了我笑

回答

1

JUst重定向到您的索引操作,而不是显示产品。如果您还想让用户在更新产品时将其重定向到索引页,这也适用于您的更新操作。只需将redirect_to @product更改为redirect_to :action => 'index'即可。