0

I have two objects, sites and scores. A site has_many scores and a score belongs_to a site. I'm trying to build out the new view for my scores so I can add a score to a specific site.

Here is my view for show.html.erb for my Site object

<h1> 
    <%= @site.name %> 
    <%= @site.net_promoter_score %>
    <% @site.scores.each do |score| %>
        <li>
            <%= score.value %>
        </li>
    <% end %>
</h1>
<br>
<%= link_to "Create Score", new_score_path(site_id: @site.id) %>
<br>
<%= link_to "Back to Index", sites_path %>

And here is my new view for score:

<html>
    <body>
        <h1 style="text-align:center">
            @site.name
        </h1>
        <br>

      <h2 style="text-align:center">
        On a scale from 1-10, how likely are you to recommend this site to a friend or colleague?
      </h2>
        <form id="NPSform"; style= "text-align:center">
            <input type="submit" name="mysubmit" value="Submit"/>
        </form>
    </body>
</html>

<script>
    var inputs = '';
    for (var i = 1; i <= 10; i++) {
        inputs += '<input name="scores" type="radio" value="' + i + '" id="' + i + '">' + i;
    }
    document.getElementById('NPSform').insertAdjacentHTML('afterbegin', inputs);
</script>

<%= link_to "Back to Index", sites_path %>

How can I make sure I am referencing the correct site that is being passed through new_score_path in my show view?

Spance
  • 555
  • 3
  • 11
  • 29

1 Answers1

1

To make the form work you have to use nested attributes.

Your question has been answered here: Getting fields_for and accepts_nested_attributes_for to work with a belongs_to relationship.

You can read more here: http://api.rubyonrails.org/classes/ActiveRecord/NestedAttributes/ClassMethods.html

Community
  • 1
  • 1
Arthur Corenzan
  • 901
  • 9
  • 17