gpt4 book ai didi

ruby-on-rails - 不允许的参数,嵌套形式 - Ruby On Rails

转载 作者:数据小太阳 更新时间:2023-10-29 08:12:30 25 4
gpt4 key购买 nike

我已经按照 Michael Hartl 的教程进行操作,现在我想将照片添加到微博中。但是当我提交微博时,图像没有保存在数据库中,在 dvlpmt 日志中我可以读取 unpermitted parameters :image我查看了其他帖子并按照他们的建议做了,但它对我不起作用。

模型

微博.rb

class Micropost < ActiveRecord::Base
belongs_to :user
has_one :photo
default_scope -> { order('created_at DESC') }
validates :content, presence: true, length: { maximum: 140 }
validates :user_id, presence: true
end

photo.rb

class Photo < ActiveRecord::Base
belongs_to :micropost
has_attached_file :image
end

控制者

photos_controller.rb

class PhotosController < ApplicationController
before_action :signed_in_user, only: [:create, :destroy, :index]

def create
@photo = Photo.new(photo_params)

respond_to do |format|
if @photo.save
format.html { redirect_to @photo, notice: 'Photo was successfully created.' }
format.json { render action: 'static_pages/home', status: :created, location: @photo }
else
format.html { render action: 'static_pages/home' }
format.json { render json: @photo.errors, status: :unprocessable_entity }
end
end
end

private
def photo_params
params.require(:photo).permit(:image)
end
end

microposts_controller.rb

class MicropostsController < ApplicationController
before_action :signed_in_user, only: [:create, :destroy]

def create
@micropost = current_user.microposts.build(micropost_params)
if @micropost.save
flash[:success] = "Micropost created!"
redirect_to root_url
else
@feed_items = []
render 'static_pages/home'
end
end

private

def micropost_params
params.require(:micropost).permit(:content, photo_attributes: [:photo_id, :image])
end
end

观点

_micropost_form.html.erb我不知道如何构造好这种形式,我尝试了很多不同的形式

<%= form_for(@micropost) do |f| %>
<%= render 'shared/error_messages', object: f.object %>
<div class="field">
<%= f.text_area :content, placeholder: "Compose new micropost..." %>
</div>

<%= f.fields_for :image, :html => { :multipart => true } do |builder| %>
<%= f.file_field :image, :f => builder %>
<% end %>

<%= f.submit "Post", class: "btn btn-large btn-primary" %>
<% end %>

当我想提交我的表单时,没问题,但是表格微博的“photo_id”列是空的。而且照片没有保存在数据库中。

服务器日志

Started POST "/microposts" for 127.0.0.1 at 2013-11-27 15:06:06 +0100
Processing by MicropostsController#create as HTML
Parameters: {"utf8"=>"✓", "authenticity_token"=>"<my_private_token>", "micropost"=>{"content"=>"Hello world", "image"=>#<ActionDispatch::Http::UploadedFile:0x00000003401a48 @tempfile=#<Tempfile:/tmp/RackMultipart20131127-4010-1r6qt80>, @original_filename="paint.jpg", @content_type="image/jpeg", @headers="Content-Disposition: form-data; name=\"micropost[image]\"; filename=\"paint.jpg\"\r\nContent-Type: image/jpeg\r\n">}, "commit"=>"Post"}
[1m[36mUser Load (0.2ms)[0m [1mSELECT "users".* FROM "users" WHERE "users"."remember_token" = '342df8f039f7f40698b3691f77d2539dc8b9c101' LIMIT 1[0m
Unpermitted parameters: image
[1m[35m (0.1ms)[0m begin transaction
[1m[36mSQL (0.4ms)[0m [1mINSERT INTO "microposts" ("content", "created_at", "updated_at", "user_id") VALUES (?, ?, ?, ?)[0m [["content", "Hello world"], ["created_at", Wed, 27 Nov 2013 14:06:06 UTC +00:00], ["updated_at", Wed, 27 Nov 2013 14:06:06 UTC +00:00], ["user_id", 101]]
[1m[35m (145.1ms)[0m commit transaction
Redirected to http://localhost:3000/
Completed 302 Found in 153ms (ActiveRecord: 145.7ms)

我现在完全卡住了,如果你能找到可以帮助我在哪里找到解决方案的东西,谢谢你!!!

最佳答案

您需要使用 accepts_nested_attributes_for在你的 Micropost 类中。

class Micropost < ActiveRecord::Base
belongs_to :user
has_one :photo
accepts_nested_attributes_for :photo
default_scope -> { order('created_at DESC') }
validates :content, presence: true, length: { maximum: 140 }
validates :user_id, presence: true
end

您的表单似乎也在将图像与您的微博相关联,而不是与微博的照片相关联。查看 fields_for 的 Ruby 文档.您可以尝试将部分表单更改为这个吗?

<%= f.fields_for :photo, :html => { :multipart => true } do |photo_fields| %>
<%= photo_fields.file_field :image %>
<% end %>

如果这样做,您需要确保在呈现 View 的 Controller 操作中调用 @micropost.build_photo

我认为您不需要在microposts_controller.rb 的强参数中列出photo_id。一旦记录保存到数据库,就应该设置它。

您还可以在 building complex forms 上阅读 Rails 指南.该指南讨论了如何设置模型、 View 和 Controller 来处理嵌套属性。他们对 strong parameters 的描述如果您还没有查看,可能会有所帮助。

关于ruby-on-rails - 不允许的参数,嵌套形式 - Ruby On Rails,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20245004/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com