Как создать и настроить свой веб-сервер на VDS (05 сен 2024)
Осенью самое время заняться установкой и тюнингом своего веб-сервера. Не правда ли?
Снова Rails и Twitter API, а также materialize и webpacker
- Aleksej
- Автор темы
- Не в сети
- Модератор
Less
Больше
7 года 4 дн. назад #1
от Aleksej
Aleksej создал тему: Снова Rails и Twitter API, а также materialize и webpacker
К материалу блога
Снова Rails и Twitter API, а также materialize и webpacker
.
index.html.erb
tweets_controller.rb
index.html.erb
Code:
<p id="notice"><%= notice %></p>
<h1>Tweets</h1>
<fieldset><legend>Twitter Accounts</legend>
<%= link_to 'New Tweet', new_tweet_path %>
<table>
<thead>
<tr>
<th>Name</th>
<th colspan="3"></th>
</tr>
</thead>
<tbody>
<% @tweets.each do |tweet| %>
<tr>
<td><%= tweet.name %></td>
<td><%= link_to 'Show', tweet %></td>
<td><%= link_to 'Edit', edit_tweet_path(tweet) %></td>
<td><%= link_to 'Destroy', tweet, method: :delete, data: { confirm: 'Are you sure?' } %></td>
</tr>
<% end %>
</tbody>
</table>
</fieldset>
<br>
<form method="get" id="twitsform" action="javascript:void(null);" onsubmit="call()">
<fieldset><legend>Twitter Tweet</legend>
<div class="input-field col s12">
<select name="select" >
<% @tweets.each do |tweets| %>
<option value="<%= tweets.key %> <%= tweets.secret %> <%= tweets.token %> <%= tweets.token_secret %>"><%= tweets.name %></option>
<% end %>
</select>
</div>
</fieldset>
<div class="row">
<form class="col s12">
<div class="row">
<div class="input-field col s12">
<textarea id="textarea1" class="materialize-textarea" name='tweet'></textarea>
<label for="textarea1">Textarea</label>
</div>
</div>
</div>
<button class="btn" type="submit">tweet</button>
<div class="progress" style="display:none;">
<div class="indeterminate"></div>
</div>
</form>
<script type="text/javascript" language="javascript">
function call() {
var msg = $('#twitsform').serialize();
$.ajax({
type: 'GET',
url: '/tweets',
data: msg,
beforeSend: function(){
$('.progress').show()
},
success: function(data) {
alert('Твиты опубликованы!'),
$('.progress').hide();
},
error: function(xhr, str){
alert('Что-то пошло не так: ' + xhr.responseCode),
$('.progress').hide();
}
});
}
</script>
<script>
$(document).ready(function() {
$('select').material_select();
});
</script>
tweets_controller.rb
Code:
class TweetsController < ApplicationController
before_action :set_tweet, only: [:show, :edit, :update, :destroy]
# GET /tweets
# GET /tweets.json
def index
@tweets = Tweet.all
if params[:tweet].present?
@client = Twitter::REST::Client.new do |config|
config.consumer_key = params['select'].split.first
config.consumer_secret = params['select'].split.second
config.access_token = params['select'].split.third
config.access_token_secret = params['select'].split.fourth
end
@array = params[:tweet].split(/[\r\n]+/)
@array.each do |i|
@client.update(i)
sleep rand(1..10)
end
end
end
# GET /tweets/1
# GET /tweets/1.json
def show
end
# GET /tweets/new
def new
@tweet = Tweet.new
end
# GET /tweets/1/edit
def edit
end
# POST /tweets
# POST /tweets.json
def create
@tweet = Tweet.new(tweet_params)
respond_to do |format|
if @tweet.save
format.html { redirect_to @tweet, notice: 'Tweet was successfully created.' }
format.json { render :show, status: :created, location: @tweet }
else
format.html { render :new }
format.json { render json: @tweet.errors, status: :unprocessable_entity }
end
end
end
# PATCH/PUT /tweets/1
# PATCH/PUT /tweets/1.json
def update
respond_to do |format|
if @tweet.update(tweet_params)
format.html { redirect_to @tweet, notice: 'Tweet was successfully updated.' }
format.json { render :show, status: :ok, location: @tweet }
else
format.html { render :edit }
format.json { render json: @tweet.errors, status: :unprocessable_entity }
end
end
end
# DELETE /tweets/1
# DELETE /tweets/1.json
def destroy
@tweet.destroy
respond_to do |format|
format.html { redirect_to tweets_url, notice: 'Tweet was successfully destroyed.' }
format.json { head :no_content }
end
end
private
# Use callbacks to share common setup or constraints between actions.
def set_tweet
@tweet = Tweet.find(params[:id])
end
# Never trust parameters from the scary internet, only allow the white list through.
def tweet_params
params.require(:tweet).permit(:name, :key, :secret, :token, :token_secret)
end
end
Пожалуйста Войти или Регистрация, чтобы присоединиться к беседе.