2011-09-26 3 views
2

안녕하세요. 나는 이것을 계속해서 생각했다. 그것은 나를 죽이고있다. 이제 답을 얻고 싶습니다. 내가 찾던 모든 것이 순간적으로 복잡한 시스템입니다. 질문은 정보 아래에 있습니다.관리 제품 라우팅

먼저 내 루트 파일 다음 _form, 새로운 인덱스 파일을 여기에,

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 

내 관리 제품보기 파일이 표준입니다 : : 다음과 같이

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

내 관리 제품 컨트롤러입니다

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 수 있는지 자신을 계속 말하고 있지만, 모든 것을 시도하고 작동하지 않습니다 ..... 도와주세요 그것은

답변

1
롤 날 죽일 ​​것입니다

JUst는 제품을 표시하는 대신 색인 작업으로 리디렉션합니다. 이는 제품을 갱신 할 때 사용자가 색인 페이지로 재 지정되기를 원하는 경우 갱신 조치에도 적용됩니다. redirect_to @productredirect_to :action => 'index'으로 변경하십시오.