중첩 된 특성을 배열로 나눠서 고유성을 검사 한 다음 저장하는 기능이 있습니다. 문제는, 내가 Can't mass-assign protected attributes: _destroy
오류가 발생했습니다. nested_form gem
은 hidden_field를 삽입하여 삭제해야 할 속성을 강조 표시하기 때문입니다. 로그 여기이 작성 기능을 수정하려면 어떻게해야합니까?
def self.find_or_initialize_location_set(location_set)
#create a locations array
locations = []
locations = locations.delete_if { |elem| elem.flatten.empty? }
location_set.each do |key, location|
locations << find_or_initialize_by_name(location)
end
locations
end
그리고 것 :
Started POST "/blog/posts" for 127.0.0.1 at 2012-12-03 17:31:54 +0000
Processing by Blogit::PostsController#create as HTML Parameters: {"utf8"=>"✓", "authenticity_token"=>"r74iCzC4tJgVI6FiCEH7XzfiTmaqKihF5JSs7Ow3MSI=", "post"=>{"title"=>"This
is a post about Paris", "body"=>"This is a post about ParisThis is a post about Paris
This is a post about ParisThis is a post about Paris", "tag_list"=>"",
"locations_attributes"=>{"0"=>{"_destroy"=>"false", "name"=>"Paris", "longitude"=>"2.3522219", "latitude"=>"48.856614"}, "1354555760002"=>{"_destroy"=>"false", "name"=>"Los Angeles", "longitude"=>"-118.5155901", "latitude"=>"3
4.03563310000001"}}}, "_wysihtml5_mode"=>"1", "name"=>"losds", "legname"=>"Los Angeles", "longitude"=>"-118.5155901", "latitud
e"=>"34.03563310000001", "commit"=>"Submit"}
User Load (0.1ms) SELECT "users".* FROM "users" WHERE "users"."id" = 1 LIMIT 1
ActsAsTaggableOn::Tag Load (0.1ms) SELECT "tags".* FROM "tags" INNER JOIN "taggings" ON "tags"."id" = "taggings"."tag_id" W
HERE "taggings"."taggable_id" IS NULL AND "taggings"."taggable_type" = 'Blogit::Post' AND (taggings.context = 'tags' AND taggings.tagger_id IS NULL)
Location Load (0.1ms) SELECT "locations".* FROM "locations" WHERE "locations"."name" = 'Paris' LIMIT 1
LOGGER LOCATIONS ARE[#<Location id: 768, name: "Paris", latitude: #<BigDecimal:7f86aadf6230,'0.48856614E2',18(45)>, longitude:
#<BigDecimal:7f86aadf5f60,'0.23522219E1',18(45)>, post_id: nil, created_at: "2012-12-01 17:27:33", updated_at: "2012-12-01 17
:27:33", notes: nil>]
Location Load (0.2ms) SELECT "locations".* FROM "locations" WHERE "locations"."name" = 'Los Angeles' LIMIT 1
Completed 500 Internal Server Error in 97ms
ActiveModel::MassAssignmentSecurity::Error (Can't mass-assign protected attributes: _destroy):
app/models/location.rb:23:in `block in find_or_initialize_location_set'
app/models/location.rb:21:in `each'
app/models/location.rb:21:in `find_or_initialize_location_set'
app/controllers/blogit/posts_controller.rb:79:in `create'
여기
def create
location_set = params[:post].delete(:locations_attributes) unless params[:post][:locations_attributes].blank?
@post = current_blogger.blog_posts.new(params[:post])
@post.locations = Location.find_or_initialize_location_set(location_set) unless location_set.nil?
if @post.save
redirect_to @post, notice: 'Blog post was successfully created.'
else
render action: "new"
end
end
이
location.rb에서 함수의 :
은 여기 * Posts_Contoller.rb * 만드는 기능입니다 이 기능을 사용하여 삭제 기능을 포함시킬 수있는 방법에 대한 제안 이전과 같이 params에서 켜기 때문에 Can't mass-assign protected attributes: _destroy
오류가 표시되지 않습니까?
감사합니다.
안녕하세요 숀, 미안 해요, 그냥이 테스트하고 실제로 모든 특성을 삭제 기능을 보냈다 실현. 'if location.attributes = {: _destroy => "1"}'아이디어와 같은 것을 고를 필요가 있습니까? –
삭제시': _destroy'를 사용 했습니까? 내가 실수로': delete'을했는데 ... 제 편집을 봅니다. 해시에서 'delete'를 호출하면 키를 삭제하여 해시를 수정하고 삭제 된 키를 반환해야합니다. 그래서 당신은'deleted_key = location.delete (: _ destroy)'를 가질 수 있고': _destroy'는'location'에서 제거되어'deleted_key'에 할당 될 것입니다. –
안녕하세요, 예, 알아 차리고 변경했습니다. 문제는 모든 속성에 '파괴'속성이 있지만 제거되어야하는 경우 '_destroy'가 1로 설정됩니다. 이것은 현재 읽는 방법입니다 : 'location_set.each do | key, location | if location.delete (: _ 파괴) locations.delete_if {| elem | elem = ' "_destroy"=> "1"'} end' 나는 그것이 모든 파괴의 모든 속성을 집어 들고 있다고 생각합니다. 어떤 아이디어? –