从多态关联重定向

时间:2012-12-01 02:36:33

标签: 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 => :destroy
end

提交是嵌套路线,而帖子不是。

在我的评论控制器中:

  def create
    @commentable = find_commentable
    @comment = @commentable.comments.build(params[:comment])
    @comment.user = current_user
    if @comment.save
      #CommentMailer.comment_email(@user, @comment, @commentable).deliver
      flash[:notice] = "Successfully created comment."
      if @commentable == @submission
        redirect_to [@contest, @commentable]
      else
      redirect_to [@commentable]
      end
    else
     render :action => 'new'
    end
  end

find_contest

def find_contest
    @contest = Contest.find(params[:contest_id])
end

find_commentable:

def find_commentable
  params.each do |name, value|
    if name =~ /(.+)_id$/
      return $1.classify.constantize.find(value)
    end
  end
  nil
end

通过@commentable发布的重定向工作正常,但重定向到提交是没有找到比赛。

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 %>

2 个答案:

答案 0 :(得分:2)

您无需实例化或分类任何内容。

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

并将其包含在ApplicationController

include RouteHelpers

然后,您可以在应用中的任何位置呼叫comment_association_redirect_to(控制器等)。

答案 1 :(得分:0)

我从应用程序中删除了嵌套路由,现在它工作正常,而且更简单。当视图必须与依赖关系相关时,我不确定是否可以想出使用嵌套路由的充分理由。