I'm working on a videogame database, where users can signup, make game entries, rate and review them.
I have the Models: User, Game, Rating and Review. Each user can give his own rating to any game, so I have to pass the game.id to the ratings.game_id in order to associate ratings to certain games.
And I would like to think that it should work the same way about posting reviews, but every time I the game.id shall be passed, get a Runtime Error
"Called id for nil, which would mistakenly be 4 -- if you really wanted the id of nil, use object_id"
This is what my associations in my models look like
user.rb has_many :reviews, dependent: :destroy has_many :reviewed_games, :through => :reviews, :source => :games
game.rb has_many :reviews has_many :authors, :through => :reviews, :source => :users
review.rb belongs_to :user belongs_to :game
This is my review controller def create @game = Game.find_by_id(params[:game_id]) @review = current_user.reviews.build(params[:review]) @review.game_id = @game.id @review.user_id = current_user.id
if @review.save flash[:success] = "review created!" redirect_to @review else render 'new' end end
And my Form <%= form_for(@review) do |f| %> <div class="field"> <%= f.text_area :content, placeholder: "Compose new review..." %> </div> <%= hidden_field_tag("game_id", @game.id) %> <%= f.submit "Post", class: "btn btn-large btn-primary" %> <% end %>
The Runtime Error occurs, once I try to open the reviews/new url. When I dismiss the hidden_field_tag, I receive the review form, but now posting causes the exact same error (because of the game.id in the create method).
I really can't explain why this is happening, because the very same way my whole rating feature is programmed and it works just fine.
Can you Guys help me out?