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

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

新規登録して質問してみよう
ただいま回答率
85.46%
Ruby on Rails 5

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

Ruby

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

Q&A

解決済

1回答

397閲覧

選択できないチェックボックスをdisabledにしたい

NASKA--

総合スコア21

Ruby on Rails 5

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

Ruby

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

0グッド

0クリップ

投稿2021/12/09 00:27

会員に当番を割り振る機能をつけているのですが、各当番には1人ずつしかつくことができず、他の会員に同じ当番を登録しようとするとエラーメッセージが出るようになるというところまでは実装できました。
ですが、ここから、同じ当番を選択することができないのなら、チェックボックスを最初からdisabledにして選択できないようにしたいのですが、その実装がうまく行きませんどのように書けばよいでしょうか
member.rb

class Member < ApplicationRecord has_secure_password has_many :entries, dependent: :destroy has_one_attached :profile_picture attribute :new_profile_picture attribute :remove_profile_picture, :boolean has_many :votes, dependent: :destroy has_many :voted_entries, through: :votes, source: :entry has_many :duties, dependent: :nullify attribute :new_duty_ids, :intarray, default: [] validates :number, presence: true, numericality: { only_integer: true, greater_than: 0, less_than: 100, allow_blank: true }, uniqueness: true validates :name, presence: true, format: { with: /\A[A-Za-z][A-Za-z0-9]*\z/, allow_blank: true, message: :invalid_member_name }, length: { minimum: 2, maximum: 20, allow_blank: true }, uniqueness: { case_sensitive: false } validates :full_name, presence: true, length: { maximum: 20 } validates :email, email: { allow_blank: true } validates :birthday, date: { before: Date.today } attr_accessor :current_password validates :password, presence: { if: :current_password } validate do new_duty_ids.each do |i| m = Duty.find(i).member_id if m && self.id != m errors.add(:new_duty_ids, :uniqueness_duty) end end end validate if: :new_profile_picture do if new_profile_picture.respond_to?(:content_type) unless new_profile_picture.content_type.in?(ALLOWED_CONTENT_TYPES) errors.add(:new_profile_picture, :invalid_image_type) end else errors.add(:new_profile_picture, :invalid) end end before_save do if new_profile_picture self.profile_picture = new_profile_picture elsif remove_profile_picture self.profile_picture.purge end self.duty_ids = new_duty_ids end after_initialize do |member| self.new_duty_ids.replace(duty_ids) end def votable_for?(entry) entry && entry.author != self && !votes.exists?(entry_id: entry.id) end class << self def search(*query) rel = order("number") if query[0].present? rel = rel.where("name LIKE ? OR full_name LIKE ?", "%#{query[0]}%", "%#{query[0]}%") end if query[1].present? rel = rel.where(sex: query[1]) end rel end end end

_member_form.html.erb

<%= render "shared/errors", obj: @member %> <table class="attr"> <tr> <th><%= form.label :new_profile_picture %></th> <td> <div><%= form.file_field :new_profile_picture %></div> <% if @member.profile_picture.attached? %> <div> <%= image_tag @member.profile_picture.variant(resize: "128x128") %> <%= form.check_box :remove_profile_picture %> <%= form.label :remove_profile_picture %> </div> <% end %> </td> </tr> <tr> <th><%= form.label :number %></th> <td><%= form.text_field :number, size: 8 %></td> </tr> <tr> <th><%= form.label :name %></th> <td><%= form.text_field :name %></td> </tr> <tr> <th><%= form.label :full_name %></th> <td><%= form.text_field :full_name %></td> </tr> <tr> <th><%= Member.human_attribute_name(:sex) %></th> <td> <%= form.radio_button :sex, 1 %> <%= form.label :sex_1 %> <%= form.radio_button :sex, 2 %> <%= form.label :sex_2 %> </td> </tr> <tr> <th><%= form.label :birthday, for: "member_birthday_1i" %></th> <td><%= form.date_select :birthday, start_year: 1940, end_year: Time.current.year, use_month_numbers: true %></td> </tr> <tr> <th><%= form.label :email %></th> <td><%= form.text_field :email %></td> </tr> <% if @member.new_record? %> <tr> <th><%= form.label :password, "パスワード" %></th> <td><%= form.text_field :password %></td> </tr> <% end %> <% if controller.kind_of?(Admin::MembersController) %> <tr> <th><%= Member.human_attribute_name(:administrator) %></th> <td> <%= form.check_box :administrator %> <%= form.label :administrator %> </td> </tr> <% end %> <tr> <th><%= form.label :duty, "当番" %></th> <td> <% Duty.all.each do |duty| %> <%= form.check_box :new_duty_ids, { multiple: true }, duty.id, "" %><%= duty.dutyname %>&emsp; <% end %> </td> </tr> </table>

変更したい箇所
_member_form.html.erb

<tr> <th><%= form.label :duty, "当番" %></th> <td> <% Duty.all.each do |duty| %> <%= form.check_box :new_duty_ids, { multiple: true }, duty.id, "" %><%= duty.dutyname %>&emsp; <% end %> </td> </tr>

このように作成しているのですが、どう変更したらよいでしょうか

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

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

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

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

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

guest

回答1

0

自己解決

<% if duty.member_id == @member.id || duty.member_id == nil %>

このif文を使用することで実装することができました。

投稿2021/12/09 00:30

NASKA--

総合スコア21

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

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

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

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

ただいまの回答率
85.46%

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

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

質問する

関連した質問