Pregunta

I am trying to save data from calling the Linkedin API in my rails app, but when it goes to save I get the error:

TypeError: can't cast LinkedIn::Mash to text

I do not want the user to edit the information either, so I set the information in the NEW controller as follows:

def new
  @client = LinkedIn::Client.new
  @client.authorize_from_access(current_user.consumer_token, current_user.consumer_secret)
  @resume = Resume.new do |u|
    u.location = current_user.location
    u.picture_url = current_user.image_url
    u.summary = @client.profile(:fields => %w(summary))
    u.work_experience = @client.profile(:fields => %w(positions))
    u.education = @client.profile(:fields => %w(educations))
    u.user = current_user
  end

  @resume.save!

  if @resume.save
    flash[:succes] = "Resume Saved!"
    redirect_to resumes_path
  else
    flash[:error] = "Resume did not save."
    redirect_to resumes_path
  end
end

Is that considered bad practice? Should I set the save in the create controller? How about the TypEerror? I feel like since I am saving the information straight from the API that it can't be saved as plain test.

¿Fue útil?

Solución

This is what your config/routes.rb should look like:

resources :resumes

This is the #create method in your controller.

def create
  @resume = Resume.new(params[:resume])  # if you are on rails 4 then you have to use strong params
  if @resume.save
    flash[:succes] = "Resume Saved!"
    redirect_to resumes_path
  else
    flash[:error] = "Resume did not save."
    redirect_to resumes_path
  end
end

The #create method is a post naturally. This resource is very helpful for understanding routes. http://guides.rubyonrails.org/routing.html. As to your question regarding the mash, I do think it has been answered here How to parse a Mash from LinkedIn to create a Ruby object.

Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top