首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >rails 4.更新文章不工作

rails 4.更新文章不工作
EN

Stack Overflow用户
提问于 2014-05-25 19:03:08
回答 1查看 119关注 0票数 1

我想更新一篇文章(小册子),我有一个错误:Unpermitted parameters: contenu, type_de_billet

代码语言:javascript
复制
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已经是“许可证”了

这是一根原木

代码语言:javascript
复制
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)

我不得不说,我也有一个嵌套的模型,叫做照片

控制器# GET /billet/ new new @billet = Billet.new呈现布局:"amabiblio_blog“end

代码语言:javascript
复制
  # 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
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2014-05-25 19:14:41

update方法中有下一行:

代码语言:javascript
复制
if @billet.update(params[:billet].permit(:title,:description))

,您应该更改为与create中相同的

代码语言:javascript
复制
if @billet.update(billet_params)

现在,您只在contenu, type_de_billet操作中使用允许的params create

票数 4
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/23858898

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档