开发者

RoR: Controller Submit with params

开发者 https://www.devze.com 2023-03-15 15:30 出处:网络
I\'m writing an entry submission controller by this tutorial: http://www.communityguides.eu/articles/1

I'm writing an entry submission controller by this tutorial: http://www.communityguides.eu/articles/1

When I try to submit an entry, I get Users can't be blank...I don't want to pass the user_id in as a hidden field, right? So how should I change this to automatically get the user's id?

I'm using devise for authentication. :) And I'm a complete rails novice. This is my controller:

    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


From your description it seems that you would encounter this error when trying to create a new Entry.

  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

精彩评论

暂无评论...
验证码 换一张
取 消