diff --git a/app/controllers/bot/bot_omniauth_callbacks_controller.rb b/app/controllers/bot/bot_omniauth_callbacks_controller.rb index 35b1b09..89561ae 100644 --- a/app/controllers/bot/bot_omniauth_callbacks_controller.rb +++ b/app/controllers/bot/bot_omniauth_callbacks_controller.rb @@ -38,8 +38,16 @@ def callback session[:bot] = bot - unless Bot.find_by_twitter_id(bot["twitter_id"]).nil? - redirect_to(new_bot_path, alert: "すでに登録済みです") + already_bot = Bot.find_by_twitter_id(bot["twitter_id"]) + + unless already_bot.nil? + unless already_bot.deleted + redirect_to(new_bot_path, alert: "すでに登録済みです") + else + already_bot.deleted = false + already_bot.save + redirect_to bots_path(current_user.username) + end else redirect_to new_bot_path end diff --git a/app/controllers/bot/bots_controller.rb b/app/controllers/bot/bots_controller.rb index 9f1c5ae..ed6c8fd 100644 --- a/app/controllers/bot/bots_controller.rb +++ b/app/controllers/bot/bots_controller.rb @@ -33,13 +33,13 @@ def create end def edit - id = params.require(:id) - @bot = Bot.find_by(id) + bot_param = params.permit(:id) + @bot = Bot.find_by(bot_param) end def update hash_tag_ids = params[:hash_tags] - @bot = Bot.find_by(params.require(:id)) + @bot = Bot.find_by(params.permit(:id)) unless register_hashtags(hash_tag_ids) redirect_to(edit_bot_path(current_user.username, @bot.id), alert: "存在しないHashTagです") @@ -119,7 +119,7 @@ def register_hashtags(hash_tag_ids) end hash_tag_ids.each do |hash_tag_id| - hash_tag = HashTag.find_by({:id => hash_tag_id}) + hash_tag = HashTag.find_by({:id => hash_tag_id.to_i}) if @bot.nil? || hash_tag.nil? return false elsif @bot.hash_tags.include?(hash_tag) diff --git a/app/controllers/bot/hash_tags_controller.rb b/app/controllers/bot/hash_tags_controller.rb index 5a81e89..cae7b5d 100644 --- a/app/controllers/bot/hash_tags_controller.rb +++ b/app/controllers/bot/hash_tags_controller.rb @@ -13,4 +13,13 @@ def create redirect_to(new_hash_tag_path, alert: "すでに登録されているHashTagです") end end + + def destroy + hash_tag_param = params.permit(:id) + hash_tag = HashTag.find_by(hash_tag_param) + unless hash_tag.nil? + hash_tag.destroy + end + redirect_to hash_tags_path + end end diff --git a/app/models/bot.rb b/app/models/bot.rb index 2e68a5b..135fc6e 100644 --- a/app/models/bot.rb +++ b/app/models/bot.rb @@ -6,7 +6,7 @@ class Bot < ActiveRecord::Base belongs_to :user has_many :bot_hash_tag_rels - has_many :hash_tags, :through => :bot_hash_tag_rels + has_many :hash_tags, :through => :bot_hash_tag_rels, :dependent => :destroy has_one :schedule, :dependent => :destroy validates :twitter_name, presence: true diff --git a/app/models/hash_tag.rb b/app/models/hash_tag.rb index 51bb7b9..185b7e3 100644 --- a/app/models/hash_tag.rb +++ b/app/models/hash_tag.rb @@ -1,6 +1,6 @@ class HashTag < ActiveRecord::Base has_many :bot_hash_tag_rels - has_many :bots, :through => :bot_hash_tag_rels + has_many :bots, :through => :bot_hash_tag_rels, :dependent => :destroy # 論理削除されていないレコードを全取得 def self.find_by_alive diff --git a/app/views/bot/bots/index.html.erb b/app/views/bot/bots/index.html.erb index c1d1d59..9400f14 100644 --- a/app/views/bot/bots/index.html.erb +++ b/app/views/bot/bots/index.html.erb @@ -31,7 +31,7 @@ <% end %> <%= button_to "編集", edit_bot_path(current_user.username, b.id), class: 'btn btn-warning', method: :get %> - <%= button_to "削除", "bot/#{b.id}", class: 'btn btn-danger', method: :delete %> + <%= button_to "削除", destroy_bot_path(current_user.username, b.id), class: 'btn btn-danger', method: :delete %> <%end%> <% end %> diff --git a/app/views/bot/hash_tags/index.html.erb b/app/views/bot/hash_tags/index.html.erb index c61e3bf..c810e21 100644 --- a/app/views/bot/hash_tags/index.html.erb +++ b/app/views/bot/hash_tags/index.html.erb @@ -11,11 +11,13 @@ No. HashTag + 削除 <% HashTag.all.each_with_index do |b, cnt| %> <%= cnt+1 %> <%= b.hash_tag %> + <%= button_to "削除", hash_tag_path(b.id), class: 'btn btn-danger', method: :delete %> <% end %> diff --git a/app/views/layouts/application.html.erb b/app/views/layouts/application.html.erb index 0f6cc12..7619527 100644 --- a/app/views/layouts/application.html.erb +++ b/app/views/layouts/application.html.erb @@ -18,8 +18,8 @@ - Logged in as <%=link_to current_user.username, bots_path(current_user.username)%> - <%= link_to 'プロフィール変更', edit_user_registration_path(current_user.username) %> | + Logged in as <%=link_to current_user.username, bots_path(current_user.username)%>| + <%= link_to 'HashTagの登録', hash_tags_path %> | <%= link_to "ログアウト", destroy_user_session_path, method: :delete %> <% else %> <%= link_to image_tag("signin_with_twitter_logo.png", :size => "188x30", :class => "twitter_layout"), user_omniauth_authorize_path(:twitter) %> diff --git a/config/routes.rb b/config/routes.rb index 11f4601..a801151 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -7,7 +7,7 @@ root to: "home#index" - resources :hash_tags, :module => :bot, :except => [:show, :edit, :update, :destroy] + resources :hash_tags, :module => :bot, :except => [:show, :edit, :update] devise_scope :user do # sessions @@ -25,9 +25,10 @@ delete ':username' => 'devise/registrations#destroy' end - resources :bots, :module => :bot, :path => ':username', :except => [:edit, :new, :create] + resources :bots, :module => :bot, :path => ':username', :except => [:edit, :new, :create, :destroy] match ":username/:id/settings", :to => 'bot/bots#edit', :via => :get, :as => :edit_bot match "new", :to => 'bot/bots#new', :via => :get, :as => :new_bot + match ":username/:id", :to => 'bot/bots#destroy', :via => :delete, :as => :destroy_bot match ":username/bots", :to => 'bot/bots#create', :via => :post, :as => :create_bot match 'bots/auth/twitter/callback', :to => 'bot/bot_omniauth_callbacks#callback', :via => [:get, :post], :as => :bot_omniauth_callback