Note that there are some explanatory texts on larger screens.

plurals
  1. PORedirecting from polymorphic association
    primarykey
    data
    text
    <p>I have a comments model that belongs to two models: submissions and posts</p> <pre><code>class Comment &lt; ActiveRecord::Base attr_accessible :content, :show belongs_to :commentable, :polymorphic =&gt; true end class Submission &lt; ActiveRecord::Base has_many :comments, :as =&gt; :commentable, :dependent =&gt; :destroy end </code></pre> <p>Submissions is a nested route and post is not.</p> <p>In my comments controller:</p> <pre><code> 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 =&gt; 'new' end end </code></pre> <p>find_contest</p> <pre><code>def find_contest @contest = Contest.find(params[:contest_id]) end </code></pre> <p>find_commentable:</p> <pre><code>def find_commentable params.each do |name, value| if name =~ /(.+)_id$/ return $1.classify.constantize.find(value) end end nil end </code></pre> <p>The redirect to post via @commentable works fine, but the redirect to submissions is not finding the contest. </p> <pre><code>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"=&gt;"✓", "authenticity_token"=&gt;"R62NH5/EE34FPapEqy7mfpa0wKz18GtSdhH8MGYq2Ec=", "comment"=&gt;{"content"=&gt;"test", "show"=&gt;"true"}, "commit"=&gt;"Create Comment", "submission_id"=&gt;"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' </code></pre> <p>Change to submission routes:</p> <pre><code> 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 </code></pre> <p>Submission form:</p> <pre><code>&lt;%= simple_form_for @submission, :html =&gt; { :multipart =&gt; true } do |f| %&gt; &lt;div class="span7 offset2 submission"&gt; &lt;fieldset class="well pleft80 edit"&gt; &lt;%= f.hidden_field :contest_id , :value =&gt; params[:contest_id] %&gt; &lt;%= f.input :title %&gt; &lt;%= f.input :description %&gt; &lt;%= f.input :comment_show, :as =&gt; :hidden, :input_html =&gt; { :value =&gt; true } %&gt; &lt;/fieldset&gt; &lt;fieldset class="well pleft80 noborder"&gt; &lt;%= f.fields_for :image do |img_field| %&gt; &lt;h3&gt;Upload Photo&lt;%= img_field.file_field :source %&gt;&lt;/h3&gt; &lt;% end %&gt; &lt;/fieldset&gt; &lt;div class ="form-actions pleft80"&gt; &lt;%= f.submit nil, :class =&gt; 'btn btn-primary btn-large' %&gt; &lt;/div&gt; &lt;/div&gt; &lt;% end %&gt; </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload