2016-07-20 2 views
0

ここに新しいレールがあります。私は作品と呼ばれるモデルを持っているRoRのアプリを持っています。これらの要素には、statusという整数パラメータがあります。ピースが作成または更新されるたびに、deviseのcurrent_user.idメソッドが1に等しくない場合、piece.statusを2に設定します。これはcreateメソッドで機能しますが、同様の行をupdateメソッド、 何も起こりません。私は何をすべきか?更新時にモデルパラメータを設定するruby on rails

は、ここに私のコントローラ

class PiecesController < ApplicationController 
    before_action :set_piece, only: [:show, :edit, :update, :destroy] 
    before_action :authenticate_user!, except: [:index, :show] 
    respond_to :html 

def index 
    @pieces = Piece.all 
    respond_with(@pieces) 
    end 

    def show 
    respond_with(@piece) 
    end 

    def new 
    @piece = current_user.pieces.build 
    respond_with(@piece) 
    end 

    def edit 
    end 

    def create 
    @piece = current_user.pieces.build(piece_params) 
    @piece.status = 2 if current_user.id != 1 
    flash[:notice] = 'Piece was successfully created.' if @piece.save 
    respond_with(@piece) 
    end 

    def update 
    flash[:notice] = 'Piece was successfully updated.' if @piece.update(piece_params) 
    respond_with(@piece) 
    end 

    def destroy 
    flash[:notice] = 'Piece was successfully deleted.' if @piece.destroy 
    respond_with(@piece) 
    end 

    private 
    def set_piece 
     @piece = Piece.find(params[:id]) 
    end 

    def piece_params 
     params.require(:piece).permit(:title, :image, :genre, :size, :price, :description, :status) 
    end 
end 

みんなありがとうです!

答えて

0
class PiecesController < ApplicationController 
    def update 
    @piece.attributes = piece_params 
    @piece.status = 2 if current_user.id != 1 
    if @piece.save 
     flash[:notice] = 'Piece was successfully updated.' 
     respond_with(@piece) 
    end 
    end 
end 
+0

申し訳ありません、Jagdeep。 devise current_user.id!= 1の場合、このアクションを設定したかったということを忘れていました。私はcurrent_user.id!= 1の場合self.status = 2と言ってこれを試しました。エラーが発生しました。 –

+0

' current_user'をモデルに追加します。だから、うまく動作しません。 –

+0

正確に。だから私はこれをどのように実装できますか?コントローラーで?これは私の作成メソッドでは機能しますが、私の更新メソッドでも同様に動作する必要があります。 –

0

これを試してみてください。

def update 
    @piece = current_user.pieces.find(params[:id]) 
    @piece.status = 2 if current_user.id != 1 
    if @piece.save 
    flash[:notice] = 'Piece was successfully updated.' 
    respond_with(@piece) 
    end 

end 
0
  1. peicesにペイロードフィールドを追加するには、移行ファイルを作成します。お使いのコントローラのアクションで

class AddPayloadToPeices < ActiveRecord::Migration 
    def change 
     add_column :peices, :payload, :jsonb 
    end 
end 

2.今設定ペイロード:Peiceモデルで今

peice.payload = { current_user_id: current_user.id } 

3:

before_save :set_status, if: 'payload[:current_user_id] && payload[:current_user_id] != 1' 

def set_status 
    status = 2 
end 

0

@Jagdeep Singhの答えは多くの助けとなり、私の作品のフィールドの検証を追加するまで働いていました。これらのフィールドが更新アクションで検証を満たさなかった場合は、テンプレートなしエラーが発生します。したがって、これを実行する最善の方法は次のとおりです。

def update 
    @piece.attributes = piece_params 
    @piece.status = 2 if current_user.id != 1 
    if @piece.save 
    flash[:notice] = 'Piece was successfully updated.' 
    respond_with(@piece) 
    else 
    render :edit 
    end 
end 
関連する問題