advice
advice

Reputation: 5988

Ruby on Rails - Render new on show

I have nested resources and I'm trying to show the new layout for the nested resource on the show of the parent.

resources :discussions do
    resources :comments
end

discussions\show.html.erb

<%= @discussion.title %>
<%= ... render the discussion %>
<%= ... render the existing comments %>
<% render 'comments/new' %>    <--- trying something like this

comments/new throws an error because it's missing the partial.
comments/form works to get past that, but throws an error saying my @comment is nil.

comments/_form.html.erb

undefined method discussion for nil:NilClass

<%= bootstrap_form_for([ @comment.discussion, @comment] ) do |f| %>

Do I have to change something in the controller, or am I going about this incorrectly?

Thanks for your help!

Upvotes: 0

Views: 302

Answers (1)

Hardik Upadhyay
Hardik Upadhyay

Reputation: 2659

try this

discussions\show.html.erb

<%= render 'comments/form', comment: @discussion.comments.build %>

comments/_form.html.erb

<%= bootstrap_form_for([ comment.discussion, comment] ) do |f| %>

Hope this will work.

Upvotes: 1

Related Questions