gpt4 book ai didi

ruby-on-rails - Rails 3 管理命名空间

转载 作者:行者123 更新时间:2023-12-04 05:34:42 25 4
gpt4 key购买 nike

我正在尝试为 cms 创建一个管理后端。

所以我有一个站点模型、一个 SitesController 和一个 Admin:SitesController。

在/app/views/sites/show.html 和/app/views/admin/sites/index.html、new.html 等中

routes.rb

  namespace :admin do 
resources :sites, :except => :show
end

match '/:slug' => 'sites#show'

编辑

日志输出

Started POST "/admin/sites" for 127.0.0.1 at 2011-07-13 19:03:12 +0200
Processing by Admin::SitesController#create as HTML
Parameters: {"utf8"=>"✓", "authenticity_token"=>"8IVTjl6ewasDuBEN6OgczTSdRfxSRPLmPodkrbZEKB8=", "site"=>{"title"=>"Test", "content"=>"abc", "slug"=>"home"}, "commit"=>"Create Site"}
SQL (1.1ms) INSERT INTO "sites" ("content", "created_at", "slug", "title", "updated_at") VALUES (?, ?, ?, ?, ?) [["content", nil], ["created_at", Wed, 13 Jul 2011 17:03:12 UTC +00:00], ["slug", nil], ["title", nil], ["updated_at", Wed, 13 Jul 2011 17:03:12 UTC +00:00]]
Redirected to http://localhost:3000/admin/sites
Completed 302 Found in 12ms

Rails 不会使用表单中的参数创建记录。有什么想法吗?

这些是方法和 View :

管理/站点#new

  def new
@site = Site.new

respond_to do |format|
format.html # new.html.erb
end
end

管理/站点#create

定义创建 @site = Site.new(params[:site])

respond_to do |format|
if @site.save
format.html { redirect_to admin_sites_path, notice: 'Site was successfully created.' }
else
format.html { render action: "new" }
end
end

admin/sites/_form

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

<ul>
<% @site.errors.full_messages.each do |msg| %>
<li><%= msg %></li>
<% end %>
</ul>
</div>
<% end %>
<div class="fields">
<%= f.label :title %><br/>
<%= f.text_field :title %>
</div>
<div class="fields">
<%= f.label :content %><br/>
<%= f.text_area :content %>
</div>
<div class="fields">
<%= f.label :slug %><br/>
<%= f.text_field :slug %>
</div>
<div class="actions">
<%= f.submit %>
</div>
<% end %>

最佳答案

您应该重定向到

 [:admin, :sites]

索引页

UPD

<%= form_for(@site, :url => [:admin, @site]) do |f| %>

关于ruby-on-rails - Rails 3 管理命名空间,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6681985/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com