Multiple File Upload with Paperclip error - ruby-on-rails

I'm new to rails.
And to web development at all.
Sorry if some questions might seem dumb.
Trying to follow this screen cast - http://emersonlackey.com/screencasts/rails-3-with-paperclip.mov
But stopped at the problem - when i try to upload an image i get the following error :
ActiveRecord::UnknownAttributeError in PostsController#update
Unknown attribute: image
altough post_controller.rb seems ok (checked many times - it is the same as https://github.com/Emerson/Multiple-File-Uploads-with-Paperclip-and-Rails-3) :
Tried googlin of course, but didn't find anything.
Has anyone been trough this tutorial and had this problem ?

Problem fixed, the _form code, was incorrect!
I had to change:
<%= f.fields_for :assets do |asset| %>
to
<%= f.fields_for :assets do |asset_fields| %>
and
<%= asset.file_field :image %>
to
<%= asset_fields.file_field :asset %>
And it worked.
The reasons was quite silly, i just didn't the watch the screencast till the end, because I stopped at the middle - when the problem showed-up, and spent my whole attention googling for the solution.
Beginners mistake!

Posts model:
class Post < ActiveRecord::Base
attr_accessible :title, :content, :assets_attributes
has_many :assets
accepts_nested_attributes_for :assets, :allow_destroy => true
end
Assets model:
class Asset < ActiveRecord::Base
belongs_to :post
has_attached_file :asset, :styles => { :large => "640x480", :medium=>"300x300>",
:thumb => "100x100>" }
end
Post controller:
class PostsController < ApplicationController
def index
#posts = Post.all
end
def show
#post = Post.find(params[:id])
end
def new
#post = Post.new
5.times { #post.assets.build }
end
def create
#post = Post.new(params[:post])
if #post.save
redirect_to #post, :notice => "Successfully created post."
else
render :action => 'new'
end
end
def edit
#post = Post.find(params[:id])
5.times { #post.assets.build }
end
def update
#post = Post.find(params[:id])
if #post.update_attributes(params[:post])
redirect_to #post, :notice => "Successfully updated post."
else
render :action => 'edit'
end
end
def destroy
#post = Post.find(params[:id])
#post.destroy
redirect_to posts_url, :notice => "Successfully destroyed post."
end
end
_form:
<%= form_for #post, :html => { :multipart => true } do |f| %>
<%= f.error_messages %>
<p>
<%= f.label :title %><br />
<%= f.text_field :title %>
</p>
<p>
<%= f.label :content %><br />
<%= f.text_area :content %>
</p>
<%= f.fields_for :assets do |asset| %>
<% if asset.object.new_record? %>
<%= asset.file_field :image %>
<% end %>
<% end %>
<p><%= f.submit %></p>
<% end %>
Model index:
<% title "Posts" %>
<p><%= link_to "New Post", new_post_path %></p>
<hr />
<% for post in #posts %>
<div class="post">
<h2><%= link_to post.title, post%></h2>
<p class="content">
<%= post.content.html_safe %>
<br /><br />
<%= link_to "Edit", edit_post_path(post) %> | <%= link_to "Destroy", post,
:confirm => 'Are you sure?', :method => :delete %>
</p>
</div>
<% end %>
The error:
Started PUT "/posts/1" for 127.0.0.1 at Tue Sep 20 11:00:52 +0300 2011
Processing by PostsController#update as HTML
Parameters: {"commit"=>"Update Post", "post"=>{"title"=>"AAAAA", "content"=>"T
he enormous success of DropBox clearly shows that there's a huge need for simple
and fast file sharing.\r\n\r\nOur app will have the following features:\r\n\r\n
simple user authentication\r\n upload files and save them in Amazon S3\r\
n create folders and organize\r\n share folders with other users\r\n\r\nTh
roughout the tutorial, I will point out different ways that we can improve our a
pp. Along the way, we'll review a variety of concepts, including Rails scaffoldi
ng and AJAX.", "assets_attributes"=>{"0"=>{"image"=>#<ActionDispatch::Http::Uplo
adedFile:0x436fda0 #tempfile=#<File:C:/DOCUME~1/emve/LOCALS~1/Temp/RackMultipart
20110920-8900-zgz1ej-0>, #headers="Content-Disposition: form-data; name=\"post[a
ssets_attributes][0][image]\"; filename=\"02.jpg\"\r\nContent-Type: image/jpeg\r
\n", #content_type="image/jpeg", #original_filename="02.jpg">}}}, "authenticity_
token"=>"WHsbBak0O2xYBFe/h82+4/5aV2VPzHDdXcgb4QYmC4A=", "utf8"=>"Ō£ō", "id"=>"1"
}
←[1m←[35mPost Load (0.0ms)←[0m SELECT "posts".* FROM "posts" WHERE "posts"."i
d" = ? LIMIT 1 [["id", "1"]]
Completed 500 Internal Server Error in 63ms
ActiveRecord::UnknownAttributeError (unknown attribute: image):
app/controllers/posts_controller.rb:32:in `update'
Rendered C:/RailsInstaller/Ruby1.8.7/lib/ruby/gems/1.8/gems/actionpack-3.1.0/lib
/action_dispatch/middleware/templates/rescues/_trace.erb (0.0ms)
Rendered C:/RailsInstaller/Ruby1.8.7/lib/ruby/gems/1.8/gems/actionpack-3.1.0/lib
/action_dispatch/middleware/templates/rescues/_request_and_response.erb (0.0ms)
Rendered C:/RailsInstaller/Ruby1.8.7/lib/ruby/gems/1.8/gems/actionpack-3.1.0/lib
/action_dispatch/middleware/templates/rescues/diagnostics.erb within rescues/lay
out (46.9ms)

Related

Rails 6 not reporting hidden_values from form for multiple object construction

I'm trying to create a form that populates fields for each of the users pulled from a query, then submits all objects at once. With how my code is currently set up when the entire form submits only the non-hidden fields are sent to the controller.
<%= form_tag evaluations_path(method: :post) do |f| %>
<% Ingroup.where(group_id: Group.where(course_id: Project
.find(params[:project_id]).course_id)).each do |mem| %>
<h3>Evaluation for <%= "#{mem.user.Fname} #{mem.user.Lname}" %></h3>
<%= fields_for :evaluation do |form| %>
<% form.hidden_field :project_id, :value => params[:project_id] %>
<% form.hidden_field :user_id, :value => mem.user_id %>
<div class="field">
<%= form.label :score %>
<%= form.number_field :score %>
</div>
<div class="field">
<%= form.label :comment %>
<%= form.text_area :comment %>
</div>
<% end %>
<% end %>
<div class="actions">
<%= submit_tag "Submit" %>
</div>
<% end %>
EDIT: I added some additional code from the terminal and controller view.
EDIT 2: Removed the plural from evaluations and am still not seeing the hidden field values being passed.
Terminal Error:
Started POST "/evaluations?method=post" for 127.0.0.1 at 2020-12-02 21:55:08 -0500
Processing by EvaluationsController#create as HTML
Parameters: {"authenticity_token"=>"9pSgJJh3iE1doRy81Jhh3gHEgEJZt7pzcZw3C5EZeMEBh22VG8pmMKtHTwFml+Sj/XZmb3pBv6CmOLb9WvEVkQ==", "evaluation"=>{"score"=>"1", "name"=>"asdf"}, "commit"=>"Submit", "method"=>"post"}
(0.1ms) begin transaction
↳ app/controllers/evaluations_controller.rb:31:in `create'
Evaluation Exists? (0.2ms) SELECT 1 AS one FROM "evaluations" WHERE "evaluations"."user_id" IS NULL AND "evaluations"."project_id" IS NULL LIMIT ? [["LIMIT", 1]]
↳ app/controllers/evaluations_controller.rb:31:in `create'
(0.1ms) rollback transaction
↳ app/controllers/evaluations_controller.rb:31:in `create'
No template found for EvaluationsController#create, rendering head :no_content
Completed 204 No Content in 9ms (ActiveRecord: 0.3ms | Allocations: 5851)
Controller:
class EvaluationsController < ApplicationController
before_action :set_evaluation, only: [:show, :edit, :update, :destroy]
def new
#evaluation = Evaluation.new
end
def create
#evaluation = Evaluation.new(evaluation_params)
respond_to do |format|
if #evaluation.save
format.html { redirect_to #evaluation, notice: 'Evaluation was successfully created.' }
format.json { render :show, status: :created, location: #evaluation }
else
format.html { render :new }
format.json { render json: #evaluation.errors, status: :unprocessable_entity }
end
end
end
private
# Use callbacks to share common setup or constraints between actions.
def set_evaluation
#evaluation = Evaluation.find(params[:id])
end
# Only allow a list of trusted parameters through.
def evaluation_params
params.require(:evaluation).permit(:project_id, :user_id, :score, :name)
end
end
In your strong param says:
params.require(:evaluation).permit(:project_id, :user_id, :score, :name)
but in your view you have:
<%= fields_for :evaluations do |form| %>
Remove the final s for evaluations and it should work.
Related with your hidden fields, you're missing to use <%=. Try with:
<%= form.hidden_field :project_id, :value => params[:project_id] %>
<%= form.hidden_field :user_id, :value => mem.user_id %>

Rendering a nested partial template - to rended a nested form on a two separate models

I'm on week two of this issue and have recently used the railsCast #196 (revised). I know this is older - maybe that's my issue. As an extra spin I'm hosting my rails server off Cloud 9.
I've tried following a few different tutorials just to get one going & this is as far as I've gotten. The weird part is none of their syntex matches what the official ruby on rails documentation has ... Rails View templates.
In the railsCast the guy is able to get blank fields to show up ... I'm not sure how...so I haven't managed to populate the question or answer fields yet. I'm not even sure what the two rails console messages mean - besides there aren't records there to be had.
Thanks for reading & any suggestions!
-M
Without further ado, my senario ... nested forms via templates as shown in railsCast 196 ...
My rails console ...
2.2.1 :045 > cc = Survey.first.questions.first
Survey Load (0.5ms) SELECT "surveys".* FROM "surveys" ORDER BY "surveys"."id" ASC LIMIT 1
Question Load (0.2ms) SELECT "questions".* FROM "questions" WHERE "questions"."survey_id" = ? ORDER BY "questions"."id" ASC LIMIT 1 [["survey_id", 1]]
=> nil
2.2.1 :046 > cc = Survey.first.questions
Survey Load (0.3ms) SELECT "surveys".* FROM "surveys" ORDER BY "surveys"."id" ASC LIMIT 1
Question Load (0.2ms) SELECT "questions".* FROM "questions" WHERE "questions"."survey_id" = ? [["survey_id", 1]]
=> #<ActiveRecord::Associations::CollectionProxy []>
My terminal console log ...
Started GET "/surveys/5/edit" for 68.54.21.200 at 2015-11-27 02:46:48 +0000
Cannot render console from 68.54.21.200! Allowed networks: 127.0.0.1, ::1, 127.0.0.0/127.255.255.255
Processing by SurveysController#edit as HTML
Parameters: {"id"=>"5"}
Survey Load (0.4ms) SELECT "surveys".* FROM "surveys" WHERE "surveys"."id" = ? LIMIT 1 [["id", 5]]
Question Load (0.2ms) SELECT "questions".* FROM "questions" WHERE "questions"."survey_id" = ? [["survey_id", 5]]
Rendered surveys/_form.html.erb (4.2ms)
Rendered surveys/edit.html.erb within layouts/application (7.3ms)
Completed 200 OK in 70ms (Views: 67.9ms | ActiveRecord: 0.5ms)
So my code ...
surveys_controller.rb
class SurveysController < ApplicationController
def index
#surveys = Survey.all
end
def show
#survey = Survey.find(params[:id])
end
def new
#survey = Survey.new
3.times do
question = #survey.questions.build
4.times { question.answers.build }
end
end
def create
#survey = Survey.new(survey_params)
if #survey.save
flash[:notice] = "Successfully created survey."
redirect_to #survey
else
render :action => 'new'
end
end
def edit
#survey = Survey.find(params[:id])
end
def update
#survey = Survey.find(params[:id])
if #survey.update_attributes(params[:survey])
flash[:notice] = "Successfully updated survey."
redirect_to #survey
else
render :action => 'edit'
end
end
def destroy
#survey = Survey.find(params[:id])
#survey.destroy
flash[:notice] = "Successfully destroyed survey."
redirect_to surveys_url
end
private
def survey_params
params.required(:survey).permit(:id, :survey, :notice)
end
end
Edit action view
<% title = "Edit Survey" %>
<%= render 'form' %>
<p>
<%= link_to "Show", #survey %> |
<%= link_to "View All", surveys_path %>
</p>
_form.html.erb
<%= form_for(#survey) do |f| %>
<% if #survey.errors.any? %>
<div id="error_explanation">
<h2><%= pluralize(#survey.errors.count, "error") %> prohibited this survey from being saved:</h2>
<ul>
<% #survey.errors.full_messages.each do |message| %>
<li><%= message %></li>
<% end %>
</ul>
</div>
<% end %>
<div class="field">
<%= f.label :name %>
<%= f.text_field :name %>
</div>
<% f.fields_for :questions do |builder| %>
<%= render "question_fields", :f => builder %>
<% end %>
<p><%= f.submit "Submit" %></p>
<% end %>
_question_fields.html.erb
<p>
<%= f.label :content, "Question" %><br />
<%= f.text_area :content, :rows => 3 %><br />
<%= f.check_box :_destroy %>
<%= f.label :_destroy, "Remove Question" %>
</p>
<% f.fields_for :answers do |builder| %>
<%= render 'answer_fields', :f => builder %>
<% end %>
_answer_fields.html.erb
<p>
<%= f.label :content, "Answer" %>
<%= f.text_field :content %>
<%= f.check_box :_destroy %>
<%= f.label :_destroy, "Remove" %>
</p>
In each of the 7 projects I ran off the web...
It was the nesting of the array's within the params.require() that was the problem.. It's one thing to tell someone it has to be nested - it's another to show them the syntax when they are new :)
Example:
// Note this is from memory, as I deleted this version of the github..so it's not exactly right or tested...
params.require(:survey).permit(:id,:questions => [:id, :survey_id, :question, :answers => [:id, :question_id, :answer]])
Here's the break down of that same example in depth:
params.require(:survey).permit(
:id,
:questions => [:id, // This is the 1st nesting
:survey_id, :question, :answers => [:id, // This is 1st nested array ":questions"
:question_id, :answer] // End the 2nd nested array ":answers"
] // End the 2nd array ":questions"
) // End the ":surveys" array & the .permit as a whole

Rails ajax form for products

I've been working with app for pizzeria where customers could order pizzas through their website. I currently working with the product page where I try to submit products to shopping cart through ajax, but I'm really stuck. I haven't been able to build a shoppingcart which would accept product-id, product-size-id, extra-toppings as an array and quantity. I decided to try to go with session-store where all the order-row ids are stored and on menu page every product has a form where user could add product, size and quantity to shoppingcart but I keep getting this error in server logs:
Started POST "/order_row" for ::1 at 2015-08-03 11:18:21 +0300
Processing by OrderRowsController#create as JS
Parameters: {"utf8"=>"✓", "order_row"=>{"product"=>"1", "size"=>"0", "quantity"=>"2"}, "commit"=>"Tilaa"}
Completed 500 Internal Server Error in 2ms (ActiveRecord: 0.0ms)
ActiveRecord::AssociationTypeMismatch (Product(#70158072501800) expected, got String(#70158039566200)):
app/controllers/order_rows_controller.rb:4:in `create'
I have models Product, ProductCategory, Order, OrderRow and my session stores order-row-ids as mentioned. My menu page is actually product_categories#show -view where products belonging to that category are listed.
#order_rows_controller.rb
class OrderRowsController < ApplicationController
respond_to :html, :js
def create
#orow = OrderRow.new(order_rows_params)
if #orow.save
session[:order_row_ids] << #orow.id
flash[:notice] = "Lisättiin ostoskoriin!"
else
flash[:error] = "Tuotteen lisääminen ostoskoriin epäonnistui."
redirect :back
end
end
def update
#orow = OrderRow.find(params[:id])
if #orow.update_attributes(params[:order_row])
flash[:notice] = "Ostoskori päivitetty."
else
flash[:error] = "Ostoskorin päivitys epäonnistui."
end
end
def destroy
#orow.find(params[:id]).destroy
flash[:notice] = "Tuote poistettu onnistuneesti"
end
private
def order_rows_params
params.require(:order_row).permit(:product, :size, :quantity) #, :extras => []
end
end
ProductCategories-controller
class ProductCategoriesController < ApplicationController
before_action :set_product_category, only: [:edit, :update, :destroy]
respond_to :html, :js
def index
#product_categories = ProductCategory.all
end
def show
#product_category = ProductCategory.friendly.find(params[:id])
#product_categories = ProductCategory.all
#products = #product_category.products
#order_row = OrderRow.new(order: nil, product: nil, size: nil, extras: nil, quantity: nil)
end
And menu-page in product_categories/show.html.erb
#product_categories#show -view
<!--- category descriptions -->
<div class="container">
<% #products.each do |product| %>
<div class="col-sm-6 col-md-4">
<div class="product well">
<h3><%= product.name %></h3>
<span><%= product.description %></span>
<p class="prices">
<%= price(product.normal_price) %> | <%= price(product.plus_size_price) %> | <%= price(product.lunch_price) %>
</p>
<br>
<div id="form-<%= product.id %>">
<%= simple_form_for #order_row, :url => url_for(:controller => 'order_rows', :action => 'create'), remote: true do |f| %>
<%= f.hidden_field :product, :value => product.id %>
<h5>Koko</h5>
<div style="padding-left: 13px">
<%= f.input :size, collection: OrderRow.sizes, as: :radio_buttons, label: false, item_label_class: "radio-inline", item_wrapper_tag: false %>
</div>
<h5>Määrä</h5>
<div style="width: 8%; padding-left: 13px;">
<%= f.input :quantity, as: :string, label: false %>
</div>
<p>
<%= f.submit "Tilaa", class: "btn btn-success btn-lg" %>
</p>
<% end %>
</div>
</div>
</div>
<% end %>
</div>
Create.js.erb in order_rows#create action
#create.js.erb
$("#form-<%= params[:product] %>").load(document.URL + "#form-<%= params[:product]");
Associations:
#order_row
belongs_to :order
belongs_to :product
#product
belongs_to :product_category
has_one :campaign_producte
belongs_to :dish_type
#product_categories
has_many :products
has_many :campaign_products
has_many :product_extras
has_many :dish_types, through: :products
#product_extra
belongs_to :product_category
Link to github-repo: https://github.com/casualCodeAndDesign/ravintolamammamia
What's the reason for this server error and why it doesn't store my order_row to the database?
ActiveRecord::AssociationTypeMismatch (Product(#70158072501800)
expected, got String(#70158039566200))
You need to change
<%= f.hidden_field :product, :value => product.id %>
to
<%= f.hidden_field :product_id, :value => product.id %>
and product to product_id in create.js.erb and order_rows_params

Remove Paperclip Attachment from Nested Attribute (Cocoon)

I have an INCIDENT with an attached WITNESS.
I am trying to show a link to remove an attachment from a nested attribute, but my link is pulling the :id of the parent record (invoice.id) instead of the nested/child record (invoice.witness_id).
I know I'm doing something wrong in my routes or in calling the correct id number from the controller or view... any help is appreciated!
incident.rb
has_many :witnesses
accepts_nested_attributes_for :witnesses, :reject_if => :all_blank, :allow_destroy => true
witness.rb
belongs_to :incident
has_attached_file :statement
routes.rb
match 'witness/:id' => 'witnesses#remove_statement', via: [:get, :post], as: 'remove_statement'
witnesses_controller
def index
#witnesses = #incident.witnesses.all
end
def remove_statement
#witness = Witness.find(params[:id])
#witness.statement = nil
respond_to do |format|
if #witness.save
format.html { redirect_to :back, notice: 'Attachment was removed.' }
format.json { head :no_content }
else
format.html { redirect_to :back, error: 'Attachment could not be removed.' }
format.json { render json: #witness.errors, status: :unprocessable_entity }
end
end
end
private
def set_witness
#witness = #incident.witnesses.find(params[:id])
end
def witness_params
params[:witness].permit(:first_name, :last_name, :phone, :email, :statement, :incident_id)
end
_witness_fields partial
<div class="nested-fields">
<div class="form-group">
....
<%= link_to "Remove Attachment", remove_statement_path, :id => :witness_id %>
...
incidents/_form.html.erb
<%= form_for(#incident, html: { :multipart => true , class: 'form-horizontal' }) do |f| %>
<%= f.error_notification %>
<% if #incident.errors.any? %>
<div class="red">
<% #incident.errors.full_messages.each do |msg| %>
<%= msg %><hr>
<% end %>
</div>
<% end %>
.....
<!-- WITNESS SECTION -->
<div class="span6">
<hr>
<fieldset id="witnesses">
<%= f.fields_for :witnesses do |builder| %>
<%= render 'witness_fields', :f => builder %>
<% end %>
</fieldset>
<p class="links">
<%= link_to_add_association 'Add Witness/Contact', f, :witnesses, { class:"btn btn-primary" } %>
</p>
</div>
</div>
<!-- END WITNESSES SECTION -->
.....
In your _withness_fields partial, you write
<%= link_to "Remove Attachment", remove_statement_path, :id => :witness_id %>
That should be something like
<%= link_to "Remove Attachment", remove_statement_path(f.object.id) %>
So two things: the path helper remove_statement_path needs the id as a parameter, and secondly, you need to actually give it the correct id of the object for which you are currently rendering.
Please note, since you dynamically add these, for new records this will not be valid (since there is no idea).
So you will have to check if the record is a new_record? and only show that link if it is not (because then you will have a valid id). If it is not a new record, you can just use the cocoon helper to remove it.

Redirecting from polymorphic association

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 %>
You don't need to instantiate or classify anything.
redirect_to #comment.commentable
If you can't do that then you will need to build a global helper module for it and include that into the controller.
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
And include it within the ApplicationController:
include RouteHelpers
Then you can call comment_association_redirect_to anywhere in your app (controllers and so on).
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.

Resources