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

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

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

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

Q&A

0回答

506閲覧

railsで特定の可変カラムをfindしたい

KosukeYamamoto

総合スコア220

Ruby on Rails

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

0グッド

0クリップ

投稿2020/03/20 03:34

ヘディングのテキストrailsで特定の可変カラムをfindして、値を取得したいです。

sqlite> select * from comments;
id|body|post_id|user_id|created_at|updated_at|Evaluation
37|kosuke|39|37|2020-03-15 08:47:24.171803|2020-03-15 08:47:24.171803|0
38|kosuke|39|37|2020-03-15 08:47:27.133821|2020-03-15 08:47:27.133821|0

上記のcommentテーブルのidを取得して、いいね機能を実装したいです。

37のコメント上でいいねを押したら37をいいねするという形で、可変のidに紐づくいいねを実装したいです。

イメージ説明

class LikesController < ApplicationController def create @post = Post.find(params[:post_id]) @comment = @post.comments.find(params[:comment_id]) @like = current_user.likes.create(id:params[:id],post_id: params[:post_id],comment_id: params[:comment_id],user_id: current_user.id) redirect_back(fallback_location: root_path) end def destroy @post = Post.find(params[:post_id]) @comment = @post.comments.find(params[:comment_id]) @like = Like.find_by(params[:id]) @like.destroy redirect_back(fallback_location: root_path) end end
class CommentsController < ApplicationController def create @post = Post.find(params[:post_id]) @post.comments.create(comment_params.merge(user_id: current_user.id)) redirect_to post_path(@post) end def destroy @post = Post.find(params[:post_id]) @comment = @post.comments.find(params[:id]) @comment.destroy redirect_to post_path(@post) end def update if params[:promote] then @post.comments.increment!(:Evaluation, by = 0) @post.comments.touch redirect_to @post end end private def comment_params params.require(:comment).permit(:body) end # Use callbacks to share common setup or constraints between actions. def set_comment @post.comments = Comment.find(params[:id]) end end

showのビュー

<div class="container"> <div class="row"> <div class= "col-sm-6"> <h2 class="show-title"><%= @post.title %></h2> <% if @post.user == current_user %> <p>質問者:<%= @post.user.name %></p> <% else %> <% if @post.situation? %> <% else %> <p>質問者:<%= @post.user.name %></p> <% end %> <% end %> <a href="https://twitter.com/share" class="twitter-share-button" data-size="large">Tweet</a> <script>!function(d,s,id){var js,fjs=d.getElementsByTagName(s)[0],p=/^http:/.test(d.location)?'http':'https';if(!d.getElementById(id)){js=d.createElement(s);js.id=id;js.src=p+'://platform.twitter.com/widgets.js';fjs.parentNode.insertBefore(js,fjs);}}(document, 'script', 'twitter-wjs'); </script> <div class="show-content"><%= simple_format(@post.content) %> </div> <div class= "sold-out"> <% if @post.user == current_user %> <p class= "sold-out">質問を匿名に変更しますか?<br>(匿名にすると質問のユーザー名が匿名になります)</p> <%= form_for(@post) do |f| %> <%= f.label :situation, 'はい' %> <%= f.radio_button :situation, "true", {:check => true} %> <%= f.label :situation, 'いいえ' %> <%= f.radio_button :situation, "false", {:check => false} %> <%= f.submit "更新する", action: "update", class: "btn-success" %> <% end %> <% end %> </div> <div class="row"> <ul class="list-inline"> <% if @post.user == current_user %> <li class= "edit"><h4><%= link_to "編集", edit_post_path(@post) %></h4></li> <li class= "edit"><h4><%= link_to "削除", @post ,method: :delete , data:{confirm: "本当に削除しますか?"} %></h4></li> <% end %> <li class= "edit"><h4><%= link_to "戻る", :back %></h4></li> </ul> </div> <div class="row"> <div class="answer"> <h4>回答</h4> <% if @post.comments.any? %> <ul> <% @post.comments.each do |comment| %> <% if @post.user == comment.user %> <% if @post.situation? %> <p>質問者</p> <% else %> <p>質問者:<%= @post.user %></p> <% end %> <% else %> <p>回答者:<%= @post.user.name %></p> <% end %> <li> <%= comment.body %> <% if @post.user == current_user %> <%= link_to '回答を削除する', post_comment_path(@post, comment), method: :delete, class: 'command', data: {confirm: '回答を削除しますか?' } %> <% end %> </li> <div class="evalation"> <% if current_user.already_liked?(@post) %> <%= button_to '♡', post_comment_like_path(@post,comment), method: :delete, class: "button" %><%= comment.likes.count %> <% else %> <% if comment.likes.count == 0 %> <%= button_to '♡', post_comment_likes_path(@post,comment), class: "button"%> <% else %> <%= button_to '♡', post_comment_likes_path(@post,comment), class: "button"%><%= comment.likes.count %> <% end %> <% end %> </div> <% end %> </ul> <% end %> <%= form_for([@post, @post.comments.build]) do |f| %> <p> <%= f.text_field :body, class: "show-content" %> </p> <p> <%= f.submit "回答する", class: "comment" %> </p> <% end %> </div> </div> </div> <div class = "col-sm-6"> <div class="row"> <% if @post.image? %> <h3>参考画像</h3> <%= link_to (image_tag @post.image[0].url, class: "img-responsive img placeholder"), @post.link %> </div> <div class="row thumbnails"> <% @post.image.each do |image| %> <div class="col-sm-3 col-xs-4"> <%= link_to (image_tag image.url, class: "img-responsive thumbnails"), image.url %> </div> <% end %> <% end %> </div> </div> </div> <div class="row"> <div class= "col-sm-6 col-md-offset-6 text-center"> </div> </div> <div class="row"> <div class= "attenssions col-sm-6 col-md-offset-6"> <% if @post.user == current_user %> <p>あと<span class="counter"><%= 5 - @post.counter %></span>回PRできます!</p> <% if @post.counter < 5 %> <%= form_for(@post, url:{controller:'posts', action: 'update'}) do |f| %> <%= f.submit "質問をPRする", name: "promote", class:"PR center-block"%> <% end %> <% end %> <% end %> </div> </div> <div class="row"> <div class = "col-sm-12 comp"> <h3 class = "h3-simi">似ている質問</h3> <div class= "grid"> <div class="grid-sizer col-sm-4 col-md-3 col-xs-6"></div> <% @similar.each do |post| %> <div class="grid-item col-sm-4 col-md-3 col-xs-6"> <%= link_to(post) do %> <div class = "posts"> <div class = "image"> <%= image_tag(post.image[0].url, class: "img-responsive") unless post.image.blank? %> </div> <div class = "description"> <ul class = "list-unstyled"> <li class = "title"><%= post.title %></li> <% if post.price? %> <li class = "price">¥ <%= post.price %></li> <% elsif %> <li class = "price"><%= post.price %></li> <% end %> </ul> </div> </div> <% end %> </div> <% end %> </div> </div> </div>

どうぞよろしくお願いいたします。

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

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

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

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

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

guest

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

まだ回答がついていません

会員登録して回答してみよう

アカウントをお持ちの方は

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

ただいまの回答率
85.46%

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

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

質問する

関連した質問