Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/ruby-on-rails/56.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/ruby/23.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ruby on rails 尝试使用活动资源更新时,参数丢失或值为空_Ruby On Rails_Ruby_Strong Parameters_Activeresource - Fatal编程技术网

Ruby on rails 尝试使用活动资源更新时,参数丢失或值为空

Ruby on rails 尝试使用活动资源更新时,参数丢失或值为空,ruby-on-rails,ruby,strong-parameters,activeresource,Ruby On Rails,Ruby,Strong Parameters,Activeresource,我正在制作一个简单的rails应用程序,其中包含用于crud操作的简单表单,还有一个RailsAPI,它将具有模型并使用rails应用程序中的数据执行所有crud操作。在应用程序中,我使用ActiveResource。目前我在更新方面遇到了一些问题…其他的一切只有在我更新时才会起作用,我不知道为什么,请提供帮助 api/后控制器 class API::PostsController < ApplicationController before_action :set_post, onl

我正在制作一个简单的rails应用程序,其中包含用于crud操作的简单表单,还有一个RailsAPI,它将具有模型并使用rails应用程序中的数据执行所有crud操作。在应用程序中,我使用ActiveResource。目前我在更新方面遇到了一些问题…其他的一切只有在我更新时才会起作用,我不知道为什么,请提供帮助

api/后控制器

class API::PostsController < ApplicationController
  before_action :set_post, only: [:show, :update, :destroy]

  # GET /posts
  # GET /posts.json
  def index
    @posts = Post.all

    render json: @posts
  end

  # GET /posts/1
  # GET /posts/1.json
  def show
    render json: @post
  end

  # POST /posts
  # POST /posts.json
  def create
    @post = Post.new(:title => params[:title], :content => params[:content])

    if @post.save
      render json: @post, status: :created, location: @post
    else
      render json: @post.errors, status: :unprocessable_entity
    end
  end

  # PATCH/PUT /posts/1
  # PATCH/PUT /posts/1.json
  def update

    if @post.update_attributes(post_params)
        render json: @post
    else
       render json: @post.errors, status: :unprocessable_entity
    end

  end

  # DELETE /posts/1
  # DELETE /posts/1.json
  def destroy
    @post.destroy

    head :no_content
  end

  private

    def set_post
      @post = Post.find(params[:id])
    end

     def post_params
      params.require(:post).permit(:title, :id, :content)
    end

end 
class PostsController < ApplicationController

    before_action :set_post, only: [:show, :update, :edit, :destroy]

    def index
        @posts = Post.all
    end

    def new
        @post = Post.new(:title => "", :content => "")
    end

    def create
        @post = Post.new(post_params)
        if @post.save
            redirect_to post_path(@post)
        else
            render "new"
        end

    end

    def show

        @comments = Comment.find(:all, :params => {:post_id => params[:id]})
    end

    def edit
    end

    def update

        @post.title = params[:title]
        @post.content = params[:content]
        if @post.save
            redirect_to post_path(@post)
        else
            render "edit"
        end
    end

    def destroy
        @post.destroy
        redirect_to :action => 'index'
    end

    private

    def post_params
      params.require("post").permit(:title, :content)
    end

    def set_post
      @post = Post.find(params[:id])
    end
end
api路线

resources :posts do
        resources :comments
    end
namespace :api, :defaults => {:format => :json} do
    resources :posts, except: [:new, :edit] do
        resources :comments, except: [:new, :edit]
    end
  end
应用程序活动资源发布模型

class Post < ActiveResource::Base
    self.site = "http://localhost:3001/api"
end
class Post
当我尝试更新时,我在端口3000启动应用程序,在3001启动api

申请

这是api的一部分


有什么问题吗?

您的两个控制器的格式混淆了

api/postcontroller需要非嵌套参数,而application/postcontroller需要嵌套参数,但您以相反的方式传递它们。因此,api版本失败,因为
require(:post)
失败,而应用程序版本失败,因为您要查找的数据位于'params[:post][:title],而不是params[:title]


尝试交换两个更新方法的主体(经过适当的修改,您不希望应用程序提供Json等)。

在post.update_属性(post_参数)之前,从api/postscontroller中打印参数。我认为您的重定向可能没有将参数传递给此方法。{“id”=>“7”、“title”=>nil、“content”=>nil、“创建于”=>“2015-12-29T20:23:38.083Z”、“更新于”=>“2015-12-29T21:18:07.350Z”、“format”=>“json”、“controller”=>“api/posts”、“action”=>“update”}……这是为参数返回的。检查您的
params.require(:post)。允许吗(:id,:title,:content)
希望id、title和content嵌套在一个名为“post”的对象下,如下所示:{“post”=>{“id”=>7,“title”=>“asdf”,“content”=>“asdsdfsfs”},“format”=>“json”,等等请注意,在您发布的回复中,标题和内容是零。参数的传递方式有问题。是的,我理解……但应用程序是否正在发送“post”=>{“title”=>“basd”,“content”=>“lkasdj”}…如图中所示,该图片中的参数看起来不错…可能与应用程序/postcontroller#更新调用
redirect_to post_path(@post)
有关。这些重定向方法通常采用id,而不是整个对象。
class Post < ActiveResource::Base
    self.site = "http://localhost:3001/api"
end