缺少必需参数:发布新控制器操作

时间:2013-05-19 09:18:14

标签: controller ruby-on-rails-3.2 routes

我有一个名为publish的控制器已添加到现有posts_controller.rb 当我访问http://localhost:3000/posts/15/publish时,它会在浏览器中重新启动Missing required Parameter:post

我不确定我错过了什么。 有人能指出这个拼图的缺失部分吗?提前谢谢你

posts_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

publish.html.erb

  <h1>PUBLISHED POST</h1>

的routes.rb

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

我正在使用strong_parameters&amp;这是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)

1 个答案:

答案 0 :(得分:1)

我认为它已将@post.update_attributes(post_params)更改为@post.update_attributes(params[:post])