rails 4.更新帖子不起作用

时间:2014-05-25 19:03:09

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

我想更新帖子(方坯),我发现此错误: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已经允许&#39;

这是一个日志

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

的嵌套模型

控制器     #GET / billets / new       def new         @billet = Billet.new         渲染布局:&#34; amabiblio_blog&#34;       端

  # 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

1 个答案:

答案 0 :(得分:4)

您在update方法中有下一行:

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

您应该更改为create中的相同内容:

if @billet.update(billet_params)

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