0

当我在浏览器中 访问它时,我有一个publish添加到现有的控制器。posts_controller.rbhttp://localhost:3000/posts/15/publishMissing required Parameter:post

我不确定我错过了什么。有人能指出拼图中缺失的部分吗?先感谢您

post_controller.rb

  # PUBLISH /post/1/publish
  def publish
     @post = Post.find(params[:id])
     @post.publish = true
     @post.published_at = Time.now.utc
     respond_to do |format|
       if @post.update_attributes(post_params)
         format.html 
       else
         format.html { redirect_to posts_url, notice: 'Something went wrong. Try again.' }
       #  format.json { render json: @post.errors, status: :unprocessable_entity }
       end
     end   
  end

发布.html.erb

  <h1>PUBLISHED POST</h1>

路线.rb

  match 'posts/:id/publish' => 'posts#publish' , :as => :publish
  match 'posts/:id/unpublish' => 'posts#unpublish', :as => :unpublish
  resources :posts

我正在使用 strong_parameters & 这是在 posts_controller.rb 中明确定义的允许参数

 def post_params
      params.require(:post).permit(:id, :rental_type, :rent_amt, :street_address, :city, :state, :description, :pet_friendly, :duration, :occupancy, :zip, {:roommate_preference => []}, :latitude, :longitude, :start_date, :publish, :published_at, :user_id)
    end

日志:

Started GET "/posts/15/publish" for 127.0.0.1 at 2013-05-19 02:07:29 -0700
Creating scope :page. Overwriting existing method Post.page.
Processing by PostsController#publish as HTML
  Parameters: {"id"=>"15"}
  Post Load (15.2ms)  SELECT "posts".* FROM "posts" WHERE "posts"."id" = $1 LIMIT 1  [["id", "15"]]
  Rendered text template (0.0ms)
Completed 400 Bad Request in 603ms (Views: 62.5ms | ActiveRecord: 382.7ms)
4

1 回答 1

1

我发现它已更改@post.update_attributes(post_params)@post.update_attributes(params[:post])

于 2013-05-20T07:15:01.167 回答