初めまして。
ruby on railsのモデルのデータ処理に関してのご質問です。
ゲームのフレンド募集掲示板を作成しており、登録したゲームのユーザーIDが同一の場合のみ、データを上書きするようにしたいです。
「user_id」が例えば、「11111」とした場合、再度友達を募集しようとしたときにそのkeyを基に上書きをしたいです。
弱点は、荒らしが現れた時に同一のIDのみ上書きというやり方だと、簡単に変更されるなという危惧はありますが、方法が思いつきません。
モデルの状況は下記のようになっています。
class CreatePosts < ActiveRecord::Migration def change create_table :posts do |t| t.integer :user_id t.integer :rank t.string :monster_name t.string :monster_recruit def change add_index :user_id, unique: true end t.timestamps null: false end end end
コントローラーになります。
before_action :set_post, only: [:show, :edit, :update, :destroy] def create @post = Post.new(post_params) respond_to do |format| if @post.save format.html { redirect_to @post, notice: 'Post was successfully created.' } format.json { render :show, status: :created, location: @post } else format.html { render :new } format.json { render json: @post.errors, status: :unprocessable_entity } end end end private # Use callbacks to share common setup or constraints between actions. def set_post @post = Post.find(params[:id]) end
現在はscaffoldでサンプルを作っている状況です。
どうかよろしくお願いしますm(_ _)m
回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2015/08/23 10:09 編集