gpt4 book ai didi

ruby-on-rails - 使用嵌套表单和回形针 gem 的 Rails Unpermitted 参数

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

我正在创建一个网站,用户可以在其中发布属性的广告,其中可以有很多照片。

我已经解决这个问题 2 天了,我找到的答案都没有解决这个问题。

我认为这是问题所在的主程序。我已经根据这段代码测试了许多不同的版本,但没有一个是成功的。

控制台中的参数散列返回正确的值,只是对它的操作导致了问题。

{"utf8"=>"✓", "authenticity_token"=>"3OWguFpgwOYaLmQ4szRmPK/i13kLRr4XT1hcU6YEVlgml1iK1OzAg5c9UyETK1MiqdpoHYLcDGgx4aGd/FaZJg==",

"property"=>{ "title"=>"asda", "price"=>"3", "numberOfBeds"=>"4", "toilets"=>"34", "description"=>"salads", "photo"=>{"property_pic"=>#, @original_filename="ASC_0321.jpg", @content_type="image/jpeg", @headers="Content-Disposition: form-data; name=\"property[photo][property_pic]\"; filename=\"ASC_0321.jpg\"\r\nContent-Type: image/jpeg\r\n">}}, "commit"=>"Create Ad"}

属性.rb

    class Property < ActiveRecord::Base
belongs_to :user
has_many :photos , dependent: :destroy
accepts_nested_attributes_for :photos , reject_if: lambda {|t| t['photo'].nil?
}
end

properties_controller.rb

  def new
@property = Property.new
@photo = @property.photos.new
end

def create

@property = current_user.properties.new(params[:property])

@property.photos.new(params[[:photo][:property_pic]])
if @property.save
redirect_to property_path @property
else
render :edit
end

end

def property_params
params.require(:property).permit(:title,:price,:numberOfBeds,:toilets,:description , photo:[:property_pic])
end

properties/new.html.erb

<div class="wrapper_skinny">

<%= form_for @property do |p| %>

<%= p.label :title %>
<%= p.text_field :title %> <br><br>

<%= p.label :price %>
<%= p.number_field :price %> <br><br>

<%= p.label :numberOfBeds %>
<%= p.number_field :numberOfBeds %> <br><br>

<%= p.label :toilets %>
<%= p.number_field :toilets %> <br><br>

<%= p.label :description %>
<%= p.text_area :description %> <br><br>

<%= p.fields_for :photo do |builder| %>
<%= builder.label :property_pic %>
<%= builder.file_field :property_pic %>
<% end %> <br><br>


<%= p.submit "Create Ad" , class:"button button-chosen"%>
<% end %>

</div>

谢谢!

最佳答案

进一步Muhamad的答案,这是我要做的(几处更改):

#app/models/property.rb
class Property < ActiveRecord::Base
belongs_to :user
has_many :photos
accepts_nested_attributes_for :photos, reject_if: :all_blank #-> seems like you're doing this incorrectly
end

#app/controllers/properties_controller.rb
class PropertiesController < ApplicationController
def new
@property = current_user.properties.new
@property.photos.build
end

def create
@property = current_user.properties.new property_params
@property.save
end

private

def property_params
params.require(:property).permit(:title,:price,:number_of_beds,:toilets,:description , photos_attributes: [:property_pic])
end
end

这应该可以解决您的问题(您的参数显示您正在传递嵌套的表单属性,但您需要确保使用 fields_for 帮助器:

#app/views/properties/new.html.erb
<%= form_for @property do |f| %>
<%= f.fields_for :photos do |photo| %>
<%= photo.file_field :property_pic %>
<% end %>
<%= f.submit %>
<% end %>

注意事项

蛇形盒

总是,总是,总是调用filesattributes通过他们 snake_case 名称。通过他们的调用类 CamelCase名称,其他一切都以蛇形盒为准。

-

HTML

您的 HTML 也可以大大改进:

#app/views/properties/new.html.erb
<%= form_for @property do |p| %>

<% options = [[:title, "text"], [:price, "number"], [:number_of_beds, "number"], [:toilets, "number"], [:description, "area"]] %>

<% options.each do |type,input| %>
<% input = "text_area" if input == "area" %>
<%= p.label type %>
<%= p.send(input, type)
<% end %>

<%= p.fields_for :photo do |builder| %>
<%= builder.label :property_pic %>
<%= builder.file_field :property_pic %>
<% end %>

<%= p.submit "Create Ad" , class:"button button-chosen"%>
<% end %>

此外,不要使用 HTML 进行样式设置 - 仅使用 CSS。

人们使用 <p><br>在他们的应用程序中创建空间,当margin-top/margin-bottom是在 CSS 中执行此操作的正确方法。

关于ruby-on-rails - 使用嵌套表单和回形针 gem 的 Rails Unpermitted 参数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33292552/

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