0

I am trying to make an app in Rails 4.

My profiles form allows users to make a profile. A section of the profile is for their qualifications.

The associations are:

profile.rb

has_many :qualifications  
    accepts_nested_attributes_for :qualifications,  reject_if: :all_blank, allow_destroy: true

qualifications rb

  belongs_to :profile

My profiles view folder then includes a partial to show that profile's qualifications. The show partial has:

<% Qualification.pending.order(year_earned: :asc).each do |qualification| %>
            <div class="row">

                      <div class="col-md-12">
                        <div class="profilequalifications">
                            <%= qualification.current_study  %>

                        </div>  
                      </div>    
            </div>

          <% end %>

My qualification model has a scope as:

  scope :pending, -> { where(pending: true) }

The problem is, each profile lists all the qualifications. How do I limit this to each profile just listing its own qualifications.

I've tried adding 'Profile' / '@profile' to the view partial in various places, but those attempts just produce errors.

Is there an example for how to do this. I thought rails might know how to do this given the view form for qualifications is nested inside the profiles form.

TAKING TIM'S SUGGESTION

I change the qualifications partial show to:

<% profile.qualifications.pending.order(year_earned: :asc).each do |qualification| %>
            <div class="row">

                      <div class="col-md-12">
                        <div class="profilequalifications">
                            <%= qualification.current_study  %>

                        </div>  
                      </div>    
            </div>

And i change the profiles show page to:

<%= render :partial => 'profiles/qualifications', profile: @profile %>

I get this error:

undefined local variable or method `profile' for #<#:0x007f97ba143030>

Mel
  • 2,481
  • 26
  • 113
  • 273
  • If you are going to use render :partial format, then you need to use locals to wrap the profile e.g. render partial: 'some/path/to/my/partial', locals: { custom_var: 'Hello' } – Tim Jan 10 '16 at 02:24
  • Hi Tim, I'm afraid I don't know what that means. I added the bit after the comma on your suggestion below. Really not sure what the path should be. Definitely not following you in the reference to Hello. Sorry - you've lost me. – Mel Jan 10 '16 at 02:51
  • OK, what I was trying to point out is that if you use render :partial as the way to call your partial, then you have to wrap your variables (the profile: @profile bit) with " locals: { ... } ". note I did not use render :partial, just render, see http://stackoverflow.com/questions/4402556/rails-confused-about-syntax-for-passing-locals-to-partials – Tim Jan 10 '16 at 03:04
  • OK. Well, <%= render 'profiles/qualifications', profile: @profile %> still lists all qualifications for all users. I'll try to get a grip on what the link you sent is wanting. I really don't understand coding terminology. I've no idea what a local is or does. I'll try and do some research. Thanks for the steer – Mel Jan 10 '16 at 03:09
  • Wait - i updated to copy your style in th view again. seems to work now – Mel Jan 10 '16 at 03:15
  • Cool, sorry I was not clearer, I'm on a tablet and its painful to copy and paste – Tim Jan 10 '16 at 03:16

1 Answers1

0

I think you need to pass your profile to the partial, so:

<%= render 'your_partial', profile: @profile %>

Then you can use it in the partial, e.g.

<% profile.qualifications.each do |q| %>
  <%= q.name %>
<% end %>
Tim
  • 1,005
  • 3
  • 10
  • 21
  • Hi Tim, thanks. I tried this, but it doesnt work. It gives this error: <% Profile.qualification.pending.order(year_earned: :asc).each do |qualification| %> undefined method `qualification' for # – Mel Jan 10 '16 at 00:56
  • Ah.. I'd made an assumption that you might have a has_many :qualifications in your profile model. How do you link profiles and qualifications? – Tim Jan 10 '16 at 01:03
  • Also it should be profile, the variable, not Profile, the class, and if you have qualifications as a has_many then you need to change your code from singular to plural – Tim Jan 10 '16 at 01:11
  • associations added above – Mel Jan 10 '16 at 01:23
  • OK, see my comment just above yours. I think it should be profile.qualifications not Profile.qualification - its case sensitive, and plurals matter – Tim Jan 10 '16 at 01:25