从头开始的 Rails 脚手架也有其缺点
第一个问题:
我正在尝试使创建方法起作用,但似乎我的表单不恰当地将数据发送到我的数据库。这是 server.log :
Processing Admin::AdminWysisController#create (for ::1 at 2010-06-22 13:43:58) [POST]
Parameters: {"commit"=>"save", "action"=>"create", "authenticity_token"=>"P8pW7GnSNr7RZcxFcejpfsu9YCX7z8hO0DJPH3rYalQ=", "controller"=>"admin/admin_wysis", "admin_wysi"=>{"post_published"=>"<p>\r\n\tstinky</p>\r\n"}}
User Columns (2.0ms) SHOW FIELDS FROM `users`
User Load (0.3ms) SELECT * FROM `users` WHERE (`users`.`id` = '1') LIMIT 1
SQL (0.1ms) BEGIN
User Update (0.2ms) UPDATE `users` SET `updated_at` = '2010-06-22 17:43:58', `perishable_token` = 'XMxt1yi5_4JEwPP-21GO', `last_request_at` = '2010-06-22 17:43:58' WHERE `id` = 1
SQL (0.4ms) COMMIT
AdminWysi Columns (1.0ms) SHOW FIELDS FROM `admin_wysis`
SQL (0.1ms) BEGIN
AdminWysi Create (0.1ms) INSERT INTO `admin_wysis` (`post_published`) VALUES(NULL)
SQL (0.4ms) COMMIT
Redirected to http://localhost:3000/admin/admin_wysi
Completed in 139ms (DB: 5) | 302 Found [http://localhost/admin/admin_wysi]
SQL (0.1ms) SET NAMES 'utf8'
SQL (0.1ms) SET SQL_AUTO_IS_NULL=0
该语句中最重要的部分是这个:
"admin_wysi"=>{"post_published"=>"<p>\r\n\tasdfasdfasd</p>\r\n"}
这应该很简单:
"post_published"=> "<p>\r\n\tasdfasdfasd</p>\r\n"
让我们看看我的视图..
- form_for @admin_wysi, :url => admin_admin_wysi_path do |f|
= f.error_messages :header_message => FORM_ERROR_HEADER_MESSAGE, :message => FORM_ERROR_MESSAGE
= f.cktext_area :post_published, :cols => '70', :rows => '30', :width => '555px', :height => '240px', :toolbar => 'HQ'
%br/
.grid_2.prefix_5.align_right
= f.submit 'save', {:class => 'button'}
这是我的控制器:D
def new
@admin_wysi = AdminWysi.new
end
def create
@admin_wysi = AdminWysi.new(params[:admin_wysi])
if @admin_wysi.save
redirect_to admin_admin_wysi_path
end
end
知道什么可能会困扰这个吗?
第二个问题
奇怪的是,当它保存时,它会将我重定向到 website.com/wysi 而不是 website.com/wysis/ 。后一个是工作链接,前一个不是真正的链接。
我的routes.rb看起来像这样:
map.namespace :admin do |admin|
admin.resource :admin_wysi
end
1st Question:
I'm trying to make the create method work, but it appears that my form is sending data innapropriately to my DB. This is the server.log :
Processing Admin::AdminWysisController#create (for ::1 at 2010-06-22 13:43:58) [POST]
Parameters: {"commit"=>"save", "action"=>"create", "authenticity_token"=>"P8pW7GnSNr7RZcxFcejpfsu9YCX7z8hO0DJPH3rYalQ=", "controller"=>"admin/admin_wysis", "admin_wysi"=>{"post_published"=>"<p>\r\n\tstinky</p>\r\n"}}
User Columns (2.0ms) SHOW FIELDS FROM `users`
User Load (0.3ms) SELECT * FROM `users` WHERE (`users`.`id` = '1') LIMIT 1
SQL (0.1ms) BEGIN
User Update (0.2ms) UPDATE `users` SET `updated_at` = '2010-06-22 17:43:58', `perishable_token` = 'XMxt1yi5_4JEwPP-21GO', `last_request_at` = '2010-06-22 17:43:58' WHERE `id` = 1
SQL (0.4ms) COMMIT
AdminWysi Columns (1.0ms) SHOW FIELDS FROM `admin_wysis`
SQL (0.1ms) BEGIN
AdminWysi Create (0.1ms) INSERT INTO `admin_wysis` (`post_published`) VALUES(NULL)
SQL (0.4ms) COMMIT
Redirected to http://localhost:3000/admin/admin_wysi
Completed in 139ms (DB: 5) | 302 Found [http://localhost/admin/admin_wysi]
SQL (0.1ms) SET NAMES 'utf8'
SQL (0.1ms) SET SQL_AUTO_IS_NULL=0
The most important part of that statement is this one :
"admin_wysi"=>{"post_published"=>"<p>\r\n\tasdfasdfasd</p>\r\n"}
Which should be just simply :
"post_published"=> "<p>\r\n\tasdfasdfasd</p>\r\n"
Let's look at my view..
- form_for @admin_wysi, :url => admin_admin_wysi_path do |f|
= f.error_messages :header_message => FORM_ERROR_HEADER_MESSAGE, :message => FORM_ERROR_MESSAGE
= f.cktext_area :post_published, :cols => '70', :rows => '30', :width => '555px', :height => '240px', :toolbar => 'HQ'
%br/
.grid_2.prefix_5.align_right
= f.submit 'save', {:class => 'button'}
Here is my controller :D
def new
@admin_wysi = AdminWysi.new
end
def create
@admin_wysi = AdminWysi.new(params[:admin_wysi])
if @admin_wysi.save
redirect_to admin_admin_wysi_path
end
end
Any idea what might be bugging this?
2nd Question
Strange as it is, when it saves, it redirects me to website.com/wysi NOT website.com/wysis/ . The latter one is a working link, the prior is not a real link.
My routes.rb looks like this :
map.namespace :admin do |admin|
admin.resource :admin_wysi
end
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您获得的参数是 Rails 应用程序中所期望的。否则,就没有好方法只访问作为要保存的对象一部分的参数(例如,丢弃
commit
、action
等键) /值对)。您从创建操作中收到哪些错误?如果它正在重定向您,则似乎在保存时可能不会出现错误。至于您的路由问题,您得到单数 (wysi) 而不是复数 (wysis) 的原因是您使用的是
map.resource
而不是map.resources
。如果你改变它,我相信它应该解决路由问题。The params you're getting are what's expected in a Rails app. Otherwise, there would be no good way to access only the parameters that are part of the object to the save (for instance, to discard the
commit
,action
, etc. key/value pairs). What errors are you getting from the create action? If it's redirecting you, it seems like it is probably not getting errors while saving.As to your routing problem, the reason you're getting the singular (wysi) rather than plural (wysis) is that you're using
map.resource
instead ofmap.resources
. If you change that, I believe it should fix the routing problem.