2014-09-29 92 views
0

我在使用Ruby + Rails Web应用程序发布JSON字段(使用邮递员)时遇到了问题。我很困惑控制器的权限(强参数)和后续的发布格式。将JSON发布到Rails 4

我试图用邮差以原始格式发送(内容类型设置为application/JSON)

{"recipe":{"recipe_id":"174a4839020d0820","Category":"Eaten"}, "Nuts":{...}, "Milk":{...}} 

我的控制器看起来像:

class OnboardingsController < ApplicationController 
    before_action :set_onboarding, only: [:show, :edit, :update, :destroy] 
    before_filter :authenticate_user!, :if => Proc.new { |c| c.request.format == 'application/json' } 

    # GET /onboardings 
    # GET /onboardings.json 
    def index 
    @onboardings = Onboarding.all 
    end 

    # GET /onboardings/1 
    # GET /onboardings/1.json 
    def show 
    end 

    # GET /onboardings/new 
    def new 
    @onboarding = Onboarding.new 
    end 

    # GET /onboardings/1/edit 
    def edit 
    end 

    # POST /onboardings 
    # POST /onboardings.json 
    def create 
    ap params 
    @onboarding = Onboarding.new(onboarding_params) 

    respond_to do |format| 
     if @onboarding.save 
     format.html { render :json => @onboarding.to_json , notice: 'Onboarding was successfully created.' } 
     format.json { render :show, status: :created, location: @onboarding } 
     else 
     format.html { render :new } 
     format.json { render json: @onboarding.errors, status: :unprocessable_entity } 
     end 
    end 
    end 

    # PATCH/PUT /onboardings/1 
    # PATCH/PUT /onboardings/1.json 
    def update 
    respond_to do |format| 
     if @onboarding.update(onboarding_params) 
     format.html { redirect_to @onboarding, notice: 'Onboarding was successfully updated.' } 
     format.json { render :show, status: :ok, location: @onboarding } 
     else 
     format.html { render :edit } 
     format.json { render json: @onboarding.errors, status: :unprocessable_entity } 
     end 
    end 

    end 

    # DELETE /onboardings/1 
    # DELETE /onboardings/1.json 
    def destroy 
    @onboarding.destroy 
    respond_to do |format| 
     format.html { redirect_to onboardings_url, notice: 'Onboarding was successfully destroyed.' } 
     format.json { head :no_content } 
    end 
    end 

    private 
    # Use callbacks to share common setup or constraints between actions. 
    def set_onboarding 
     @onboarding = Onboarding.find(params[:id]) 
    end 

    # Never trust parameters from the scary internet, only allow the white list through. 
    def onboarding_params 
     params.require(:onboarding).permit(:recipe, :recipe_id, :category, `enter code here`:fruits, :nuts, :cereal, :milk) 
    end 
end 

和响应是所有空但有些东西被创建:

{ 
    "id": 32, 
    "recipe": null, 
    "fruits": null, 
    "nuts": null, 
    "cereal": null, 
    "milk": null, 
    "created_at": "2014-09-29T06:11:39.874Z", 
    "updated_at": "2014-09-29T06:11:39.874Z" 
} 

日志看起来像:

Unpermitted parameters: recipe 
    (0.2ms) BEGIN 
    SQL (0.6ms) INSERT INTO "onboardings" ("created_at", "updated_at") VALUES ($1, $2) RETURNING "id" [["created_at", "2014-09-28 23:11:39.874650"], ["updated_at", "2014-09-28 23:11:39.874650"]] 
    (2.1ms) COMMIT 
    Rendered onboardings/show.json.jbuilder (0.4ms) 
Completed 201 Created in 126ms (Views: 5.5ms | ActiveRecord: 9.6ms) 

任何方向感谢!查看所有相关SO帖子后完全丢失。

回答

0

您的POST数据的关键是recipe,但你onboarding_params

params.require(:onboarding)

为了做正确的强烈PARAMS我认为这将需要像

params.require(:recipe).permit(:recipe_id, :Category)