【发布时间】:2011-07-14 01:37:18
【问题描述】:
我正在尝试为 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 不会使用表单中的参数创建记录。有什么想法吗?
这些是方法和视图:
admin/sites#new
def new
@site = Site.new
respond_to do |format|
format.html # new.html.erb
end
end
admin/sites#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 %>
【问题讨论】:
标签: ruby-on-rails ruby-on-rails-3.1