Rendering a partial for a form that is nested within two resources

So I've gone a little over my head again, but it's an interesting situation that I think others may find themselves in at some point. In the YelpDemo, we create a restaurant, and comments are nested into that resource. Well, I've tried to nest another resource between those two resources. I've tried a lot of different things, but can't seem to get it to work out to the right route. I've added everything to stackoverflow because it's easier to read the code. You can go over and see this question at: http://stackoverflow.com/questions/26154379/rails-4-form-for-double-nested-comments Thanks! Hope this can help someone else too.

Posted about 5 years ago by Nate
Posted about 5 years ago by Alex Yang

Tom Kadwill's answer on StackOverflow is right on point. Basically, you're seeing this error because you haven't properly defined the variables you want to use. Typically, this just happens in your controller but it's a little more complicated here because you're using partials.

Here's what you need to know:
1) Define the variables you need in your controller. So if you're going to need @bark at some point (whether it's in your View page or in the partial that's called from your View page), it has to be defined in your controller first.
2) Redefine the variables you'll need when you call the partial. So if @bark has already been defined in your controller, that variable will work in your View page but NOT in the partial that's called from your View page. To make sure the variable works in the partial, you have to define it a second time when you call the partial. For example, <%= render partial: "form", locals: {bark: @bark} %>. This creates a variable 'bark' that you can use in the partial code whose value is the same as the '@bark' variable you defined in your controller.

I hope this solves the issue and helps you to better understand how variables are defined in Rails!

0
Posted about 5 years ago by Nate

Yep! That was it. I've been learning more about partials, and getting the right variables they need has been a constant problem. Thanks :)

Now, there's a new problem. The form is being generated multiple times. I would imagine it's because form_for ... each do |f| is iterating over each of the id's for the already submitted comments.

Any idea how to just put the input field one time?

0
Posted about 5 years ago by Alex Yang

You'll see one form for each time you're calling the form partial. I would check to see how many times you're calling the partial - does it match with the number of forms you're seeing?

0
Posted about 5 years ago by Nate

I thought so too. It's really strange. It seems to be the number of comments that affects the number of forms. If I add a new comment, the form number increases one. To test, I pasted the render partial code again. It doubled the number of forms on the page. I'm not sure why it's doing this, but it doesn't seem to be related to the partial being called.

0
Posted about 5 years ago by Alex Yang

Yeah I'd suggest walking through your code step-by-step so you understand what's being called and when. Since you have multiple 'render' statements, it sounds like you just need to map out the flow and figure it out.

0
Posted about 5 years ago by Nate

The problem was I was just throwing random things I found online at the problem.

I took out the :collection hash in the code below, and the form is showing fine now.

<%= render partial: '/comments/simpleComment', locals: { bark: bark}, :collection => bark.comments %>

0
Posted about 5 years ago by Alex Yang

Cool - glad you figured it out!

0