开发者

how to use model in different controller in RoR

开发者 https://www.devze.com 2023-01-03 00:58 出处:网络
I have a profile controller. But I want to use the User model to save some fields in the users table. So how can I load t开发者_StackOverflowhe User model in the profile controller?model are independe

I have a profile controller. But I want to use the User model to save some fields in the users table. So how can I load t开发者_StackOverflowhe User model in the profile controller?


model are independent entity and you can call it from any controller.

There can be a model without a controller and vice-versa.

For you question take a example below

class ProfileController < ApplicationController

  def some_method
    @user = User.find(params[:user_id])
    if @user.update_attributes(params[:user])
       // some action
    else

       // some action
    end
  end

end


You can load any model in any controller - just call it as you would normally.

If you need to load a User from your Profiles controller, you can just use User.find_by_whatever(), User.new, current_profile.user - not sure how your associations are set up, but there shouldn't be any access restrictions.

0

精彩评论

暂无评论...
验证码 换一张
取 消