いつもお世話になっております。
RailsアプリケーションでWebサイトを開発しているのですが、
ユーザーが投稿したコンテンツの一覧を埋め込みタグとして配信できるようにしたいと考えています。
具体的には、Twitterのタイムラインウィジェットのようなものを想定します。
https://medium.com/@gabrielhilal/widgets-api-in-rails-gabriel-hilal-375a1a859b81
上記のコードを参考にしながら、実装を試みたのですがエラーが出てしまい先に進めていません。
ソースコード
Widgetsコントローラー
Ruby
1class WidgetsController < ApplicationController 2 def show 3 respond_to do |format| 4 format.html { render params[:template], layout: 'widgets' } 5 format.js { render js: js_constructor } 6 end 7 end 8 9 def timeline 10 @user = User.find_by_id(params[:id]) 11 routing_error if @user.blank? 12 @posts = @user.posts.all 13 end 14 15 private 16 def js_constructor 17 content = render_to_string(params[:template], layout: false) 18 "document.write(#{content.to_json})" 19 end 20end
Widgetテンプレートビュー
Ruby
1<!DOCTYPE html> 2<html> 3 <head><%= stylesheet_link_tag 'widgets', media: 'all' %></head> 4 <body> 5 <%= yield %> 6 <body> 7</html>
Widgets/timline.html.erb
Ruby
1 <ul class="posts"> 2 <% @posts.each do |post| %> 3 <%= post.content %> 4 <% end %> 5 </ul>
ルーティング(routes.rb)
Ruby
1Rails.application.routes.draw do 2・・・ 3 4 get '/widgets/:template', to: 'widgets#show' 5 6・・・ 7end
出力を試みている側のHTML
Ruby
1<iframe src="http://localhost:3000//widgets/timeline" width="100%" height="100%" frameborder="0"></iframe>
困っている点
(1) iFrameを出現しようとした際にlocalhostが接続されていないというエラーが出る
(2) ユーザーが自分のページからタイムライン情報を取得してブログなどに貼り付けることを想定しているため、@user = User.find_by_id(params[:id])
の構文では不適切であるような気がする
すみませんが、どうかおたすけくださいませ。
回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。