Topic: running into lots of problems on a rails app similar to facebook

Hey everyone, I'm starting to work on this app that is supposed to be similar to a Facebook/MySpace type of thing.  So I have a User model... and another Profile model...

class User < ActiveRecord::Base
  has_one :profile
.....
end

class Profile < ActiveRecord::Base
  belongs_to :user
end

So after a user registers, he/she will need to create a profile, and then be able to edit that profile.  The problem I'm running into is in the code for my ProfileController.  I just can't seem to get it to work at all and I'm not fluent enough in Ruby to figure it out.  If you can suggest anything for me to do to the code, I would be greatly appreciative.

class ProfileController < ApplicationController
  before_filter :authorize
 
    def new
      @profile = Profile.new
    end
   
    def create
      @user = User.find_by_id(session[:user_id])
      @profile = Profile.new
      if @post.save #request.post? and @profile.save
        flash[:notice] = 'Profile was successfully created.'
        redirect_to :controller => :user, :action => :index
      else
        render :action => :edit
      end
    end
   
    def show
      @user = User.find_by_id(session[:user_id])
      @profile = @user.profile
    end
   
    def edit
      @user = User.find_by_id(session[:user_id])
      @profile = @user.profile
    end
   
    def update
      @user = User.find_by_id(session[:user_id])
      @profile = @user.profile
      if @profile.update_attributes(params[:profile])
        flash[:notice] = 'Profile was successfully updated.'
        redirect_to :controller => :user, :action => :index
      else
        render :action => :edit
      end
    end

    def index
      @user = User.find_by_id(session[:user_id])
      @profile = @user.profile
    end
   
    def find_profile
      @profile = (session[:profile] ||= Profile.new)
    end
   
    def destroy
      Profile.find(params[:id]).destroy
      redirect_to :action => :show
    end
   
end

Re: running into lots of problems on a rails app similar to facebook

Welcome cps! Your code looks pretty sound except for the create method. I think what you want is this, but I'm novice at Rails too, so forgive me if it's wrong tongue

In the view, you should have a form setup. I recomend using the built in Rails methods, like text_field, password_field, etc. and setting the object to something descriptive, like "profile" in this case.

You should also have a hidden for parameter set to the user ID, which I see you have stored in session[:user_id]:

<%= hidden_field "profile[user_id]", :value => session[:user_id] %>

Also, I am assuming that your profiles table as a column named "user_id" setup, which is what Rails uses (by default) for associations. So:
def create
  if Profile.create(params[:profile])
    flash[:notice] = 'Profile was successfully created.'
    redirect_to :controller => :user, :action => :index
  else
    render :action => "edit"
  end
end

Untested, but I think it should work. If it doesn't just come and we'll help smile

Re: running into lots of problems on a rails app similar to facebook

Here is the migration file for profiles...

class CreateProfiles < ActiveRecord::Migration
  def self.up
    create_table :profiles do |t|
      t.column :music, :text
      t.column :movies, :text
      t.column :tv, :text
      t.column :books, :text
      t.column :sports, :text
      t.column :quotes, :text
    end
  end

  def self.down
    drop_table :profiles
  end
end


and here is the view (create.rhtml)... for some reason, i am unable to get to this page....
<div class="depot-form">

        <h3>Enter User Information</h3>

        <% form_for :profile, :url => { :action => :create} do |form| %>
        <table>
            <tr>
            <td>
            <label for="user_music">Music:</label>
            </td><td>
            <%= form.text_area :music, :size => 45, :maxlength => 45 %>
        </td>
        </tr>
..............
        <tr>
        <td>
            <label for="user_movies">Movies:</label>
            </td><td>
            <%= form.text_area :movies, :size => 45, :maxlength => 45 %>
        </td>
        </tr>
       
        </table>

        <%= submit_tag "Create Profile" %>

        <% end %>

</div>


i made the changes to profile controller that you said, but all it did was remain on the same homepage and flashed a notice saying "Profile was successfully created".....

Re: running into lots of problems on a rails app similar to facebook

try this (untested)

def create
  if Profile.create(params[:profile])
    flash[:notice] = 'Profile was successfully created.'
    redirect_to :controller => :user, :action => :index and return #halt execution and redirect NOW
  else
    render :action => "edit"
  end
end

Last edited by tconley (2007-05-02 23:09:15)