我正在尝试通过在rails应用程序中调用Linkedin API来保存数据,但是当它保存时我收到错误:
TypeError: can't cast LinkedIn::Mash to text
我不希望用户编辑信息,所以我在NEW控制器中设置信息如下:
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
这被认为是不好的做法吗?我应该在创建控制器中设置保存吗? TypEerror怎么样?我觉得因为我直接从API保存信息,所以它不能保存为普通测试。
答案 0 :(得分:1)
这是config/routes.rb
应该是这样的:
resources :resumes
这是控制器中的#create
方法。
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
#create
方法自然是一个帖子。该资源对于理解路线非常有帮助。 http://guides.rubyonrails.org/routing.html。至于你关于土豆泥的问题,我认为这里已经回答How to parse a Mash from LinkedIn to create a Ruby object。