質問をすることでしか得られない、回答やアドバイスがある。

15分調べてもわからないことは、質問しよう!

新規登録して質問してみよう
ただいま回答率
85.48%
Haml

Haml(HTML abstraction markup language)は、HTML/XHTMLを効率的に記述するためのマークアップ言語および記法です。

Ruby

Rubyはプログラミング言語のひとつで、オープンソース、オブジェクト指向のプログラミング開発に対応しています。

Ruby on Rails

Ruby on Railsは、オープンソースのWebアプリケーションフレームワークです。「同じことを繰り返さない」というRailsの基本理念のもと、他のフレームワークより少ないコードで簡単に開発できるよう設計されています。

HTML

HTMLとは、ウェブ上の文書を記述・作成するためのマークアップ言語のことです。文章の中に記述することで、文書の論理構造などを設定することができます。ハイパーリンクを設定できるハイパーテキストであり、画像・リスト・表などのデータファイルをリンクする情報に結びつけて情報を整理します。現在あるネットワーク上のほとんどのウェブページはHTMLで作成されています。

Q&A

解決済

1回答

1023閲覧

collection_check_boxesに表示する条件を設定したい

a-ichi

総合スコア16

Haml

Haml(HTML abstraction markup language)は、HTML/XHTMLを効率的に記述するためのマークアップ言語および記法です。

Ruby

Rubyはプログラミング言語のひとつで、オープンソース、オブジェクト指向のプログラミング開発に対応しています。

Ruby on Rails

Ruby on Railsは、オープンソースのWebアプリケーションフレームワークです。「同じことを繰り返さない」というRailsの基本理念のもと、他のフレームワークより少ないコードで簡単に開発できるよう設計されています。

HTML

HTMLとは、ウェブ上の文書を記述・作成するためのマークアップ言語のことです。文章の中に記述することで、文書の論理構造などを設定することができます。ハイパーリンクを設定できるハイパーテキストであり、画像・リスト・表などのデータファイルをリンクする情報に結びつけて情報を整理します。現在あるネットワーク上のほとんどのウェブページはHTMLで作成されています。

0グッド

0クリップ

投稿2020/12/28 10:58

form_withのヘルパーメソッドの
collection_check_boxesを使って、チェックボックスを作成しようとしています。

そのチェックボックスを表示する条件を指定したいのですが、
その方法が分からなかった為、質問させて頂きました。

・実装したい事
ログインしているユーザーが登録したcustomerの名前だけを
チェックボックスにして表示したい
(current_user.id == user.id
のcustomerテーブルのnameをチェックボックスにしたい。)

・試した事
collection_check_boxesの前にcurrent_user.id == user.idをif文を入れてみましたが、
エラーで反映しませんでした。
エラー文
ActionView::SyntaxErrorInTemplate in EventsController#new
Encountered a syntax error while rendering template: check .Contents .Container %h3 イベント追加 = form_with(model: @event, local: true) do |f| = f.text_field :title, placeholder: "タイトル" = f.text_area :body, placeholder: "メモ", rows: "5" = f.datetime_select :start = f.datetime_select :end -if current_user.id == customer.user_id = f.collection_check_boxes :customer_ids, Customer.all, :id, :name = f.submit "追加"

new.html.haml

haml

1Contents 2 .Container 3 %h3 イベント追加 4 = form_with(model: @event, local: true) do |f| 5 = f.text_field :title, placeholder: "タイトル" 6 = f.text_area :body, placeholder: "メモ", rows: "5" 7 = f.datetime_select :start 8 = f.datetime_select :end 9 = f.collection_check_boxes :customer_ids, Customer.all, :id, :name 10 = f.submit "追加" 11

events_controller.rb

ruby

1class EventsController < ApplicationController 2 before_action :set_event, only: [:show, :edit, :update, :destroy] 3 before_action :move_to_index,except:[:index] 4 5 6 7 def index 8 @events = Event.all 9 @customer = Customer.all 10 end 11 12 def new 13 @event = Event.new 14 end 15 16 def create 17 @event = Event.create(event_params) 18 19 if @event.save 20 redirect_to root_path, notice: "イベントを登録しました" 21 else 22 redirect_to new_event_path, notice: "登録出来ませんでした" 23 end 24 end 25 26 def destroy 27 if @event.destroy 28 redirect_to root_path, notice: 'イベントを削除しました' 29 else 30 flash.now[:alert] = 'イベントを削除できませんでした' 31 render :show 32 end 33 end 34 35 def edit 36 end 37 38 def update 39 if @event.update(event_params) 40 redirect_to root_path, notice: 'イベントを編集しました' 41 else 42 flash.now[:alert] = '必須事項を入力してください' 43 render :edit 44 end 45 end 46 47 def show 48 end 49 50 51 52 private 53 54 def set_event 55 @event = Event.find(params[:id]) 56 end 57 58 def event_params 59 params.require(:event).permit(:title, :body, :start, :end, :user, customer_ids:[]).merge(user_id: current_user.id) 60 end 61 62 def move_to_index 63 unless user_signed_in? 64 redirect_to action: :index 65 end 66 end 67 68end 69

customerテーブル

ruby

1class CreateCustomers < ActiveRecord::Migration[6.0] 2 def change 3 create_table :customers do |t| 4 t.string :name 5 t.string :current_address 6 t.string :building_site 7 t.string :phone 8 t.text :body 9 t.references :user, foreign_key: true 10 t.timestamps 11 end 12 end 13end

eventテーブル

ruby

1class CreateEvents < ActiveRecord::Migration[6.0] 2 def change 3 create_table :events do |t| 4 t.string :title 5 t.string :body 6 t.datetime :start 7 t.datetime :end 8 t.references :user,foreign_key: true 9 t.timestamps 10 end 11 end 12end 13

customer_eventsテーブル

ruby

1class CreateCustomerEvents < ActiveRecord::Migration[6.0] 2 def change 3 create_table :customer_events do |t| 4 t.references :customer, foreign_key: true 5 t.references :event, foreign_key: :true 6 t.timestamps 7 end 8 end 9end 10

環境
rails (6.0.3.4)
devise利用

collection_check_boxesを諦めて別の方法を探すべきでしょうか?

調べても実装方法が分かりませんでしたので、質問させて頂きました。
宜しくお願い致します。

気になる質問をクリップする

クリップした質問は、後からいつでもMYページで確認できます。

またクリップした質問に回答があった際、通知やメールを受け取ることができます。

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

guest

回答1

0

ベストアンサー

Customer.all の所を
Customer.where(user_id: current_usr.id)

投稿2020/12/28 11:09

winterboum

総合スコア23329

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

a-ichi

2020/12/28 11:37

ありがとうございます! whereで指定出来るんですね。 findではエラーが出たので、ここはallしか使えないのかと思っていました。 勉強になりました。
guest

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

15分調べてもわからないことは
teratailで質問しよう!

ただいまの回答率
85.48%

質問をまとめることで
思考を整理して素早く解決

テンプレート機能で
簡単に質問をまとめる

質問する

関連した質問