简体   繁体   English

从控制器调用Model方法

[英]Calling a Model method from controller

I have a user view where I allow a user to upload his own image or allow him to import image from his LinkedIn profile. 我有一个用户视图,允许用户上载他自己的图像或允许他从他的LinkedIn个人资料中导入图像。 My LinkedIn OAuth works fine and returns the url of the image located on linkedin servers. 我的LinkedIn OAuth可以正常工作,并返回位于linkedin服务器上的图像的URL。 I have a picture_from_url method in my User model which is called by UsersController in it's update method. 我的用户模型中有一个picture_from_url方法,在其update方法中被UsersController调用。 I set the url to a sessions variable and then send that to the model method in the controller. 我将url设置为一个会话变量,然后将其发送到控制器中的model方法。 This ,however, is not updating the image for the user. 但是,这不会为用户更新图像。 I have the papertrail gem setup so he can use the image from his computer and change his profile picture just fine. 我有papertrail gem设置,因此他可以使用计算机上的图像并更改个人资料图片。

My Model method 我的模型方法

 def  picture_from_url(url)
   encoded_url = URI.encode(url.to_s)
   avatar= URI.parse(encoded_url)
 end

My Controller Call 我的控制器呼叫

def update
  @user = User.find(params[:id])
  @user.picture_from_url(session[:linkedin_picture])
  respond_to do |format|
    if can?(current_user, @user)
      if @user.update_attributes(user_params)
        format.html { redirect_to @user, notice: 'User was successfully  updated.' }
        format.json { render_success @user }
      else
        format.html { render action: 'edit' }
        format.json { render_error @user }
      end
    else
      format.html { render action: 'edit', status: :unprocessable_entity }
      format.json { render_error @user }
    end
  end
end

You're not actually saving it (as you said). 您实际上并没有保存它(如您所说)。

You supply your picture_from_url method with a URL, encode and parse it. 您为您的picture_from_url方法提供一个URL,对其进行编码和解析。 The last thing you do with it is set it to avatar . 使用它的最后一件事是将其设置为avatar I'm guessing that's an attribute on your User model. 我猜这是您的User模型上的一个属性。 You're not saving it though. 您没有保存它。

The following line in your controller doesn't save it either: 控制器中的以下行也不会保存它:

@user.update_attributes(user_params)

The above line is updating your @user record with the specified user_params and the URL isn't specified in it. 上一行使用指定的user_params更新@user记录, @user在其中指定URL。

So, you either have to save your @user record when you call the picture_from_url method like this: 因此,当您像这样调用picture_from_url方法时,要么必须保存您的@user记录,要么:

def picture_from_url(url)
  encoded_url = URI.encode(url.to_s)
  update_attributes avatar: URI.parse(encoded_url)
end

Or you can add the formatted URL (which you can retrieve from the picture_from_url method) to the user_params hash which is than saved when update_attributes is called in your controller, like this: 或者,您可以将格式化的URL(可以从picture_from_url方法检索到)添加到user_params哈希中,然后在控制器中调用update_attributes时保存该哈希,如下所示:

def picture_from_url(url)
  encoded_url = URI.encode(url.to_s)
  URI.parse encoded_url
end

Change the picture_from_url method to the above in your User model (technically you don't have to do this, but it is probably more readable) and your update action to the following: User模型中将picture_from_url方法更改为上述方法(从技术上讲,您不必这样做,但它可能更易读),并将update操作更改为以下内容:

def update
   @user = User.find(params[:id])
   user_params.merge! avatar: @user.picture_from_url(session[:linkedin_picture])

   ...
 end

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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