Hi everyone,
My app consist of few models which are nested. However I understand the
concept of bad deep nested resources has it gets ugly url. My question
consist on how to defeat the nested resource and the best way to do it.
Here my routes
resources :article do
resources :picture
resources :comments do
resources :reports
end
end
resources :reports
My issue is with reports, this technically is just a link with a little
form to describe what the issue would be, and on the outside reports
would be for management to do index, etc… except new which his set by
user.
Now i can do two nested resources but after that i get confused. I am
wondering if their is a clear and easy way to remove this deep nested
resources, if so how, if not, a clear way of how to solve the issue at
the third layer.
Here my form (atm)
<%= form_for [@event, comment, current_customer.reports.new] do |f| %>
<%= f.text_area :body, :rows => 3, :value => “Brief Description” %>
<%= f.submit “Report it” %>
<% end %>
Here the controller reports ( note its actually called report_comments )
POST /report_comments
POST /report_comments.json
def create
@article = Article.find(params[:event_id])
@comment = @article.comments.find(params[:comment_id])
@report_comment =
@comment.report_comments.build(params[:report_comment])
respond_to do |format|
if @report_comment.save
format.html { redirect_to @report_comment, notice: ‘Report
comment was successfully created.’ }
format.json { render json: @report_comment, status: :created,
location: @report_comment }
else
format.html { render action: “new” }
format.json { render json: @report_comment.errors, status:
:unprocessable_entity }
end
end
end
Note the report would be listed under an article / comments and you
would be able to report that comments.
Any help or guidance is appreciated!