Coder Social home page Coder Social logo

Comments (7)

Yukimarupie avatar Yukimarupie commented on August 17, 2024

使い分け

・check_box 関連するモデルがある時
・f.check_box 関連するモデルがある時、かつform_forまたはform_withの中で使う際
・check_box_tag 関連するモデルがない時
・f.check_box_tag 関連するモデルがない時、かつform_forまたはform_withの中で使う際
・collection_check_boxes すでにあるモデルからチェックボックスを自動生成。多対多のリレーションのモデルを表現する時に便利

check_box関連のタグの使い方

・Railsドキュメント「チェックボックスを生成
check_boxやcheck_box_tagの使い方@個人ブログ

from pomo.

Yukimarupie avatar Yukimarupie commented on August 17, 2024

今まで参考にしたサイト達

ルーティングで大体
memberとかpatchとか使ってる。

【Rails5】ajaxでcheck_boxのboolean値を切り替える

check_box Ajax
で検索して出てきたこのサイトでできるかも?

    <%= check_box_tag 'user[private]', true, @user.private, {
        onchange: '$(this).data("params", "user[private]=" + this.checked)',
        data: {
            url: user_url + '/user_private/',
            method: :post,
            remote: true
        }
    } %>

Rails change boolean value with checkbox and jquery ajax @stack over flow 2013年

めっちゃ前のやつ。check_box_tagが使われている。

    <%= check_box_tag 'completed', task.id , task.completed, :class => "task-check" %>

jQueryだけど

$(".task-check").bind('change', function(){
  if (this.checked){
    var bool = this.checked ? 1 : 0;
    $.ajax({
      url: '/todos/toggle',
      type: 'POST',
      data: '{"task_id":"'+ this.value +'", "bool":"'+ bool +'"}'
    });
  }
  else {
    alert("no");
  }
});
<%= check_box_tag 'completed', task.id, task.completed,
      data: {
        remote: true,
        url: url_for(action: :toggle, id: task.id),
        method: "POST"
      } %>

Passing rails variable in a onclick function of check_box_tag @stack over flow 2012年

<%= check_box_tag(joueur.id, joueur.id, false, :onclick=>"addPlayerToField(#{joueur.gch}, #{joueur.drt});")%>

さくらのレンタルサーバーでRuby on Railsを動かしてみたらかなり遅いかも from 2007年

 <%= f.check_box :done %>

How to complete a task via json using a check_box?

謎にviewをinputタグで実装してるし質問の回答もついてないので、コントローラーやルーティングのみ参考

resources :commitments do
    resources :tasks  do
        member do
            patch :done
        end
    end
end
$('#task #task-<%= @task.id %>').html("<%= j render 'tasks/index' %>")
            <input type="checkbox" class="custom-control-input" id="checklist-item-<%= task.id %>" checked>
            <label class="custom-control-label" for="checklist-item-<%= task.id %>"></label>

Build A Cool To-Do List App With Ruby On Rails!

Youtube。チェックが入ったら、表の色が変わる実装をしてる。
doneカラムをboolean型で作る、ってとこまでは参考にした。

<% if list.completed%>

Trying to mark completed items back to incomplete ruby on rails @stack over flow

Patchが使われてる。

  <div class="complete">
    <%= link_to complete_todo_list_todo_item_path(@todo_list, todo_item.id), method: :patch do %>
     <i class="fa fa-circle-thin"></i>
    <% end %>
  </div>
  resources :todo_lists do
    resources :todo_items do
      member do
        patch :complete
      end
    end

    resources :todo_items do
      member do
        get :incomplete
      end
    end

  end

  root "todo_lists#index"

end

Todoアイテムの更新と削除を実装する

これは純粋なJSのみで作るコードなので、あまりRailsとの連携という面ではやくに立たないかもしれない

checkboxでAjaxで更新(PATCH)処理 @個人ブログ 2014年

コントローラーでmethodがPATCHだった場合〜みたいに分けられるん?っていう発見。

    <%= f.check_box(:chk) %>↓
  def update
    if @memo.update(memo_params)
      @status = "O.K."
    else
      @status = "N.G."
    end
    if request.method == "PATCH"
      render :text => @status
    else
      render template: "memos/update.js.erb"
    end
  end

TIL: How to do JavaScript onclick from Rails checkbox form

あんまり読んでないけど、onclickイベントを実装してる?

checkbox_tag('item_list[]',
             value = item.id,
             checked = false,
             { disabled: false,
               onclick: "alert('hello world')" })

from pomo.

Yukimarupie avatar Yukimarupie commented on August 17, 2024

共通すること

・大体PATCHが使われてる。

・PATCHメソッド: ソースへの部分的な変更を適用する。
・POSTメソッドはリソースの作成、PUTメソッドは更新というよりは置換、PATCHメソッドは既存のリソースを更新・変更・修正、
・PATCHはupdateの処理と似ているらしい

というところまでは調べられた。

疑問点

・そのPATCHを使えばコントローラーには
「ToDoリストを単純に編集して新しい文字列に更新するupdateアクション①」と、
「ToDoリストを未完了から完了に変更して更新するupdateアクション② 」を作成する必要がなくなるのか

・そもそもmemberを使って整形する意味って何ぞ?
普通にpatch :doneじゃダメなの..??

・ルーティングでmemberが使われてる。

member: ルーティングにidが使われない、 collection: idが使われる、ところまで調べられた。

疑問点

今回の場合、memberでいいのか。

from pomo.

Yukimarupie avatar Yukimarupie commented on August 17, 2024

その他疑問

check_boxを使うのか、check_box_tagを使うのか。安定しない感じ。
関連するモデルがあるときはcheck_boxを使い、ないときはcheck_box_tagを使うって書いてあったのに
なんでみんなタスクモデルと関連してるのにcheck_box_tag使ってるんだろう?

from pomo.

Yukimarupie avatar Yukimarupie commented on August 17, 2024

<%= check_box :task, :done, {:onchange => @task.done, class: "check_box"} %>
<%= check_box_tag 'task[done]', true, @task.done, {
onchange: '$(this).data("params", "user[private]=" + this.checked)',
data: {
method: :patch,
remote: true
}
} %>

from pomo.

Yukimarupie avatar Yukimarupie commented on August 17, 2024

解決策

check_box_tagやcheck_boxではなく、アイコンでリンクを貼ることにした。

    <%= link_to done_task_path(task),
              method: :patch,
              data: { confirm: '完了しましたか?', remote: true } do %>
      <i class="far fa-check-circle"></i>
    <% end %>

from pomo.

Yukimarupie avatar Yukimarupie commented on August 17, 2024

doneアクションを実装 & indexアクションに完了タスク未完了タスクを取ってくる実装を追加。

  def index
    @tasks_undone = Task.where(done: false)
    #今日の日付より大きいもの
    @tasks_done = Task.where(done: true).where('created_at >= ?', Time.current.beginning_of_day).order(id: "DESC")
    @task = Task.new #remote: trueのために追加
  end

  def done
    @task.update(done: true) #doneをtrueに。 done.js.erbは指定せずとも自動で呼び出される
  end

from pomo.

Related Issues (5)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.