1

我有一个评论模型,它属于两个模型:提交和帖子

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

Submissions 是一个嵌套路由,而 post 不是。

在我的评论控制器中:

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

2 回答 2

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在你的应用程序中的任何地方调用(控制器等)。

于 2012-12-01T16:53:45.370 回答
0

我从应用程序中剥离了嵌套路由,现在它工作正常并且更简单。当视图必须关联依赖项时,我不确定我是否能找到使用嵌套路由的充分理由。

于 2012-12-01T19:01:12.370 回答