0

I'm building a simple Reviewer Blog Post on Rails 5 in order to teach myself. Its a Video Game Reviewer where Users can write Reviews about recent Games they've played. Users can also add Comments to Reviews.

I want to implement a custom attribute writer on my Game model through nested forms. When a User lists a Game for the first time, I also want them to be able to write a Review for that Game on the spot.

Game.rb

class Game < ApplicationRecord
  has_many :reviews, dependent: :destroy
  has_many :users, through: :reviews

  validates :title, presence: true

  def reviews_attributes=(reviews_attributes)
    reviews_attributes.values.each do |review_attributes|
      self.reviews.build(review_attributes)
    end
  end
end

games/new.html.erb

<h1>Enter a new Game</h1>

<%= form_for @game do |f| %>
  <%= render 'shared/error_messages', object: @game %>
  <%= render 'new_form', f: f %>
  <br><br>
  Review:
  <br>
  <%= f.fields_for :reviews, @game.reviews.build do |r| %>
  <%= render 'reviews/form', f: r %>

  <%= f.submit "Add Game and/or Review!" %>
  <% end %>
<% end %>

Reviews/form partial

  <%= f.label :title %>
  <%= f.text_field :title %>

  <br>
  <%= f.label :content %>
  <%= f.text_area :content %>

  <br>
  <%= f.label :score %>
  <%= f.text_field :score %>

  <%= f.hidden_field :user_id, :value => current_user.id %>

Games_Controller.rb

  def create
    @game = Game.new(game_params)
    if @game.save
      redirect_to @game
    else
      render :new
    end
  end

  private
    def game_params
      params.require(:game).permit(:title, :platform, reviews_attributes: [:rating, :content, :user_id])
    end

For some reason I keep getting Reviews is invalid whenever I try to create a new Review associated with a Game through my nested forms. My error_messages partial is rendering the error message saying: "1 error prohibited this from being saved: Reviews is invalid".

Something about the Review forms or data in the params hash isn't being transmitted I guess. I am not sure why. I even tried building the associations with the built-in Rails helper: accepts_nested_attributes_for and I still get the same error.

Here is the link to my repo for full clarity: https://github.com/jchu4483/Rails-Assessment-

Thanks, and any help or advice is appreciated.

Jason Chu
  • 355
  • 1
  • 2
  • 14

2 Answers2

1

It looks like the attributes in your reviews_attrbitues in the game_params don't match the attributes on the form. Game_params lists rating, content, user_id. In the form you have title, content, score.

0

Now I think the problem may be because of nested forms with has_many and through associations. Your Review isn't passing validation because it is joined to the User model as well. Your Review model should have accepts_nested_attributes_for User

class Review < ApplicationRecord
  belongs_to :user
  belongs_to :game

  accepts_nested_attributes_for :user
end

and you form should have yet another fields_for for the user

<%= form_for @game do |f| %>
   <%= f.fields_for :reviews do |r| %>
      <%= r.fields_for :users do |u| %>
         ...
      <% end %>
   <% end %>
   <%= f.submit %>
<% end %>

and in your controller in game_params you need to pass an array for users_attributes too

def game_params
  params.require(:game).permit(:title, :platform, reviews_attributes: [:id, :rating, :content, :user_id, user_attributes: [...])
end

The answer to this SO question may be helpful: https://stackoverflow.com/a/21983998/5531936

Community
  • 1
  • 1
Ren
  • 1,379
  • 2
  • 12
  • 24
  • does the error give you a source file and code line number? Can you tell what exactly the error is referring to? – Ren Sep 22 '16 at 02:17
  • Actually, there is no ActiveRecord or Rails Error, my error_messages partial is rendering the error message saying: "1 error prohibited this from being saved: Reviews is invalid". Something about the Review forms or data in the params hash isn't being transmitted I guess. – Jason Chu Sep 22 '16 at 02:19
  • hm, you should probably post that in your question. very helpful to know – Ren Sep 22 '16 at 02:21
  • Okay, just updated my question as per your suggestion. – Jason Chu Sep 22 '16 at 02:27
  • But the user_id is automatically being transmitted in my reviews/form <%= f.hidden_field :user_id, :value => current_user.id %>. So you're saying that in my forms for creating a new game and review, I also have to create a new fields_for user? – Jason Chu Sep 22 '16 at 02:58