简体   繁体   中英

Rails Many-to-Many Debugging

Im writing a rails application for pairing people up when they want to play videogames together. I have a User model, and a Game model. Then I have a UserGame model for the many to many relationship.

The User may choose which games they are willing to play on sign up or in a profile edit page, and their games will be displayed in a (ul) list on their profile page. All that is shown in the list when the page loads, though, is [] and I have no idea what that's supposed to mean...

However, I am finding that after signing up and/or editing the user model, the list appears to remain empty. I am not sure whether I am assigning the fields incorrectly during edit/sign up, or whether I am accessing them incorrectly later for display and honestly I have no idea how to go about finding out... Any help would be appreciated as I am still learning to use both Rails and Ruby. I have been through the Rails tutorial that builds a simple blog, but I am still new to some of the more complicated stuff like this many-to-many.

The User Model:

class User < ActiveRecord::Base
  attr_accessible :name, :password, :password_confirmation

  attr_accessor :password
  before_save :encrypt_password

  validates_confirmation_of :password
  validates_presence_of :password, :on => :create
  validates_presence_of :name
  validates_uniqueness_of :name

  has_many :user_games
  has_many :games, :through => :user_games

  def self.authenticate(name, password)
    user = find_by_name(name)
    if user && user.password_hash == BCrypt::Engine.hash_secret(password, user.password_salt)
      user
    else
      nil
    end
  end

  def encrypt_password
    if password.present?
      self.password_salt = BCrypt::Engine.generate_salt
      self.password_hash = BCrypt::Engine.hash_secret(password, password_salt)
    end
  end
end

The Game model:

class Game < ActiveRecord::Base
  attr_accessible :name, :player_count

  has_many :user_games
  has_many :users, :through => :user_games
end

User Update:

def update
  @user = User.authenticate(current_user.name, params[:user][:password])
  respond_to do |format
    if @user
      if @user.update(params[:user])
        format.html{ redirect_to @user, notice: "User was successfully updated!" 
      else
        format.html{ redirect_to edit_profile_path, notice: "Unable to update!" }
      end
    else
        format.html{ redirect_to edit_profile_path, notice: "Unable to authenticate user " + current_user.email }
    end
  end
end

User show.html.erb:

<h1><%= @user.name %></h1>

<p>
  <ul>
  <%= @user.games.each do |g| %>
    <li><%= g.name %></li>
  <% end %>
  </ul>
</p>

User edit.html.erb:

<%= form_for @user do |f| %>
  <% if @user.errors.any? %>
    <div class="error_messages">
      <h2>Form is invalid</h2>
      <ul>
        <% for message in @user.errors.full_messages %>
          <li><%= message %></li>
        <% end %>
      </ul>
    </div>
  <% end %>
  <p>
    <%= f.label :name %><br />
    <%= f.text_field :name %>
  </p>
  <p>
    <%= f.label :password %><br />
    <%= f.password_field :password %>
  </p>
  <p>
    <%= f.label :password_confirmation %><br />
    <%= f.password_field :password_confirmation %>
  </p>
  <p>
    <%= f.label "select any games you're willing to play (you can add new ones later)" %><br />
    <%= collection_select( @user, :game_ids, Game.all, :id, :name, {}, { :multiple => true } ) %>
  </p>
  <p class="button"><%= f.submit %></p>
<% end %>

EDIT: after further attempts to find the problem, I have found that the @user.update() function call is failing. Not sure what could cause this.

this is not an answer because of not enough reputations, Imagine it's a comment.

Try to debug it.

<%=debug @user %>

<%=debug collection_select( @user, :game_ids, Game.all, :id, :name, {}, { :multiple => true } ) %>

You will be able to see if it fetches something or not. how to debug a rails app .

When i have problems like that, I fire up rails c and I try to understand it by entering all the models step by step example:

User.first.user_games

error or not?

If it was success I try to build it in my rails app.

I have solved the problem. I needed to list :game_ids under attr_accessible in the model definition. The @user.update(params[:user]) call in the User Controller was failing otherwise.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM