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

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

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

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

Ruby on Rails 4

Ruby on Rails4はRubyによって書かれたオープンソースのウェブフレームワークです。 Ruby on Railsは「設定より規約」の原則に従っており、効率的に作業を行うために再開発を行う必要をなくしてくれます。

Q&A

解決済

1回答

2419閲覧

hiddenを使わずにsessionでコメントを作成する方法が知りたい。

NaojirouHisada

総合スコア60

Ruby on Rails

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

Ruby on Rails 4

Ruby on Rails4はRubyによって書かれたオープンソースのウェブフレームワークです。 Ruby on Railsは「設定より規約」の原則に従っており、効率的に作業を行うために再開発を行う必要をなくしてくれます。

0グッド

0クリップ

投稿2016/05/01 16:37

編集2016/05/07 17:13

###実装内容
現在Facebookのようなアプリを作成中です。
コメントの表示・投稿・保存の機能は既に実装していますが、
現在hiddenフィールドを使用してるので、セキュリティー
を考えてsessionによる外部キーの値の挿入を行いたい。
###背景
ネットの記事やある講師から、
hiddenフィールドはセキュリティー上良くないため、あまり使わないよう言われていた。
その際、sessionを使った方法をするとよいと書かれていました。
そのため、sessionを使用しようと思いました。
###問題点
sessionを使おうとは書いてあったが、
具体的な方法は記載されておらず、
やり方がわかりません。

どのようにコードを記述して、
sessionによるコメントの保存をさせるのでしょうか?
お手数おかけしますが、
アドバイス宜しくお願いします。

###ソースコード
コメントの投稿フォームは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 %>
userController class UsersController < ApplicationController before_action :set_user, only: [:show , :edit , :update ] def show @user = User.find(params[:id]) @microposts = @user.microposts @comments = @user.comments.build @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
_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 %>

お手数おかけしますが、
宜しくお願いします。

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

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

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

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

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

guest

回答1

0

ベストアンサー

つぎのようなページを読んで、 セッションについて学ぶと良いと思います。
参考情報

投稿2016/05/01 22:06

katoy

総合スコア22324

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

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

NaojirouHisada

2016/05/02 00:57

katoyコメントありがとうございます。 早速拝見してチャレンジしてみます(^^)
guest

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

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

ただいまの回答率
85.50%

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

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

質問する

関連した質問