我试图在Rails 4中为oEmbed创建一个提供程序 创建了路径,控制器和操作。
但是我无法使用oembed所需的参数创建一个对象(例如,type,version,html,...)
我试图这样做:
class ServicesController < ApplicationController
def oembed
# get project ID
url = params[:url].split("/")
project_id = url[4]
@project = Project.find(project_id)
html = render_to_string :partial => "projects/oembed", :formats => [:html], :locals => { :project => @
# here's the problem:
oembed_response["type"] = "rich"
oembed_response["version"] = "1.0"
oembed_response["title"] = @project.name
oembed_response["html"] = html
respond_to do | format |
if(@project)
format.html { render :text => "Test" }
format.json { render json: oembed_response, status: :ok }
format.xml { render xml: oembed_response, status: :ok }
else
# error
end
end
end
end
我总是收到以下错误:
NameError(未定义的局部变量或方法
oembed_response' for #<ServicesController:0x007f9a8be579a0>): app/controllers/services_controller.rb:11:in
oembed&#39;
如何在不使用模型的情况下创建具有所需属性的自定义对象以及通常已完成的(强)参数?
我错过了什么吗?
提前致谢!
此致
CHRI
答案 0 :(得分:3)
首先将oembed_response
初始化为空哈希:
oembed_response = {}
与此错误无关,但您应该更改从ID获取对象网址的方式。路由应该自动解析id,例如。如果你在routes.rb
文件中有这个:
get "/projects/oembed/:id" => 'services#oembed'
然后你可以从params hash访问id(由路由路径中的:id
给出)directyl:
project_id = params[:id]