Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8949679
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T13:16:27+00:00 2026-06-15T13:16:27+00:00

I have a comments model that belongs to two models: submissions and posts class

  • 0

I have a comments model that belongs to two models: submissions and posts

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 is a nested route and post is not.

In my comments controller:

  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

The redirect to post via @commentable works fine, but the redirect to submissions is not finding the contest.

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'

Change to submission routes:

 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

Submission form:

<%= 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 %>
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-15T13:16:28+00:00Added an answer on June 15, 2026 at 1:16 pm

    I stripped the nested routing out of the app and now it works fine and it’s much simpler. Not sure I can think of a good reason to use nested routing when the views must relate the dependencies.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have two models, post and comment. Posts has many comments and comments belongs
Let's say I have two models like so: class Comment < ActiveRecord::Base belongs_to :user
I have two models, Article and Post that both inherit from a base model
Okay i have two models: posts and comments. as you can think comments has
I have the following model class Comment extends AppModel { var $useTable = comments;
I am in rails 3.1. I have the following models class Tool < ActiveRecord::Base
Given models User and Comments being populated such that a user may have many
Let's say I have 3 models (Note, Comment, Article) that I want to search
Assuming I have a comments model and a posts model, What code can I
I have a content model and a comment model. On insertion of comment I

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.