0

I am using Rails 4 for first time. Documentation says you can pass an instance variable to a partial:

<%= render partial: "customer", object: @new_customer %>

So I try to do the same. I am using fullcalendar engine and I render index.html.erb and inside that I render form partial. I pass @record to partial. I know the record is not nil because I added inspect_record helper to inspect that the @record does exist:

<%= render "form", :object => @record  %>
<%= inspect_record @record %>

However, in _form.html.erb partial, the @record is nil, and this condition returns false:

<% unless @record.nil? %>

Note I also tried this:

<%= render 'form', locals: {record: @record} %>

What might I be doing wrong?

3
  • 2
    instance variables like @this_one defined in the controller's action should be available between the action's view and all partial views used. Commented Aug 21, 2014 at 18:43
  • 1
    Let us see the controller action where you populate @record Commented Aug 21, 2014 at 18:47
  • @JTG the issue was that the form was being rendered via an ajax call to a new action, not the index action. Commented Aug 21, 2014 at 20:12

1 Answer 1

1

If you are using:

<%= render 'form', locals: {record: @record} %>

then, you will need to use variable record instead of @record in your _form.html.erb as:

<% unless record.nil? %>

Description:

locals: {record: @record} means, @record's value will be accessible by using record in the partial being rendered.

Sign up to request clarification or add additional context in comments.

5 Comments

Dan, if this one won't help, thought it should, try adding word partial, <%= render partial: 'form' .... explicitly saying about partial.
@IvanShamatov good point, however, the partial is getting called successfully.
got this issue couple of times, when not adding 'partial' word affected on record, but it might be because of collection partial, or something like that, I cant remember actually.
@IvanShamatov totally agree with you... I also have encountered this issue sometimes..
the issue was that the form was being rendered via an ajax call to a new action, not the index action. The new action does not have an existing @record with an id.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.