###やりたいこと
現在Facebookのようなアプリを作成中です。
コメントの表示・投稿・保存の機能は既に実装していますが、
現在hiddenフィールドを使用してるので、セキュリティー
を考えてsessionによる外部キーの値の挿入を行いたい。
###具体的にやりたいこと
_comment.html.erbで現在は、
hideenに @micropost.id を入れて、
Comment.createメソッドに送信して、
どの記事に対するコメントなのか識別していますが、
この 個々の@micropost.id をsessionを使ってComment.createメソッドに値を引き回し、
どの記事に対するコメントなのか識別したいです。
現状@micropostIds = Micropost.where(user_id: @user.id).select(:id)
でユーザに属する記事の一覧は取得できるのですが、
それをコメント記事が送信された際に、
どの記事に対するコメントなのか識別できないのに、困っております。
###背景
ネットの記事やある講師から、
hiddenフィールドはセキュリティー上良くないため、あまり使わないよう言われていた。
その際、sessionを使った方法をするとよいと書かれていました。
そのため、sessionを使用しようと思いました。
###問題点
コントローラ内で、
記事の複数のIdを取得することができたので、
可逆方式を使って暗号化し、
hiddenに入れてかつ、validationを試みましたが、
どうしても特定の値をcommentコントローラに渡せません。
Railsガイドや、
他のsession使用のサイトを見たのですが、
どれもユーザのように1つしかないもののsessionの使い方ばかりで、
記事のように複数の中から特定のものを持ってくるのはありませんでした。
また、
Ruby on Rails4アプリケーションプログラミングの本を読み返して、
試行錯誤したのですが、
結局2週間経っても
未だに解決できません(^^;)
###ソースコード
コメントの投稿フォームはshow.html.erbにレンダーする形で表示します。
show.html.er <%= @user.first_kanji%> <%= link_to '基本情報編集' ,edit_user_path(@user) %> <%= render 'follow_form' if logged_in? %> <hr /> <% if @user.microposts.any? %> <%= render partial: 'microposts/micropost', collection: @microposts %> <% end %>
class UsersController < ApplicationController before_action :set_user, only: [:show , :edit , :update ] def show @user = User.find(params[:id]) session[:other_user_id] = nil @microposts = @user.microposts @comments = @user.comments.build @micropostIds = Micropost.where(user_id: @user.id).select(:id) session[:micropost] = @micropostIds @replay = Replay.new if current_user.follower_relationships.any? if @user.id == current_user.id @follower_relationships = current_user.follower_relationships.find_by( params[:followed_id] == current_user.id) else @follower_relationships = current_user.follower_relationships.find_by(params[:follow_id] == @user.id , params[:followed_id] == current_user.id) end end if current_user.following_relationships.any? if @user.id == current_user.id @following_relationships = current_user.following_relationships.find_by(params[:follow_id] == current_user.id ) else @following_relationships = current_user.following_relationships.find_by(params[:follow_id] == current_user.id , params[:followed_id] == @user.id ) end end end
comments.controller.rb class CommentsController < ApplicationController before_action :logged_in_user def create @user = User.find(params[:user_id]) @micropost = Micropost.find(params[:micropost_id]) @comment = current_user.comments.build(comment_params) @comment.micropost = @micropost if @comment.save @succeed = true else flash[:danger] = 'missing' end end private def comment_params params.require(:comment).permit(:content) end end
_micropost.html.erb <div id="micropost<%=micropost.id %>"> <% @micropost = micropost %> <p><%= micropost.user.first_kanji %> <%= micropost.user.last_kanji %>さん<br /> <%= micropost.content %> <% if current_user.following?(@user) && @following_relationships.status == 1 || current_user.follower?(@user) && @follower_relationships.status == 1 %> <%= render partial:'shared/comment_form' , locals:{comments: @micropost.comments} %> <% end %> <div id="comments<%=micropost.id %>"> <%= render partial: 'comments/comment' ,collection: @micropost.comments %> </div> <%- if current_user == micropost.user %> <%= link_to "削除", micropost , method: :delete , data:{confirm: "削除してよろしいですか?"} %> <% end %> </div> <hr />
_comment_form.html.erb <%= form_for(@comments,remote: true) do |f| %> <%= f.text_field :content , class: "comment_field" %> <%= hidden_field_tag :micropost_id , @micropost.id %> <%= hidden_field_tag :user_id , current_user.id %> <%= f.submit 'コメントする' %> <% end %>
このような場合、
javascript等で送信時に値を設定するみたいなことを
するのでしょか??
お手数おかけしますが、
アドバイス宜しくお願いします。
回答1件
あなたの回答
tips
プレビュー