2015-03-01から1ヶ月間の記事一覧

Rails カテゴリ分け through

Question, Category, Question_Category という3つのモデルを作ると、 Question生成のときにその数だけcategoryを生成しなくてすむ。 すると、カテゴリ数は一定で決められるので、 カテゴリーごとのQuestionをリストアップできるようになる。 参考リンク: …

Rails 変数をcontrollerで入力するとき paramsがセーブできないときのエラー

変数をcontrollerで入力するときparamsがセーブできないときのエラー create(saveというメソッドも同時にしてしまうメソッド)ではなく、そこでbuildを使ってからsaveさせる。 元のコード: def create @user = current_user @question = Question.find(param…

Rails undefined method `where' for nil:NilClass

undefined method `where' for nil:NilClass コード: - @questions.where(user_id: current_user.id).where(id: id).each do |question|

Rails Facebookシェアできるようにするリンク

もくじ 1:シェアできるようにするリンクの貼り方 2:シェアされるときの画像指定の方法 ー1: <%= link_to image_tag('/images/FaceBookIcon.png'), 'http://www.facebook.com/sharer.php?u='+request.fullpath, :target => :blank %> up vote5down vote…

Haml ページ内リンクのつけ方

HTMLからの自動変換: ビューファイル(リンク元): %a{:href => "file:///Users/HOkaniwa/Desktop/IBplat%202/Questions%23index_category.html#category1", :title => "", :target => "blank"} ・家庭学習 機能するHaml: =link_to "回答する", :action => …

if user logged_in? チュートリアル

Chapter 8: Log in, log out | Ruby on Rails Tutorial (3rd Ed.) | Softcover.io Listing 8.15: The logged_in? helper method.app/helpers/sessions_helper.rb module SessionsHelper # Logs in the given user. def log_in(user) session[:user_id] = use…

Rails投稿者のidを投稿に登録する方法

投稿者のidを投稿に登録する方法 エラーメッセージ:NoMethodError at /users/4/questions/17undefined method `user_id' for #

投稿者しか見れないボタンをつくる

- if current_user.id == @question.user_id = link_to "編集", edit_user_question_path(current_user, @question), :class => "a edit btn btn-primary float_right"

favoritesお気に入り機能 メモ

引数と式を.rb fileで定義 HAMLで代入する値を入れて、式を使う。 このときquestionと連携させたかったので、その引数も"user"のほかにつける必要があった。 HAML; -if @question.favorited_by?(current_user, @question) = link_to "お気に入りを解除", que…

Rails Checkboxの使い方 サイトまとめ3つ

Using checkboxes in railsstackoverflow.com プログラマの覚書: Rails チェックボックスの配列化(修正) 【Rails3.1】複数のチェックボックスの処理 : ノンプログラマーブログ

rails consoleの[END]から抜け出す方法

q とタイプ。

ページ内リンク link_to にクラスをつけたいけれどできない

クラスをつけない場合: =link_to "回答する", :action => 'show', :anchor => 'answer' 参考: link_toでアンカーリンク(ページ内リンク)へ飛ばす方法 - マオの徒然日記gutch.hatenablog.com クラスをつける場合: ????? 失敗したコード: =button_t…

notice、alert 表示

noticeの表示 Settings#editのnotice app/controllers/settings_controller.rb 6 6 def update 7 7 @user = current_user 8 8 if @user.update_attributes(params_user) 9 redirect_to user_url(@user) 9 redirect_to user_url(@user), notice: "プロフィー…

DEBUG model or controller

config/initializers のなかに log_formatter.rbをつくる (内容をこぴぺ) def show loggger.debug params end ほかに、 logger.debug @board.inspect などとする。 ほかに、 def show raise end としてbetter errorsでみる。

logoutしたいとき

Note: method: :delete (:destroyではない!)

Rails 「~さんのページ」と表示したいとき

「~ さんのページ」となってしまう。 コード: %span %h4= @user.screen_name さんのページ エラーになるコード: %h4= @user.screen_name さんのページ NameError at /user.1undefined local variable or method `さんのページ' for #<#<Class:0x007f943901ade0>:0x007f9439739ab8> </class:0x007f943901ade0>…

Rails ログイン時のみの設定 if user_signed_in?

引用: It appears that user_logged_in? Is not used anymore, and instead you need to use user_signed_in? So with the code you gave this should work: <% if user_signed_in? %> <%= render :partial => "home/statusbar" %> <% end %> This code ren…

Rails デフォルトの言語を日本語に設定する

引用: Ruby on Rails実践講座 - Twitterの簡易クローンを開発する デフォルトの言語を日本語に設定する config/application.rb 18 18 19 19 # The default locale is :en and all translations from config/locales/*.rb,yml are auto loaded. 20 20 # conf…

Rails 時間表示をカスタマイズする方法

・日付を本人のパソコンの時計に合わせ ・曜日表記を日本語にする方法 Hamlなどのview ファイルに: Time.now.strftime("%m/%d(#{%w(日 月 火 水 木 金 土)[Time.now.wday]})")例: =answer.created_at.strftime("%m/%d(#{%w(日 月 火 水 木 金 土)[Time.now…

Sorceryを使って新規登録機能をつける(1)

<a href="https://github.com/NoamB/sorcery/wiki" data-mce-href="https://github.com/NoamB/sorcery/wiki">NoamB/sorcery</a>github.com 参考: Ruby on Rails実践講座 - Twitterの簡易クローンを開発するcyllabus.jp 1)bundle install gem 'spring' gem 'pry-rails' gem 'better_errors' gem 'haml-rails' gem 'bootstrap-sass' gem 'font-a…

Git, SourceTreeでpushする

git init git add . git commit -m "first commit" git remote add origin git@github.com:HOkn/ibhiroba.git git push -u origin master <branches> git branch develop git checkout develop(結果:Switched to branch 'develop') <pushする> git push origin master git pus</pushする></branches>…

When assigning attributes, you must pass a hash as an argument

などのエラーの時:edit, update 関数を分ける。 def edit @question = Question.find(params[:question_id]) @answer = @question.answers.find(params[:id]) end def update @question = Question.find(params[:question_id]) @answer = @question.answers…

デフォルトでhaml, sassファイルを作る方法

Application.rb: module HamlDemo class Application < Rails::Application config.generators do |g| g.template_engine :haml end config.sass.preferred_syntax = :sass

link_toタグでブロックをリンクにする方法

%ul %li.float.indextop1 質問 %li.float.indextop2 詳細 %li.float.indextop3 日付 %li.float.indextop4 回答数 .clear %ul - @questions.each do |question| %li = link_to(question) do .float.indexcontent.indexcontent1= question.title[0,17] .float.…

Rails データの初めの数文字を表示させる方法

例:クラス:Question, コラム名:titleのとき初めの17文字を表示するためにはquestion.title[0,17] ほかにクラスを定義することもできる。 参考: Ruby: How to get the first character of a stringstackoverflow.com

views/layouts/application.html.hamlのなか: !!! 5%html%head %title Ibhiroba2 = stylesheet_link_tag 'application', media: 'all', 'data-turbolinks-track' => true = javascript_include_tag 'application', 'data-turbolinks-track' => true = csrf_…

hamlをファイルのデフォルトテンプレートにする

gem 'haml-rails' をinstallする config/locales/のフォルダのapplication.rbで: module HamlDemo class Application < Rails::Application config.generators do |g| g.template_engine :haml endmodule HamlDemo class Application < Rails::Application …

expecting end-of-input

=form_for :question, url: question_path(@question), method: :patch do |f| などの関数が閉じられていない。 SyntaxError in QuestionsController#edit syntax error, unexpected keyword_ensure, expecting end-of-input

Hamlでclass名をつける2:submitボタンにつける

エラー:SyntaxError in QuestionsController#create syntax error, unexpected =>, expecting :: or '[' or '.' ...rue_false(( f.submit, :class => "a btn btn-default" ... ^ もとのコード: %p .box_button = f.submit, :class => "a btn btn-default" …

link_to (Haml)タグにクラス名をつける

syntax error, unexpected keyword_ensure, expecting end-of-input Haml in Rails:画像をリンクにするときのエラー: 間違えたコード: = link_to %a{:href => "file:///Users/HOkaniwa/Desktop/IBplat%202/Questions%23index_home.html"} = image_tag "lo…