您希望在这里得到一些答案。我一遍又一遍地想到这一点,它正在杀了我。想要现在得到答案,我所发现的一切都比我目前的litle系统更复杂。问题如下。
首先是我的路线档案:
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,索引文件:
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,但是尝试了一切并且它不起作用.....请帮助它是杀了我哈哈
答案 0 :(得分:1)
JUst重定向到您的索引操作,而不是显示产品。这也适用于您的更新操作,如果他们还希望用户在更新产品时被重定向到索引页面。只需将redirect_to @product
更改为redirect_to :action => 'index'
。
答案 1 :(得分:0)