administrateでacts-as-taggable-onと連携できるようにする
最近、管理画面をadministrateで作ってみたが、 acts-as-taggable-onが設定されたモデルをうまく表示する方法が分からなかった。。
試行錯誤の結果、一応できるようになったのでメモとして残す。
やったこと
- app/dashboards/xxx_dashboard.rbにtagsをField::HasManyで定義
- acts_as_taggable_onのtagとtaggingのdashboardを生成
- 生成したdashboard/controllerを移動・修正
- config/routes.rbの設定
app/dashboards/xxx_dashboard.rbにtagsをField::HasManyで定義
acts_as_taggableを定義しているモデルのxxx_dashboard.rbにtagsを定義する。
(例えば、bookモデルにacts_as_taggableを定義している場合はbook_dashboard.rb)
ATTRIBUTE_TYPES = { # ... tags: Field::HasMany.with_options(class_name: "::ActsAsTaggableOn::Tag"), } COLLECTION_ATTRIBUTES = { # ... :tags, } SHOW_PAGE_ATTRIBUTES = { # ... :tags, } FORM_ATTRIBUTES = { # ... :tags, }
administrateのField::HasManyができると編集画面などで便利なので、Field::HasManyでできるようにするのが目標だった。
acts_as_taggable_onのtagとtaggingのdashboardを生成
administrateのgenarateコマンドを実行する。
$ bundle exec rails generate administrate:dashboard ActsAsTaggableOn::Tag $ bundle exec rails generate administrate:dashboard ActsAsTaggableOn::Tagging
生成したdashboardを移動・修正
$ mkdir app/dashboards/acts_as_taggable_on $ mkdir app/controllers/admin/acts_as_taggable_on $ mv app/dashboards/tag_dashboard.rb app/dashboards/acts_as_taggable_on/ $ mv app/controllers/admin/tags_controller.rb app/controllers/admin/acts_as_taggable_on/ $ mv app/dashboards/tagging_dashboard.rb app/dashboards/acts_as_taggable_on/ $ mv app/controllers/admin/taggings_controller.rb app/controllers/admin/acts_as_taggable_on/
生成したままのclass ActsAsTaggableOn::TagDashboard
などのままでもいいが、私は下記のようにmoduleでネームスペースを切るようにした。
module ActsAsTaggableOn class TagDashboard < Administrate::BaseDashboard #... # Overwrite this method to customize how tags are displayed # across all pages of the admin dashboard. # def display_resource(tag) ←コメントアウトされているのを解除。ここが新規/編集時のtagsに表示される項目名になる tag.name end end end
module ActsAsTaggableOn class TaggingDashboard < Administrate::BaseDashboard #... end end
module Admin module ActsAsTaggableOn class TagsController < Admin::ApplicationController # ... end end end
module Admin module ActsAsTaggableOn class TaggingsController < Admin::ApplicationController # ... end end end
config/routes.rbの設定
各画面でacts-as-taggable-on関連のパスも生成しようとするのでroutes.rbの設定をしておく。
namespace :admin do #... namespace :acts_as_taggable_on do resources :tags resources :taggings end end
これで、acts-as-taggable-onを利用しているモデルのdashboardでもCRUDがhas_manyと同様でできるようになる。
ただ、tagやtaggingは表示のみで新規/編集/削除が画面からできないのでできるようにしたいところ(まだ、直近で必要無いので今は保留)
ネームスペースで区切られたモデルはまだサポートされてなさそう。 下記のpull requestが取り込まれると解決するか?
2017/06/27 追記
最新のmasterブランチだと上記のpull requestも取り込まれていて、HasOneもサポートされています!
一応、Qiitaの方にも投稿しておいた。 qiita.com
HerokuのDBにimportする
下記のコマンドを実行。
heroku pg:psql --app アプリ名 DATABASE_URL < sqlファイル
※dockerでpostgresqlをインストールして利用している場合は、ローカルにpostgresqlをインストールしていないと怒られる
herokuでridgepole用のDB設定をする
まあ、下記にある通りなんだけど。
herokuのpostgresqlのDATABASE_URLは下記のような形式になっているので、config/database.ymlをそれように変更する。
postgres://username:password@hostname:5432/db_name
activeadminでログインにDeviseではなくSorceryを利用する
管理画面をサクッと作成できるactiveadminで、ユーザ認証にDeviseではなくSorceryを利用したのでその手順を紹介。
※前提としてactiveadminは導入済みとします
1. sorceryをインストール
Gemfileにsorceryを記述して、bundle install
[Gemfile] ・・・ gem 'sorcery' ・・・
2. sorceryの設定ファイルなどを生成
rails generate sorcery:install
を実行
※remember_meなどのsubmodulesも設定したい場合はドキュメントを参照。
※初回のbundle installでpathオプションを指定した人はbundle exec rails generate sorcery:install
ですよ。念のため初心者の為に補足。
3. db:migrateを実行
手順2. でmigrationファイルが生成されているのでrails db:migrate
を実行。
4. 認証関連のController/Viewの作成
DeviseのようにビルドインでControllerやViewはないので、ログインに必要なものを自分で作ります。
※すごく簡易なログイン画面とログイン/ログアウト処理を実装します
- app/controllers/sessions_controller.rb
class SessionsController < ApplicationController def new @user = User.new end def create login(params[:user][:email], params[:user][:password]) # redirect_toの代わりにsorceryのredirect_back_or_toでもOK redirect_to admin_dashboard_path ← admin_dashboard_pathはactiveadminが定義したdashboardへのパス end def destroy logout redirect_to new_session_path end end
- app/views/sessions/new.html.erb
<h2>Log in</h2> <%= form_for @user, url: sessions_path do |f| %> <div> <%= f.text_field :email %> <%= f.password_field :password %> </div> <div> <%= f.submit "Log in" %> </div> <% end %>
コントローラー作成したのでconfig/routes.rbも設定します。
- config/routes.rb
Rails.application.routes.draw do ActiveAdmin.routes(self) resources :sessions, only: %i(new create) delete 'sign_out', to: 'sessions#destroy' ・・・ end
5. activeadminの設定の変更
sorceryの準備がやっと完了したので、やっとactiveadminとsoceryの連携部分です。
やることはドキュメントにあるとおり、config/initializers/active_admin.rbの設定を行います。
下記のコメントアウトを外してsorcery用の設定をしますです。
- config.authentication_method
- config.current_user_method
- config.logout_link_path
- config.logout_link_method
# # This setting changes the method which Active Admin calls # within the application controller. - # config.authentication_method = :authenticate_admin_user! + config.authentication_method = :require_login # == User Authorization # @@ -86,7 +86,7 @@ ActiveAdmin.setup do |config| # # This setting changes the method which Active Admin calls # (within the application controller) to return the currently logged in user. - # config.current_user_method = :current_admin_user + config.current_user_method = :current_user # == Logging Out # @@ -98,13 +98,13 @@ ActiveAdmin.setup do |config| # will call the method to return the path. # # Default: - config.logout_link_path = :destroy_admin_user_session_path + config.logout_link_path = :sign_out_path # This setting changes the http method used when rendering the # link. For example :get, :delete, :put, etc.. # # Default: - # config.logout_link_method = :get + config.logout_link_method = :delete # == Root # ・・・
6. root_pathの設定
手順5. まで完了したらhttp://localhost:3000/admin
にアクセスしてみましょう。
※localhostやポート番号の3000は自身の環境に合わせてください
アクセスすると下記のエラーが発生すると思います。
NameError (undefined local variable or method `root_path' for #<Admin::AlcoholsController:0x0055a9592f3d40>): tmp/bundler/ruby/2.3.0/gems/sorcery-0.10.3/lib/sorcery/controller.rb:98:in `not_authenticated' tmp/bundler/ruby/2.3.0/gems/sorcery-0.10.3/lib/sorcery/controller.rb:25:in `require_login' ・・・
これは認証が必要なページに未認証でアクセスした時にsorceryがroot_pathにリダイレクトしようとしているのですが、root_pathが設定されていないのでこのエラーが発生しています。
これを解決するにはconfig/routes.rbでrootの設定をすればいいです。
未認証でアクセスした場合はログイン画面に遷移して欲しいのでログイン画面のアクションを指定します。
- config/routes.rb
Rails.application.routes.draw do ActiveAdmin.routes(self) resources :sessions, only: %i(new create) delete 'sign_out', to: 'sessions#destroy' root to: 'sessions#new' ・・・ end
これで、未認証状態でhttp://localhost:3000/admin
にアクセスした時にログイン画面へ遷移させることができるようになりました。
【番外】config/routes.rbにrootを設定する以外の方法
上記でconfig/routes.rbにrootを設定する方法を紹介しましたが、rootがログイン画面なのは何かしっくり来ません。(私だけでしょうか?)
そんな場合はapplication_controller.rbのでsorceryが定義したnot_authenticatedをオーバーライドします。
- app/controllers/application_controller.rb
class ApplicationController < ActionController::Base ・・・ # override sorcery not_authenticated method def not_authenticated redirect_to new_session_path end ・・・ end
7. 確認
rails consoleなどでユーザを作成してからログインを行いましょう。ログイン後activeadminのダッシュボード画面が表示されると思います。
※ログアウトも問題なくできるはずです
以上で完了です。
react-native-navigationでRCCManager.h file not found.エラー
react-native-navigationを使ってみようとインストールしてみたが、
RCCManager.h file not found.
とエラーになってしまい起動できなかった。。。
バージョン
- react-native: 0.41.2
- react-native-navigation: 2.0.0-experimental.256
エラーが出た手順
- yarn add react-native-navigation@next
- react-native link
- AppDelegate.mを変更
- AppDelegate.mをコピペした
手順3.を終えた時点で、#import "RCCManager.h"
の箇所がRCCManager.h file not found.エラーになってしまいシュミレーターも起動できない。。。
解決方法
公式のドキュメントはreact-native linkを利用せずに手動でライブラリをリンクさせている。
この手順のHeader Search Pathsの箇所の対応をするとエラーが消えてシュミレーターも起動できるようになった。
In Xcode, in Project Navigator (left pane), click on your project (top) and select the Build Settings tab (right pane). In the Header Search Paths section add $(SRCROOT)/../node_modules/react-native-navigation/ios. Make sure on the right to mark this new path recursive (screenshots)
おそらく公式の手順通りに手動リンクしていれば発生しなかったのだろう。。
jquery.validate.jsで2つあるsubmitボタンの内の1つのバリデーションを無効にする
もともと変更を行うformを作成していたが、そこで追加で別submitボタンで追加したかった。
ただ、何もしないと新しく追加した方のsubmitボタンを押してもvalidate.jsの入力チェックが効いてしまう。
※新しく追加した方は入力チェックをしたくない
validate.jsのドキュメントを読んでみたら、ちょうどいいオプションがあったのでそれで解決した。
Reference documentation | jQuery Validation Plugin
※formnovalidateを対象のsubmitボタンに設定するとそちらだけ入力チェックをスキップできる
Railsでsubmit_tagを使っていたので下記のように書いた。
<%= submit_tag "submit2", {formnovalidate: true} %>
React Nativeでrun-iosした時にPrint: Entry, ":CFBundleIdentifier", Does Not Existになってしまう問題
react-native run-iosを実行した時に下記のようなメッセージが表示されてしまい、アプリが起動しなかった。
(XCodeでRunするとちゃんとアプリが起動する)
The following commands produced analyzer issues: Analyze Modules/RCTRedBox.m (1 command with analyzer issues) Installing build/Build/Products/Debug-iphonesimulator/アプリ名.app An error was encountered processing the command (domain=NSPOSIXErrorDomain, code=2): Failed to install the requested application An application bundle was not found at the provided path. Provide a valid path to the desired application bundle. Print: Entry, ":CFBundleIdentifier", Does Not Exist Command failed: /usr/libexec/PlistBuddy -c Print:CFBundleIdentifier build/Build/Products/Debug-iphonesimulator/アプリ名.app/Info.plist Print: Entry, ":CFBundleIdentifier", Does Not Exist
ググると下記のQiitaページがヒットするが、ちょっと今回の事象とは違う。
(RCTSRWebSocketのエラーは出てない)
もう少し調べてみると、react-nativeのissueでそれらしきものを発見。 下記のコメントの対応で解決した。
build/Build/Productsにするだけでなく、"Relative to Workspace"を選択するのがポイントみたい。