【问题标题】:rails 4. update posts doesn't workrails 4.更新帖子不起作用
【发布时间】:2014-05-25 19:03:08
【问题描述】:

我想更新一个帖子(Billet),但我遇到了这个错误:Unpermitted parameters: contenu, type_de_billet

class BilletsController < ApplicationController
    def billet_params
      params.require(:billet).permit(:title, :contenu, :user_id,  :type_de_billet, :image)
    end
end

所以parameters: contenu, type_de_billet 已经是“许可”了

这是一个日志

Started PATCH "/billets/lorem-ipsum-stackoverflow" for 127.0.0.1 at 2014-05-25 14:57:20 -0400
Processing by BilletsController#update as HTML
  Parameters: {"utf8"=>"✓", "authenticity_token"=>"0ZXyfNcbhd1nWe2DTRSWMre7jd+dD85YMysVNs1Hqhc=", "billet"=>{"title"=>"Lorem Ipsum stackoverflow-2", "contenu"=>"<p>Melon oat cake applicake biscuit unerdwear.com gummies gingerbread. Chocolate bar pudding tiramisu liquorice jujubes pie. Marzipan applicake toffee liquorice pastry cupcake ice cream topping. Chocolate tootsie roll danish caramels sweet roll pastry apple pie. Caramels pastry pie. Chupa chups ice cream jelly cheesecake liquorice. Gingerbread sweet roll lollipop cake apple pie cookie powder bear claw muffin. Chocolate bar cheesecake chocolate cake cake icing fruitcake. Sweet jelly topping. Cotton candy gummies muffin brownie jelly beans. Jujubes halvah biscuit chocolate cake apple pie candy sweet roll applicake. Bonbon applicake sweet pudding wafer marzipan candy lemon drops carrot cake.</p>\r\n", "type_de_billet"=>"a"}, "user_id"=>{"field"=>"1"}, "images"=>[#<ActionDispatch::Http::UploadedFile:0x8641524 @tempfile=#<Tempfile:/tmp/RackMultipart20140525-20869-hzjqaq>, @original_filename="1506647_10152395985951605_3858459246170152977_n.jpg", @content_type="image/jpeg", @headers="Content-Disposition: form-data; name=\"images[]\"; filename=\"1506647_10152395985951605_3858459246170152977_n.jpg\"\r\nContent-Type: image/jpeg\r\n">, #<ActionDispatch::Http::UploadedFile:0x86414e8 @tempfile=#<Tempfile:/tmp/RackMultipart20140525-20869-hgykkh>, @original_filename="Ken13sam1DX_6059.jpg", @content_type="image/jpeg", @headers="Content-Disposition: form-data; name=\"images[]\"; filename=\"Ken13sam1DX_6059.jpg\"\r\nContent-Type: image/jpeg\r\n">], "commit"=>"submit", "id"=>"lorem-ipsum-stackoverflow"}
  Billet Load (0.2ms)  SELECT  `billets`.* FROM `billets`  WHERE `billets`.`slug` = 'lorem-ipsum-stackoverflow'  ORDER BY created_at DESC LIMIT 1
  User Load (0.2ms)  SELECT  `users`.* FROM `users`  WHERE `users`.`id` = 1  ORDER BY `users`.`id` ASC LIMIT 1
I will update some files
Unpermitted parameters: contenu, type_de_billet
   (0.1ms)  BEGIN
  SQL (0.5ms)  UPDATE `billets` SET `title` = 'Lorem Ipsum stackoverflow-2', `updated_at` = '2014-05-25 18:57:20' WHERE `billets`.`id` = 13
  FriendlyId::Slug Load (0.1ms)  SELECT  `friendly_id_slugs`.* FROM `friendly_id_slugs`  WHERE `friendly_id_slugs`.`sluggable_id` = 13 AND `friendly_id_slugs`.`sluggable_type` = 'Billet'  ORDER BY `friendly_id_slugs`.id DESC LIMIT 1
   (121.9ms)  COMMIT
   (0.2ms)  BEGIN
  SQL (0.6ms)  INSERT INTO `photos` (`billet_id`, `created_at`, `image`, `updated_at`) VALUES (13, '2014-05-25 18:57:21', '1506647_10152395985951605_3858459246170152977_n.jpg', '2014-05-25 18:57:21')
   (41.5ms)  COMMIT
   (0.2ms)  BEGIN
  SQL (0.4ms)  INSERT INTO `photos` (`billet_id`, `created_at`, `image`, `updated_at`) VALUES (13, '2014-05-25 18:57:21', 'Ken13sam1DX_6059.jpg', '2014-05-25 18:57:21')
   (37.1ms)  COMMIT
Redirected to http://localhost:3000/billets/lorem-ipsum-stackoverflow
Completed 302 Found in 828ms (ActiveRecord: 205.7ms)

我不得不说我也有一个嵌套模型,叫做 Photo

控制器 # 获取 /billets/new 定义新 @billet = Billet.new 渲染布局:“amabiblio_blog” 结束

  # GET /billets/1/edit
  def edit
    render layout: "amabiblio_blog"
  end

  # POST /billets
  # POST /billets.json
  def create
    @billet = current_user.billets.build(billet_params)
    #authorize @billet

      if @billet.save

        # to handle multiple images upload on create
        if params[:images]
          params[:images].each { |image|
            @billet.photos.create(image: image)
          }
        end
        flash[:notice] = "Your billet has been created."
        redirect_to @billet
      else 
        flash[:alert] = "Something went wrong."
        render :new
      end

  end

  # PATCH/PUT /billets/1
  # PATCH/PUT /billets/1.json
  def update
    puts "I will update some files"
    #authorize @album
    if @billet.update(params[:billet].permit(:title,:description))
      # to handle multiple images upload on update when user add more picture
      if params[:images]
        params[:images].each { |image|
          @billet.photos.create(image: image)
        }
      end
      flash[:notice] = "billet has been updated."
      redirect_to @billet
    else
      render :edit
    end
  end

【问题讨论】:

  • 发布您的完整控制器代码。
  • 也发布您的模型
  • 请注意,"user_id"=&gt;{"field"=&gt;"1"} 不在 bullet 哈希中,因此不应允许。

标签: ruby-on-rails ruby-on-rails-4


【解决方案1】:

update 方法中有下一行:

if @billet.update(params[:billet].permit(:title,:description))

你应该改成和create一样的:

if @billet.update(billet_params)

现在您仅在 create 操作中使用允许的参数 contenu, type_de_billet

【讨论】:

    猜你喜欢
    • 2011-02-07
    • 1970-01-01
    • 2018-12-15
    • 2018-12-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多