我的表单如下:
<%= form_for [:admin, @post] do |f|%>
<div style="width:660px;">
<%= f.text_field :title, :size => 150 %>
<br/>
<%= f.text_area :body, :id => "body", :rows => 15 %>
<br/>
<%= f.submit %>
</div>
<% end %>
网址目前是:
http://localhost:3000/admin/posts/21/edit
管理员帖子编辑的佣金路线是:
edit_admin_post GET /admin/posts/:id/edit(.:format)
由于某种原因,edit_admin_post_path正在返回:
/admin/post/the-post-title/edit
所以我手动将帖子标题更改为id。
当我执行更新时,我重定向:
if @post.update_attributes(params[:post])
redirect_to edit_admin_post_path @post
end
但是再次使用&#39;标题后重定向&#39;而不是id。
为什么会这样?
这是rails 3
注:
对于show url,我想要/ post / my-post-title而不是/ post / 234所以我不确定我在哪里更改了b / c我看不到它的参考代码!
答案 0 :(得分:5)
听起来你的帖子模型中定义了这些内容:
class Post < ActiveRecord::Base
def to_param
#{name}"
end
这将导致它只返回名称而不是ID。删除您在帖子模型中定义的所有to_params,看看是否能解决它。
将其更改为:
def to_param
"#{id}-#{name}".downcase.gsub(/\W+/, "-").gsub(/^[-]+|[-]$/,"").strip
end
这将为您提供相当干净的URL,例如:http://localhost:3000/admin/posts/21-my-post-title/edit和Post.find(21-my-post-title)的工作原理与Post.find(21)相同。