2011-06-27 3 views
0

で送信:http://www.communityguides.eu/articles/1RoRの:コントローラ私はこのチュートリアルでエントリー提出コントローラを書いているのparams

私は、エントリを提出しようとすると、私はUsers can't be blankを取得...私は中のuser_idを渡したくありません隠されたフィールドとして、右か?だから私は自動的にユーザーのIDを取得するためにこれを変更する必要がありますか?

私は認証にdeviseを使用しています。 :)と私は完全なレールの初心者です。新しいエントリを作成しようとしたときにこのエラーが発生したであろうと思わあなたの説明から、

def submit 
     @entry = current_user.articles.find(params[:id]) 

     # submit only, if article is currently in draft or rejected-state 
     if (@entry.state == 0) or (@article.state == 2) 
     @entry.state = 1 
     @entry.submitted = Time.now 

     if @entry.save 
      flash[:notice] = 'Your article was successfully submitted for approval.' 
     else 
      flash[:error] = 'There was an error while submitting your article.' 
     end   
     else 
     flash[:error] = 'This article can not be submitted.' 
     end 

     respond_to do |format| 
     format.html { redirect_to(:action => 'myarticles') } 
     format.xml { head :ok } 
     end 
    end 
    # GET /entries/1/edit 
    def edit 
    @entry = Entry.find(params[:id]) 
    end 

    # POST /entries 
    # POST /entries.xml 
    def create 
    @entry = Entry.new(params[:entry]) 

    respond_to do |format| 
     if @entry.save 
     format.html { redirect_to(@entry, :notice => 'Entry was successfully created.') } 
     format.xml { render :xml => @entry, :status => :created, :location => @entry } 
     else 
     format.html { render :action => "new" } 
     format.xml { render :xml => @entry.errors, :status => :unprocessable_entity } 
     end 
    end 
    end 

    # PUT /entries/1 
    # PUT /entries/1.xml 
    def update 
    @entry = current_user.entries.find(params[:id]) 

     #if the entry has been approved, the user cannot change the title or URL. 
     if @entry.state > 2 
      params[:entry].delete(:title) 
      params[:entry].delete(:url) 
     end 
    respond_to do |format| 
     if @entry.update_attributes(params[:entry]) 
     format.html { redirect_to(@entry, :notice => 'Entry was successfully updated.') } 
     format.xml { head :ok } 
     else 
     format.html { render :action => "edit" } 
     format.xml { render :xml => @entry.errors, :status => :unprocessable_entity } 
     end 
    end 
    end 

答えて

0

: これは私のコントローラです。

def create 
    @entry = current_user.entries.new(params[:entry]) # <-- Scope to the current user 

    respond_to do |format| 
     if @entry.save 
     format.html { redirect_to(@entry, :notice => 'Entry was successfully created.') } 
     format.xml { render :xml => @entry, :status => :created, :location => @entry } 
     else 
     format.html { render :action => "new" } 
     format.xml { render :xml => @entry.errors, :status => :unprocessable_entity } 
     end 
    end  
    end 
+0

また、編集操作でも同じことをしたいと思うでしょう。 '@entry = current_user.entries.find(params [:id])'ユーザが別のユーザのエントリを編集できないようにする。 –

+0

Alrightie!ありがとう。 :)あなたは私のためにもう少し説明をしますか?私が言ったように、私は新しいです - 私はあなたが 'スコープ 'の意味を正確に理解していません... –

+1

' Entry.find(params [:id]) 'はあなたのデータベースのエントリレコードを見つけることができます非常に多くの場合、ユーザーは、ユーザーがそのレコードに属しているレコードにしかアクセスしないようにします。上の編集アクションでは、IDを知っている(または推測している)ユーザーはすべてのエントリを編集できます。現在のユーザーに属しているエントリのみを返すようにfindを制限することで、この問題を回避するのが一般的な方法です。 'current_user.entries.find(params [:id])'は、 'WHERE entries.user_id = 123'のようなSQLクエリにWHERE条件を追加するだけでそれを行います –

関連する問題