【Ruby on Rails】 cocoon gemで動的に要素を追加・削除できるフォームを作る
今回はRecipe
とIngredient
というモデルがあり、その中間テーブルとしてRecipeItem
というモデルがあります。
RecipeItemにはamount
という属性があり、Recipeのフォーム画面において、関連するIngredientをセレクトボックスから選び、その量(amount)も登録できるようにします。
動的に関連する要素(今回はIngredient)を追加していけるフォームを作成したいので、cocoon
というgemを導入することにします。
- まず画像から
- Install cocoon
- Recipe
- RecipeItem
- Ingredient
- レシピのform作成
- パーシャル作成
- その他参考記事
まず画像から
レシピの詳細画面はこんなです。
新規作成画面。わかりにくいですが、セレクトボックスで「Bacon」を選択し、その下の「Amount」に100を入力しています。
さらに「add Ingredient」というリンクをクリックすると、フォームが追加されます。
そしてレシピを作成すると「Bacon」と「Chicken fat」が登録されています。
続きを読む
クリックするとアイコンが変化するiconate.jsを使ってみた
デモページを見てみると一発でわかるのですが、
アイコンをクリックすると別のアイコンに変化します。
使い方
https://github.com/bitshadow/iconate#basic-usage
公式ページより
インストール
以下の3つの方法があります。
- NPM:
npm install iconate
- Bower:
bower install iconate
- Download zip.
Note: iconate.js also supports AMD and commonJS module pattern.
今回はCodepenでやってみましたのでRawGitを使用しました(あんまりよくないかもしれんけど)
スタイルシートとjsファイルの設定
<link rel="stylesheet" href="iconate.min.css"> <script type="text/javascript" src="iconate.min.js">
アイコンの要素を作る
fontawsomeを使用しているようなのでその導入も忘れず。
<i id="icon" class="fa fa-bars fa-lg"></i>
アニメーションの設定
var iconElement = document.getElementById('icon'); var options = { from: 'fa-bars', to: 'fa-arrow-right', animation: 'rubberBand' }; iconate(iconElement, options);
optionsのfrom:
に始めのアイコン(ここではハンバーガーアイコン)、to:
に右矢印を設定。
animation:
はrubberBand
(ゴムみたいにみょ〜んと動く)
そしてiconate関数の第一引数に対象となる要素、第二引数にオプションを渡します(第三引数にコールバック関数を設定することも可能)
というわけでCodepenでこんなのを作ってみました。
続きを読むBootstrapのアコーディオンパネル(collapse)でページ遷移の際に開いた状態を維持する
【Ruby on Rails】 bundler1.11.2に更新したら?rails g migration, rails cなどができなくなって困った
rails g migrationとかrails cの突然の死
bundle install
した際に「bundler古いよ」と言われたので、その後rbenv exec gem update bundler
で1.11.2
に更新しました。
すると今度は(bundle exec)rails g migration
やrails c
でエラーが出るようになりました(なぜかrails s
は大丈夫ですが)
(でも結局bundlerを更新したせいなのかどうかはわかりません)
/Users/noriyo_tcp/.rbenv/versions/2.2.3/lib/ruby/2.2.0/rubygems/core_ext/kernel_require.rb:54:in `require': cannot load such file -- bundler/setup (LoadError) from /Users/noriyo_tcp/.rbenv/versions/2.2.3/lib/ruby/2.2.0/rubygems/core_ext/kernel_require.rb:54:in `require' from /Users/noriyo_tcp/workspace/feel/vendor/bundle/ruby/2.2.0/gems/spring-1.6.0/lib/spring/commands.rb:33:in `<module:Spring>' from /Users/noriyo_tcp/workspace/feel/vendor/bundle/ruby/2.2.0/gems/spring-1.6.0/lib/spring/commands.rb:4:in `<top (required)>' from /Users/noriyo_tcp/.rbenv/versions/2.2.3/lib/ruby/2.2.0/rubygems/core_ext/kernel_require.rb:54:in `require' from /Users/noriyo_tcp/.rbenv/versions/2.2.3/lib/ruby/2.2.0/rubygems/core_ext/kernel_require.rb:54:in `require' from /Users/noriyo_tcp/workspace/feel/vendor/bundle/ruby/2.2.0/gems/spring-1.6.0/lib/spring/application.rb:77:in `preload' from /Users/noriyo_tcp/workspace/feel/vendor/bundle/ruby/2.2.0/gems/spring-1.6.0/lib/spring/application.rb:143:in `serve' from /Users/noriyo_tcp/workspace/feel/vendor/bundle/ruby/2.2.0/gems/spring-1.6.0/lib/spring/application.rb:131:in `block in run' from /Users/noriyo_tcp/workspace/feel/vendor/bundle/ruby/2.2.0/gems/spring-1.6.0/lib/spring/application.rb:125:in `loop' from /Users/noriyo_tcp/workspace/feel/vendor/bundle/ruby/2.2.0/gems/spring-1.6.0/lib/spring/application.rb:125:in `run' from /Users/noriyo_tcp/workspace/feel/vendor/bundle/ruby/2.2.0/gems/spring-1.6.0/lib/spring/application/boot.rb:18:in `<top (required)>' from /Users/noriyo_tcp/.rbenv/versions/2.2.3/lib/ruby/2.2.0/rubygems/core_ext/kernel_require.rb:54:in `require' from /Users/noriyo_tcp/.rbenv/versions/2.2.3/lib/ruby/2.2.0/rubygems/core_ext/kernel_require.rb:54:in `require' from -e:1:in `<main>'
ググっても「bundler入れろ」的な記事しか見つからない。よく見るとSpring関係で怒れらているのかなあとは思うのですが・・・。
Spring止めてみる、というのも試したのですけど、ダメでした。
【Ruby On Rails】 「rails generate」が動かない | approad
railsのコマンドが動かない時はspringを(stop|kill)してみよう - Qiita
続きを読む