0

I am trying to make an app in Rails 4. I'm trying to figure out how to show associated attributes.

My objective is to have educators create projects. Students can then express interest in them. Once the project is created, the projects show includes a link to a form where students can express interest in participating, as follows:

          <%= link_to  'Join this project', project_student_eoi_path %>

I have a model called Project.rb and another model called Project_student_eoi.rb.

The associations are:

Project has many project_student_eois
Project_student_eoi belongs to Project

In my project_student_eoi form, I want to show some of the attributes from the project table (project :title and :hero_image).

I found this problem on SO which is similar to mine and have tried the suggestion. It isn't working. http://stackoverflow.com/questions/15862365/displaying-attributes-for-associated-models-in-views

In my project_student_eoi view #form, I have:

    <%= project_student_eoi.project.title if project.title %>

    <%= project_student_eoi.project.hero_image.thumb if project.hero_image%>

I have actually got title and hero_image attributes - they display properly in the project # show page. I put the if statement in because that's the suggestion from the other SO post.

I get this error:

undefined local variable or method `project' for #<#<Class:0x007f9d44d50690>:0x007f9d44a52ce0>

I've tried putting '@' in front of the line to see whether that helps. I get this error:

        <%= @project_student_eoi.project.hero_image.thumb if project.hero_image%>

In this case, I get this error:

NameError at /project_student_eois/new
undefined local variable or method `project' for #<#<Class:0x007f9d44d50690>:0x007f9d41d0f788>

I can't find any resources that help understand how to show associated attributes. I have a book called the Rails 4 Way - it talks a lot about defining associations but no clues about how to use them.

Project_student_eoi.rb:

class ProjectStudentEoi < ActiveRecord::Base

  # --------------- associations

  belongs_to :project
  # belongs_to :student

  # --------------- sweeps

  # --------------- validations

  # --------------- class methods

  # --------------- callbacks

  # --------------- instance methods

  # --------------- private methods

end

I only added the if statement because it was a suggestion in the above SO post.

When I try:

    <%= @project_student_eoi.project.title %>

    <%= @project_student_eoi.project.hero_image.thumb %>

I get this error:

NoMethodError at /project_student_eois/new
undefined method `title' for nil:NilClass

The first set of comments below identify that I am trying to show existing project attributes in the form (a new action) for project_student_eoi. The form is not yet saved and doesn't yet exist. Is it best to create another view in projects, a nest the project student eoi form in that projects view, so that i can show the project details in a sidebar?

If this is a valid approach, I have added a new file to my project view folder called' student_eoi.html.erb. Inside that I have this code:

<div class="containerfluid">
<%= render 'static/deviselinks'%>
  <div class="row">
    <div class="col-md-10 col-md-offset-1">

      <h1 class="header-project" style="margin-bottom:10%">
        Express your interest
      </h1>
      <div class="col-md-7 col-md-offset-1">

      <%= render 'project_student_eois/form' %>
      </div>

      <div class="col-md-3 col-md-offset-1">
      <div class="uniboxtitle" style="background-color: #f1f1f1; padding:20px ">

            <%= @project.title %>

            <%= @project.hero_image.thumb %>


       </div>
      </div>


    <div class="formminor">
        <%= link_to 'Back', project_path %>
    </div>
    </div>
  </div>
</div>

Then, in my projects#show page I have:

<%= link_to  'Join this project', student_eoi_path %>

When I try this, I get this error:

NameError at /projects/2
undefined local variable or method `student_eoi_path' for #<#<Class:0x007ff72bc87290>:0x007ff734312c60>

I think this might be because I have to add something to the projects controller to handle this view - I just don't know what that thing is. Is it a line nested in the show action, that says @student_eoi? I don't understand this process and don't know how to solve from here (if this is even a credible approach).

Also, i have restful routes. This will be the first time i've made a view that doesnt come from the scaffolding so not sure if i have to add something to the routes (which are currently just resources :projects).

My attempt at this is:

In my routes.rb, I now have:

