0

Rails 4 で oEmbed 用のプロバイダーを作成しようとしています。ルート、コントローラー、およびアクションが作成されます。

しかし、oembed に必要なパラメーター (タイプ、バージョン、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:inoembed'

通常行われているように、モデルなしで必要な属性と (強い) パラメータを持つカスタム オブジェクトを作成するにはどうすればよいですか?

何か不足していますか?

前もって感謝します!

よろしく、

クリ

4

1 に答える 1

3

oembed_response最初に空のハッシュとして初期化します。

oembed_response = {}

このエラーとは関係ありませんが、id からオブジェクトの URL を取得する方法を変更する必要があります。ルートは、ID を自動的に解析する必要があります。routes.rbファイルにこれがある場合:

get "/projects/oembed/:id" => 'services#oembed'

:id次に、params ハッシュから ID (routes パスで指定) directyl にアクセスできます。

project_id = params[:id]
于 2014-06-01T21:51:12.930 に答える