Search code examples
ruby-on-railsrubyruby-on-rails-3rubygemsruby-on-rails-plugins

In ruby on rails 3 is a database only accessible from a user_controller?


Is it possible to pull a user object from any controller? For me it seems to only work in my users controller.

I'm trying to render a form that a user can use to update the details they signed up to the website with e.g. names, email, password etc. The problem is the page won't load (it is called using .load jquery function). It won't load when the form is added to the page but will load when it isn't there. I'm thinking it's something to do with it not being able to access the database.

In my pages controller I have this:

def edit_account

    @user  = User.find(params[:id])
    case params[:view]
      when 'basic_info'
        render 'pages/settings/basic_info'
      when 'relatives'
        render 'pages/settings/relatives'
    end

    @title = "My Account"
  end

It doesn't seem to work. Am I missing something? @user object works fine in user_controller and it's associated views but not in pages controller. Is there a way to give pages controller access?

Thanks


Solution

  • If you are using proper REST methods, the id will be the id of the page not the id of the user.

    What you can do is

    @page = Page.find(params[:id]) 
    

    And if pages and users are related (ie a page belongs to a user), you can do:

    @user = @page.user