首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >管理产品路由

管理产品路由
EN

Stack Overflow用户
提问于 2011-09-26 19:46:36
回答 2查看 219关注 0票数 2

嗨,希望在这里得到一些答案。我想了一遍又一遍,这简直要了我的命。现在想要得到答案,我所发现的一切都比我目前的小系统复杂得多。下面是问题的信息。

第一个我的路由文件:

代码语言:javascript
复制
get 'admin' => 'admin#index'
  namespace "admin" do
  resources :products
end

我的管理产品控制器如下所示:

代码语言:javascript
复制
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,新的,索引文件:

代码语言:javascript
复制
    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/admin/products/new,我会进入表单来创建一个新产品。但是,如果我完成了表单,它会将我带到下面的localhost:3000/product/:id。我想要它到redirect_to管理/产品。

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

EN

回答 2

Stack Overflow用户

发布于 2011-09-29 21:09:15

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

票数 1
EN

Stack Overflow用户

发布于 2011-10-02 21:22:28

这并不起作用,但是这里是一个逐步的指南。http://icebergist.com/posts/restful-admin-namespaced-controller-using-scaffolding

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/7554586

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档