On 15 April 2013 21:22, Ryo Saeba <li...@ruby-forum.com> wrote:
> 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).

The error message should tell you which line of code is causing the
error.  The problem is that whatever you are calling .id on at that
line of code is nil.

Have a look at the Rails Guide on Debugging and it will show you
techniques that you can use to debug your code.

Colin

-- 
You received this message because you are subscribed to the Google Groups "Ruby 
on Rails: Talk" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to rubyonrails-talk+unsubscr...@googlegroups.com.
To post to this group, send email to rubyonrails-talk@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.


Reply via email to