Papouche Guinslyzinho
Papouche Guinslyzinho

Reputation: 5458

rails 4. update posts doesn't work

I would like to update a post (Billet) and I've this error : 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

So parameters: contenu, type_de_billet are already 'permits'

here is a log

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)

I have to say that I have a nested model too called Photo

The controller # GET /billets/new def new @billet = Billet.new render layout: "amabiblio_blog" end

  # 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

Upvotes: 1

Views: 121

Answers (1)

Alexander Kireyev
Alexander Kireyev

Reputation: 10825

You have next line in update method:

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

that you should change to same as in create:

if @billet.update(billet_params)

Now you are using permitted params contenu, type_de_billet only in create action.

Upvotes: 4

Related Questions