RoR: Контроллер Отправить с параметрами - PullRequest
0 голосов
/ 28 июня 2011

Я пишу контроллер представления записи этим учебником: http://www.communityguides.eu/articles/1

Когда я пытаюсь отправить запись, я получаю Users can't be blank ... Я не хочу передавать user_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

1 Ответ

0 голосов
/ 28 июня 2011

Из вашего описания кажется, что вы столкнетесь с этой ошибкой при попытке создать новую запись.

  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
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...