Ruby on rails 从多态关联重定向
我有一个评论模型,它属于两个模型:提交和发布Ruby on rails 从多态关联重定向,ruby-on-rails,redirect,Ruby On Rails,Redirect,我有一个评论模型,它属于两个模型:提交和发布 class Comment < ActiveRecord::Base attr_accessible :content, :show belongs_to :commentable, :polymorphic => true end class Submission < ActiveRecord::Base has_many :comments, :as => :commentable, :dependent
class Comment < ActiveRecord::Base
attr_accessible :content, :show
belongs_to :commentable, :polymorphic => true
end
class Submission < ActiveRecord::Base
has_many :comments, :as => :commentable, :dependent => :destroy
end
寻觅比赛
def find_contest
@contest = Contest.find(params[:contest_id])
end
查找可评论的内容:
def find_commentable
params.each do |name, value|
if name =~ /(.+)_id$/
return $1.classify.constantize.find(value)
end
end
nil
end
通过@commentable重定向到post可以正常工作,但是重定向到提交的内容找不到比赛
Started POST "/submissions/36/comments" for 127.0.0.1 at 2012-11-30 18:34:41 -0800
Processing by CommentsController#create as HTML
Parameters: {"utf8"=>"✓", "authenticity_token"=>"R62NH5/EE34FPapEqy7mfpa0wKz18GtSdhH8MGYq2Ec=", "comment"=>{"content"=>"test", "show"=>"true"}, "commit"=>"Create Comment", "submission_id"=>"36"}
User Load (0.4ms) SELECT "users".* FROM "users" WHERE "users"."id" = 2 ORDER BY users.created_at DESC LIMIT 1
Submission Load (0.3ms) SELECT "submissions".* FROM "submissions" WHERE "submissions"."id" = $1 ORDER BY submissions.created_at DESC LIMIT 1 [["id", "36"]]
Completed 500 Internal Server Error in 116ms
ActiveRecord::RecordNotFound (Couldn't find Contest without an ID):
app/controllers/comments_controller.rb:19:in `create'
更改提交路线:
submissions GET /submissions(.:format) submissions#index
POST /submissions(.:format) submissions#create
new_submission GET /submissions/new(.:format) submissions#new
edit_submission GET /submissions/:id/edit(.:format) submissions#edit
submission GET /submissions/:id(.:format) submissions#show
PUT /submissions/:id(.:format) submissions#update
DELETE /submissions/:id(.:format) submissions#destroy
提交表格:
<%= simple_form_for @submission, :html => { :multipart => true } do |f| %>
<div class="span7 offset2 submission">
<fieldset class="well pleft80 edit">
<%= f.hidden_field :contest_id , :value => params[:contest_id] %>
<%= f.input :title %>
<%= f.input :description %>
<%= f.input :comment_show, :as => :hidden, :input_html => { :value => true } %>
</fieldset>
<fieldset class="well pleft80 noborder">
<%= f.fields_for :image do |img_field| %>
<h3>Upload Photo<%= img_field.file_field :source %></h3>
<% end %>
</fieldset>
<div class ="form-actions pleft80">
<%= f.submit nil, :class => 'btn btn-primary btn-large' %>
</div>
</div>
<% end %>
{:multipart=>true}do | f |%>
参数[:竞赛id]>
:hidden,:input_html=>{:value=>true}%>
上传照片
“btn btn主btn大”%>
您不需要实例化或分类任何内容
redirect_to @comment.commentable
如果您不能这样做,那么您需要为它构建一个全局帮助器模块,并将其包含到控制器中
module RouteHelpers
def comment_association_redirect_to(comment)
item = comment.commentable
case item.class.to_s
when 'Submission'
redirect_to submission_path(item)
end
end
end
并将其包含在应用程序控制器中
:
include RouteHelpers
然后你可以调用
comment\u association\u redirect\u到应用程序中的任何位置(控制器等)。我将嵌套路由从应用程序中剥离出来,现在它工作正常,更简单。当视图必须关联依赖项时,我想不出使用嵌套路由的好理由。你能发布rake routes
的输出部分,其中包括contest/commentable
的路由规范,以便我们确认Rails认为应该如何路由吗?@normalocity我添加了模型问题顶部的信息,应该澄清路由是如何生成的。好吧,你可能是对的,但是当谈到Rails如何实际解释路由时,rake routes
才是事实,所以我宁愿看到输出。模型信息确实告诉了我这些关系,但是路由可以被覆盖、非默认,或者不是您所期望的,这就是为什么您应该检查rake routes
,以获取此信息。可评论的路由不会显示在rake routes中,但我已经添加了来自rake路由的相关路由(在问题的底部)@contest
变量(在重定向行中)是否设置为零?错误找不到没有ID的竞赛
可能表示@Contest
为零,或者未设置为您期望的值。
include RouteHelpers