[英]Rails/MySQL only one value for attribute is being saved in column, to DB
這將是一個很難解決的問題,因此我將盡我所能。 以下是我想找出的確切問題。 由於某種原因,我保存時,經驗是刪除列中的所有其他值,並在更新當天將其寫入。 我只是保存了所有其他值,但是為了清楚起見,我將它們保留在表之外。 最奇怪的是,預算和經驗設置完全相同,但不幸的是沒有節省相同的預算。 以下是有關此問題的更多詳細信息。
+------+--------+-----------+------------+-----------------+
| id | bar_id | day | budget | experience |
+------+--------+-----------+------------+-----------------+
| 1816 | 369 | monday | LOW BUDGET | |
| 1817 | 369 | tuesday | low budget | |
| 1818 | 369 | wednesday | low budget | IN CONVERSATION |
| 1819 | 369 | thursday | low budget | |
| 1820 | 369 | friday | low budget | |
| 1821 | 369 | saturday | low budget | |
+------+--------+-----------+------------+-----------------+
+------+--------+-----------+------------+--------------------+
| id | bar_id | day | budget | experience |
+------+--------+-----------+------------+--------------------+
| 1822 | 369 | monday | LOW BUDGET | |
| 1823 | 369 | tuesday | low budget | |
| 1824 | 369 | wednesday | low budget | |
| 1825 | 369 | thursday | low budget | ON THE DANCE FLOOR |
| 1826 | 369 | friday | low budget | |
| 1827 | 369 | saturday | low budget | |
+------+--------+-----------+------------+--------------------+
這些是表單的輸入(查看)
<div class="row clearfix">
<div class="label">Budget</div>
<div class="mid budget">
<div class="mid">
<input class="venProfileInputs caps" name="bar_profile[<%= day %>][budget]" onblur=checkBudVals(this)>
</div>
</div>
</div>
<div class="row clearfix">
<div class="label">Experience</div>
<div class=" mid">
<input class="venProfileInputs caps" name="bar_profile[<%= day %>][experience]" onblur=checkExVals(this) />
</div>
</div>
然后進入控制器
def update
@bar = Bar.find(params[:id])
if @bar.update_attributes(params[:bar])
@bar.update_profiles(params[:bar_profile])
redirect_to "/admin", :notice => "Bar was successfully updated."
else
render :action => :edit
end
end
並在模型中更新
def update_profiles(params)
profiles.each {|p| p.destroy }
params[:active_profiles].split(',').each do |profile_day|
next if profile_day.blank?
add_profile(params[profile_day.to_sym])
# sets up crowd tags
crowd_tags_ids = params[profile_day.to_sym][:tags]
unless crowd_tags_ids.blank?
crowd_tags_ids.split(',').each do |tag_id|
next if tag_id.strip == ""
profiles.last.crowd_tags << CrowdTag.find(tag_id)
end
end
profiles.last.set_crowd_tags
#need to loop through cf_tags_ids = params[profile_day.to_sym][:cf_tags]
cf_tags_ids = params[profile_day.to_sym][:cf_tags]
unless cf_tags_ids.blank?
# loop through each category 0,1,2,3,4
cf_tags_ids.each_pair{ |cat, ids|
#cycle through each id in the current cat and add to crowd fiter tag
ids.split(',').each do |cf_id|
next if cf_id.strip == ""
profiles.last.crowd_filter_tags << CrowdFilterTag.find(cf_id)
end
}
end
profiles.last.set_cf_tags
# below is not working for saving experience
profiles.last.experience = params[profile_day.to_sym][:experience]
logger.info "#{profiles.last.inspect}"
#### Still need to save experience some how, budget seems to be getting saved
# but only one value for experience is being logged in a single row for each bar_id
end
end
記錄器輸出:
Parameters: {"utf8"=>"✓",
"bar"=>{"name"=>"Rue B", "website"=>"www.ruebnyc.com", "neighbourhood"=>"Greenwich Village",
"street_address"=>"188 Avenue B", "city"=>"New York", "country"=>"US", "state"=>"NY", "zipcode"=>"10009"},
"bar_profile"=>{"active_profiles"=>",monday,tuesday,wednesday,thursday,friday,saturday",
"monday"=>{"day"=>"monday", "description"=>"", "plan_b"=>"", "capacity"=>"100",
"rating"=>"5", "budget"=>"LOW BUDGET", "experience"=>"ON THE DANCE FLOOR",
"cf_tags"=>{"0"=>"83,", "1"=>"127,", "2"=>"120,", "3"=>"111,", "4"=>"128,"}, "tags"=>"16,46,45,"},
"tuesday"=>{"day"=>"tuesday", "description"=>"Come after dining hours to mellow out with the Lost Generation of today. Fedora optional.",
"plan_b"=>"", "capacity"=>"", "rating"=>"0", "budget"=>"low budget", "experience"=>"",
"cf_tags"=>{"0"=>"61,", "1"=>"", "2"=>"", "3"=>"", "4"=>""}, "tags"=>""},
"wednesday"=>{"day"=>"wednesday", "description"=>"Come after dining hours to mellow out with the Lost Generation of today. Fedora optional.",
"plan_b"=>"", "capacity"=>"", "rating"=>"0", "budget"=>"low budget", "experience"=>"",
"cf_tags"=>{"0"=>"61,", "1"=>"", "2"=>"", "3"=>"", "4"=>""}, "tags"=>""},
"thursday"=>{"day"=>"thursday", "description"=>"Come after dining hours to mellow out with the Lost Generation of today. Fedora optional.",
"plan_b"=>"", "capacity"=>"", "rating"=>"0", "budget"=>"low budget", "experience"=>"",
"cf_tags"=>{"0"=>"59,", "1"=>"", "2"=>"", "3"=>"", "4"=>""}, "tags"=>""},
"friday"=>{"day"=>"friday", "description"=>"Come after dining hours to mellow out with the Lost Generation of today. Fedora optional.",
"plan_b"=>"", "capacity"=>"", "rating"=>"0", "budget"=>"low budget", "experience"=>"",
"cf_tags"=>{"0"=>"60,", "1"=>"", "2"=>"", "3"=>"", "4"=>""}, "tags"=>""},
"saturday"=>{"day"=>"saturday", "description"=>"Come after dining hours to mellow out with the Lost Generation of today. Fedora optional.",
"plan_b"=>"", "capacity"=>"", "rating"=>"0", "budget"=>"low budget", "experience"=>"",
"cf_tags"=>{"0"=>"59,", "1"=>"", "2"=>"", "3"=>"", "4"=>""}, "tags"=>""},
"sunday"=>{"day"=>"sunday", "description"=>"", "plan_b"=>"", "capacity"=>"", "rating"=>"",
"budget"=>"", "experience"=>"", "cf_tags"=>{"0"=>"", "1"=>"", "2"=>"", "3"=>"", "4"=>""}, "tags"=>""}}, "id"=>"369"}
據我所知,它在做什么。 您的日志文件顯示,對於除“星期一”以外的每個“天”,“體驗”均設置為“”。 在星期一,它設置為“在舞池上”。 哪里的“預算”每天都設置為某項。
我將查看您的HTML / JS,看看您是否在提交表單之前以某種方式清除了該字段。
我無法想象這真的對任何人都有幫助,但是這個龐大項目中某個地方的某些JQuery依賴於html中的特定設置。 “ experience”塊缺少一個額外的div,它使JS可以獲取正確的值...這就是為什么它一直用nil進行更新的原因,因為找不到該值。
<div class="row clearfix">
<div class="label">Budget</div>
<div class="mid budget">
<div class="mid">
<input class="venProfileInputs caps" name="bar_profile[<%= day %>][budget]" onblur=checkBudVals(this)>
</div>
</div>
</div>
<div class="row clearfix">
<div class="label">Experience</div>
------> <div class="mid experience">
<div class=" mid">
<input class="venProfileInputs caps" name="bar_profile[<%= day %>][experience]" onblur=checkExVals(this) />
</div>
</div>
</div>
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.