resources :projects do
    get 'projects/student_eoi' => 'projects/student_eoi', as => 'student_interest'

I don't understand what this get line means - I have copied from some other examples around SO but they are describing different issues (so don't know if I'm on the wrong track).

In my projects_controller.rb, I have:

  def student_eoi

  end

I don't know what to put in this definition. The idea for this new project show is to have a page with a sidebar that shows key project details and holds a form for a new project_student_eoi (which is the child resource).

Can anyone see what I need to do to get this working?

The form partial is in my project_student_eoi view folder. The reason that I have nested that form inside a view which is in my projects view folder is that I want to show the project title and image in a side bar next to the form. If the form is a new action, then it doesn't know how to find the project (when I try to reference the project from the project_student_eoi form). My attempts at doing it this way are above at the top of this post.

That's why I made a new file in the projects view folder called: student_eoi.html.erb.

In that, I have:

<div class="containerfluid">
<%= render 'static/deviselinks'%>
  <div class="row">
    <div class="col-md-10 col-md-offset-1">

      <h1 class="header-project" style="margin-bottom:10%">
        Express your interest
      </h1>
      <div class="col-md-7 col-md-offset-1">

      <%= render 'project_student_eois/form' %>
      </div>

      <div class="col-md-3 col-md-offset-1">
      <div class="uniboxtitle" style="background-color: #f1f1f1; padding:20px ">

            <%= @project.title %>

            <%= @project.hero_image.thumb %>


       </div>
      </div>


    <div class="formminor">
        <%= link_to 'Back', project_path %>
    </div>
    </div>
  </div>
</div>

The gist is that the projects view knows which project it is talking about and nests the new form action from the project_student_eoi folder.

In my routes.rb, I now have:

resources :projects do
    get 'projects/student_eoi' => 'projects/student_eoi', as => 'student_interest'

In my projects_controller.rb, I have:

def student_eoi

  end

I don't know what to put in this definition. The idea for this new project show is to have a page with a sidebar that shows key project details and holds a form for a new project_student_eoi (which is the child resource).

When I try this, I get this error:

ActiveRecord::RecordNotFound at /project_student_eois/2
Couldn't find ProjectStudentEoi with 'id'=2

id 2 is the number of my project, not my projectStudentEoi. The line reference this error points to is:

def set_project_student_eoi
  @project_student_eoi = ProjectStudentEoi.find(params[:id])
end

Can anyone see what I need to do to get this to work?

1
  • Can you show the code of your Project_student_eoi model?
    – panmari
    Oct 9, 2015 at 21:12

1 Answer 1

0

You want to show :title and :hero_image but only if they exist?

And this works?

<%= @project_student_eoi.project.title %>

If the above works, the error you're getting is due to the if statement. Change this:

<%= @project_student_eoi.project.title if project.title %>

To this:

<%= @project_student_eoi.project.title if @project_student_eoi.project.title %>

The error is because project is not a valid method or variable for the view itself.

5
  • No Philip. That's not what I want. I want to show the title and hero_image of the project. None of my attempts work. I added the if statement because it was a suggestion in the other SO post. I definitively have existing attributes for title/hero image and they display properly in my projects show page. Now I want to put them in the project_student_eois form (in the view file for project_student_eois). Nothing I have tried works to enable this to be shown.
    – Mel
    Oct 9, 2015 at 21:32
  • <%= @project_student_eoi.project.title %> by itself causes an error? Oct 9, 2015 at 21:35
  • no - i added the example and the error message above
    – Mel
    Oct 9, 2015 at 21:40
  • That is the #new action. A new record won't have a project associated yet. In your controller you could do @project_student_eoi.project.build to create an empty record. Or adjust your view to use if @project_student_eoi.project.present? to test if you should display project attributes. Oct 9, 2015 at 21:42
  • That's not true. the project creator creates the project. If a student wants to participate, then it completes a form expressing interest. The project exists before the project_student_eoi is created. How do you show the project when the project_student_eoi is not yet created?
    – Mel
    Oct 9, 2015 at 21:44

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.