如何在轨道上包含红宝石中的api?

编程入门 行业动态 更新时间:2024-10-23 15:17:08
本文介绍了如何在轨道上包含红宝石中的api?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

如何在轨道上包含红宝石中的api?例如twitter api

解决方案

1)使用rails new appname创建一个api 2)然后创建twitter app dev.twitter/apps [ ^ ] 3)添加你的宝石文件里面添加Twitter宝石: 宝石''推特'' 4)rails g model tweet tweet_content:string 5)rails g controller tweet

in controller require rubygems需要twitter 类TweetController < ApplicationController def user_page @ tweet = Tweet.new 结束 def user_tweet @tweet = Tweet.new(params [:推文]) if @ tweet.save 然后 # 某些 methods require authentication。 要 获取 您的 Twitter OAuth 凭据, # register a app at dev.twitter/apps Twitter.configure do | config | config.consumer_key =' key' config.consumer_secret =' skey' config.oauth_token =' token' config.oauth_token_secret =' stoken' 结束 # 初始化 您的 Twitter client client = Twitter :: Client.new # 发布 a status upda te client.update(@ tweet.tweet_content) end render action: 'user_page', :notice = > 'Twitter成功发布' 结束 结束模型类Tweet < ActiveRecord :: Base # attr_accessible :title, :body attr_accessible :tweet_content end user_page.html.erb <% = form_for(@tweet, :url = > '/ user_tweet')do | tweet_form | %GT; <% = tweet_form.text_area:tweet_content %> <% = tweet_form.submit Tweet %> <% end % >

How to include api in ruby on rails? for example twitter api

解决方案

1)create an api using rails new appname 2)then create twitter app dev.twitter/apps[^] 3)add Inside your gemfile add the Twitter gem: gem ''twitter'' 4)rails g model tweet tweet_content:string 5) rails g controller tweet

in controller require "rubygems" require "twitter" class TweetController < ApplicationController def user_page @tweet = Tweet.new end def user_tweet @tweet = Tweet.new(params[:tweet]) if @tweet.save then # Certain methods require authentication. To get your Twitter OAuth credentials, # register an app at dev.twitter/apps Twitter.configure do |config| config.consumer_key = 'key' config.consumer_secret = 'skey' config.oauth_token = 'token' config.oauth_token_secret = 'stoken' end # Initialize your Twitter client client = Twitter::Client.new # Post a status update client.update(@tweet.tweet_content) end render action: 'user_page', :notice => 'Twitter successfully posted' end end in model class Tweet < ActiveRecord::Base # attr_accessible :title, :body attr_accessible :tweet_content end in user_page.html.erb <%= form_for(@tweet, :url => '/user_tweet') do |tweet_form| %> <%= tweet_form.text_area :tweet_content %> <%= tweet_form.submit "Tweet" %> <% end %>

更多推荐

如何在轨道上包含红宝石中的api?

本文发布于:2023-10-27 11:33:16,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1533233.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:红宝石   轨道   如何在   api

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!