.save() does not change the value of a field , django [closed] - javascript

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 3 years ago.
Improve this question
I am trying to implement a notification system. When the user clicks onto one of the notification from the drop down box , i will use a AJAX Post request to modify the boolean field to indicate that that particular instance of the Notification has been read before.
here is my code:
This is my HTML template:
<ul class="dropdown-menu dropdown-menu-right myDropDown">
{%for noti in notifications%}
{{noti}}
<li>
<a href="#" class="top-text-block" id="{{noti.id}}" onClick="return booleanchanger(this.id);">
<div class="top-text-heading">{{noti.object_type}}</div>
<p class = 'text-muted'><small>{{noti.time}}</small>></p>
<div class="top-text-light">{{noti.message}}</div>
</a>
</li>
{%endfor%}
</ul>
This is my ajax call :
function booleanchanger(clicked_id){
var a = clicked_id
$.ajax({
url : "{% url 'read-notification' %}",
type : "POST",
data : {
'csrfmiddlewaretoken' : "{{ csrf_token }}",
'pk' : a
},
success : function(result) {
}
});
This is my notifications model:
class Notifications(models.Model):
time = models.DateTimeField(auto_now=True)
target = models.ForeignKey(User , on_delete=models.CASCADE)
message = models.TextField()
object_type = models.CharField(max_length=100)
object_url = models.CharField(max_length=500,default = 'test')
is_read = models.BooleanField(default=False)
This is my view that handles the ajax request:
def ReadNotificationView(request):
if request.method=='POST' and request.is_ajax():
pk = request.POST.get('pk',False)
obj = Notifications.objects.get(pk=pk)
obj.if_read = True
obj.save()
print(obj.if_read)
return JsonResponse({'status':'Success', 'is_read': 'changed'})
else:
return JsonResponse({'status':'Fail', 'is_read':'not changed'})
this is the url.py:
path('notification/update/' , views.ReadNotificationView , name = 'read-notification')
print(obj.if_read) from within my view returns me this:
True
However , going into the django admin page and checking the status of the is_read field shows that the code does not work . Does anyone have a solution for this ? I will greatly appreciate it!

Typo. In model you got is_read in view if_read

Related

otree: ask a question depending on the answer to a previous question (on the same page)

I would like to incorporate a question in Otree that might or might not be asked depending on a previous question. Here is a very simple example:
Question 1: What is your main occupation:
A. Work.
B. Student.
C. Unemployed
Question 2 (ONLY ASKED IF the answer to "Question 1" is "A. Work"): what industry do you work on?
A. Transportation
B. Mining
C. Other
I have managed to do this when Question 1 and Question 2 are on different pages (see code below). However, I would like to have questions 1 and 2 on the same page. Any insights on how I can do this? (I am a beginner using otree/javascript)
from otree.api import *
doc = """
'other' option
"""
class C(BaseConstants):
NAME_IN_URL = 'option_other'
PLAYERS_PER_GROUP = None
NUM_ROUNDS = 1
class Subsession(BaseSubsession):
pass
class Group(BaseGroup):
pass
class Player(BasePlayer):
occupation = models.StringField(label='main occupation?',choices=['Work', 'Student', 'Unemployment'])
industry = models.StringField(label='what industry do you work on?', choices=['transportation','mining','others'])
# PAGES
class MyPage(Page):
form_model = 'player'
form_fields = ['occupation']
class MyPage2(Page):
#staticmethod
def is_displayed(player: Player):
return player.occupation == 'Work'
form_model = 'player'
form_fields = ['industry']
page_sequence = [MyPage, MyPage2]
To show a question depending on the answer to another question on the same page, you need a little javascript (as you might have guessed since your question is tagged accordingly). This javascript code can be integrated directly into the HTML template (Page.html), for example like this:
{{ block styles }}
<style>
.do-not-show {
display: none;
}
</style>
{{ endblock }}
{{ block content }}
{{ formfield "occupation" }}
<div id="industry-box" class="do-not-show">
{{ formfield "industry" }}
</div>
{{ next_button }}
{{ endblock }}
{{ block scripts }}
<script>
let industry_box = document.getElementById("industry-box");
let industry_select = document.getElementById("id_industry");
let occupation_select = document.getElementById("id_occupation");
occupation_select.addEventListener("change", function() {
if (occupation_select.value == "Work") {
industry_box.classList.remove("do-not-show");
industry_select.required = true;
} else {
industry_box.classList.add("do-not-show");
industry_select.required = false;
}
});
</script>
{{ endblock }}
To explain: First, let's hide the second question by wrapping it in a box and creating a CSS class that ensures that this box is not displayed. And then in the javascript block we create an event listener that reacts every time an answer is selected on the first question. If the answer is "Work", we'll display the second question by removing our CSS class. If the answer has a different value, we add our CSS class and hide the question (if it's not already hidden). If you want the second question to be optional (rather than mandatory), you can just remove this two lines: industry_select.required = true/false;.
It is also important that you add blank=True in the field for the second question in the player model. Otherwise, otree will always expect an answer to this question and throw an error message if the question isn't answered (because a player never saw it, for example):
class Player(BasePlayer):
occupation = models.StringField(
label='main occupation?',
choices=['Work', 'Student', 'Unemployment']
)
industry = models.StringField(
label='what industry do you work on?',
choices=['transportation','mining','others'],
blank=True
)
And of course both questions have to be included as form fields in the class of your page:
class MyPage(Page):
form_model = 'player'
form_fields = ['occupation', 'industry']

How can I show my images through JsonResponse in Django's view? [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 months ago.
Improve this question
I have a question, my collaborator's code works well, it shows some images that were previously uploaded through an input through a modal. The problem is that I want to display the images in a detail view. I modified it and I can only show one of the 10 that were uploaded. How can I show all 10? I have no idea how to handle that JSON he used
views.py
class detail_carro(DetailView):
template_name = 'carros/carros-detail.html'
queryset=Carro.objects.all()
context_object_name = 'carros'
def create_carros_picture(request):
if request.FILES['files']:
file = request.FILES['files']
fs = FileSystemStorage() # defaults to MEDIA_ROOT
new_name = "picture"
new_name = fs.get_valid_name(new_name)+".jpg"
filename = fs.save(new_name, file)
return JsonResponse({filename:file.name},safe=False)
else:
form=CarroForm()
return render(request, "carros/carros-form-add.html",{'form':form})
def create_carros_warranty(request):
if request.FILES['files']:
file = request.FILES['files']
fs = FileSystemStorage() # defaults to MEDIA_ROOT
ext = file.name.split('.')[-1]
new_name = "warranty"
new_name = fs.get_valid_name(new_name) + '.' + ext
filename = fs.save(new_name, file)
return JsonResponse({filename: file.name}, safe=False)
else:
form = CarroForm()
return render(request, "carros/carros-form-add.html", {'form': form})
carros-detail.html
{% if carros.new_name %}
<a data-id="{{carros.id}}" class="btn_view_gallery">
<img src="{% get_media_prefix %}{{carros.new_name}}" height="300">
</a>
{% endif %}
The DetailView is used to show only a single model instance, Carro in your case I think.
To show all model instaces, use the generic ListView.
https://docs.djangoproject.com/en/4.0/ref/class-based-views/generic-display/#listview
views.py
class CarroListView(ListView):
template_name = 'carros/carros-list.html'
queryset=Carro.objects.all()
context_object_name = 'carros'
carros/carros-list.html
{% for carro in carros %}
<a data-id="{{carros.id}}" class="btn_view_gallery">
<img src="{% get_media_prefix %}{{carros.new_name}}" height="300">
</a>
{% endfor %}

AJAX .done() returning undefined with $.each() on JSON response [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 2 years ago.
Improve this question
I cant access the JSON results from an $.ajax() request. I'm getting a result from the api once i select a car model, but I can't access the JSON response to get the information I need to propogate a new drop down list.
HTML
<div class="row">
<div class="form-group col">
<label>Make</label>
<select class="custom-select" id="vMake">
<option selected="" disabled="">Choose Make</option>
<?php
require "config.php";
$sql = "SELECT * FROM tbl_cars";
$stmt = $pdo->prepare($sql);
$stmt->execute();
$cars = $stmt->fetchAll(PDO::FETCH_ASSOC);
foreach ($cars as $cars) {
echo "<option id='".$cars['make']."' value='".$cars['make']."'>".$cars['make']."</option>";
}
?>
</select>
</div>
<div class="form-group">
<label>Model</label>
<select class="custom-select col" id="vModel">
</select>
</div>
</div>
JS
$(document).ready(function(){
$('#vMake').on('change', function(){
var make = $(this).val();
var url = "https://vpic.nhtsa.dot.gov/api/vehicles/GetModelsForMake/" + make + "?format=json";
$.ajax({
url: url,
type: "GET",
dataType: "json",
}).done(function(data){
$('#vModel').empty();
$.each(data, function(i, e){
var model = data[i].Model_Name;
$('#vModel').append('<option value="' + model +'">'+ model + '</option>');
});
});
});
})
My console result
{"Count":163,"Message":"Response returned successfully","SearchCriteria":"Make:Ford","Results":[{"Make_ID":460,"Make_Name":"Ford","Model_ID":1778,"Model_Name":"Crown Victoria"},{"Make_ID":460,"Make_Name":"Ford","Model_ID":1779,"Model_Name":"Focus"},{"Make_ID":460,"Make_Name":"Ford","Model_ID":1780,"Model_Name":"Fusion"},{"Make_ID":460,"Make_Name":"Ford","Model_ID":1781,"Model_Name":"Mustang"},{"Make_ID":460,"Make_Name":"Ford","Model_ID":1782,"Model_Name":"Taurus"},{"Make_ID":460,"Make_Name":"Ford","Model_ID":1796,"Model_Name":"E-150"},{"Make_ID":460,"Make_Name":"Ford","Model_ID":1797,"Model_Name":"Edge"},{"Make_ID":460,"Make_Name":"Ford","Model_ID":1798,"Model_Name":"Escape"},{"Make_ID":460,"Make_Name":"Ford","Model_ID":1799,"Model_Name":"Expedition"},{"Make_ID":460,"Make_Name":"Ford","Model_ID":1800,"Model_Name":"Explorer"},{"Make_ID":460,"Make_Name":"Ford","Model_ID":1801,"Model_Name":"F-150"},{"Make_ID":460,"Make_Name":"Ford","Model_ID":1802,"Model_Name":"Flex"},{"Make_ID":460,"Make_Name":"Ford","Model_ID":1803,"Model_Name":"Ranger"},{"Make_ID":460,"Make_Name":"Ford","Model_ID":1804,"Model_Name":"Explorer Sport Trac"},{"Make_ID":460,"Make_Name":"Ford","Model_ID":1805,"Model_Name":"F-250"},{"Make_ID":460,"Make_Name":"Ford","Model_ID":1806,"Model_Name":"F-350"},{"Make_ID":460,"Make_Name":"Ford","Model_ID":1807,"Model_Name":"F-450"},{"Make_ID":460,"Make_Name":"Ford","Model_ID":1808,"Model_Name":"F-550"},{"Make_ID":460,"Make_Name":"Ford","Model_ID":1809,"Model_Name":"F-650"},{"Make_ID":460,"Make_Name":"Ford","Model_ID":1810,"Model_Name":"F-750"},{"Make_ID":460,"Make_Name":"Ford","Model_ID":1811,"Model_Name":"Transit Connect"},{"Make_ID":460,"Make_Name":"Ford","Model_ID":1817,"Model_Name":"E-250"},{"Make_ID":460,"Make_Name":"Ford","Model_ID":1818,"Model_Name":"E-350"},{"Make_ID":460,"Make_Name":"Ford","Model_ID":1819,"Model_Name":"E-450"},{"Make_ID":460,"Make_Name":"Ford","Model_ID":1820,"Model_Name":"Expedition MAX"},{"Make_ID":460,"Make_Name":"Ford","Model_ID":2345,"Model_Name":"Thunderbird"},{"Make_ID":460,"Make_Name":"Ford","Model_ID":2346,"Model_Name":"GT"},{"Make_ID":460,"Make_Name":"Ford","Model_ID":2347,"Model_Name":"Five Hundred"},{"Make_ID":460,"Make_Name":"Ford","Model_ID":2370,"Model_Name":"Excursion"},{"Make_ID":460,"Make_Name":"Ford","Model_ID":2379,"Model_Name":"Freestyle"},{"Make_ID":460,"Make_Name":"Ford","Model_ID":2384,"Model_Name":"Freestar"},{"Make_ID":460,"Make_Name":"Ford","Model_ID":2737,"Model_Name":"Motorhome Chassis"},{"Make_ID":460,"Make_Name":"Ford","Model_ID":3160,"Model_Name":"Taurus X"},{"Make_ID":460,"Make_Name":"Ford","Model_ID":3267,"Model_Name":"Fiesta"},{"Make_ID":460,"Make_Name":"Ford","Model_ID":3410,"Model_Name":"Commercial Chassis"},{"Make_ID":460,"Make_Name":"Ford","Model_ID":3462,"Model_Name":"C-max"},{"Make_ID":460,"Make_Name":"Ford","Model_ID":3608,"Model_Name":"Transit"},{"Make_ID":1237,"Make_Name":"Affordable Aluminum","Model_ID":4563,"Model_Name":"Affordable Aluminum"},{"Make_ID":1298,"Make_Name":"Eagle Ford Tanks & Trailers LLC","Model_ID":4645,"Model_Name":"Eagle Ford Tanks & Trailers LLC "},{"Make_ID":460,"Make_Name":"Ford","Model_ID":5181,"Model_Name":"Escort"},{"Make_ID":460,"Make_Name":"Ford","Model_ID":5182,"Model_Name":"ZX2"},{"Make_ID":460,"Make_Name":"Ford","Model_ID":5264,"Model_Name":"Windstar"},{"Make_ID":460,"Make_Name":"Ford","Model_ID":5342,"Model_Name":"E-550"},{"Make_ID":460,"Make_Name":"Ford","Model_ID":5346,"Model_Name":"B-750"},{"Make_ID":460,"Make_Name":"Ford","Model_ID":5361,"Model_Name":"F-800"},{"Make_ID":460,"Make_Name":"Ford","Model_ID":5868,"Model_Name":"Explorer Sport"},{"Make_ID":2431,"Make_Name":"Milford Welding & Manufacturing","Model_ID":6243,"Model_Name":"Milford Welding & Manufacturing Inc."},{"Make_ID":460,"Make_Name":"Ford","Model_ID":6703,"Model_Name":"Aspire"},{"Make_ID":460,"Make_Name":"Ford","Model_ID":6730,"Model_Name":"Probe"},{"Make_ID":460,"Make_Name":"Ford","Model_ID":6731,"Model_Name":"Contour"},{"Make_ID":460,"Make_Name":"Ford","Model_ID":6825,"Model_Name":"Bronco"},{"Make_ID":460,"Make_Name":"Ford","Model_ID":6857,"Model_Name":"Aerostar"},{"Make_ID":460,"Make_Name":"Ford","Model_ID":7331,"Model_Name":"F-150 Heritage"},{"Make_ID":3609,"Make_Name":"Waterford Tank and Fabrication","Model_ID":7985,"Model_Name":"Waterford Tank and Fabrication, LTD"},{"Make_ID":4265,"Make_Name":"Milford Pipe & Supply","Model_ID":9047,"Model_Name":"Milford Pipe & Supply"},{"Make_ID":460,"Make_Name":"Ford","Model_ID":9213,"Model_Name":"L8501"},{"Make_ID":460,"Make_Name":"Ford","Model_ID":9214,"Model_Name":"LT8501"},{"Make_ID":460,"Make_Name":"Ford","Model_ID":9215,"Model_Name":"L9501"},{"Make_ID":460,"Make_Name":"Ford","Model_ID":9216,"Model_Name":"LT9501"},{"Make_ID":460,"Make_Name":"Ford","Model_ID":9217,"Model_Name":"L8511"},{"Make_ID":460,"Make_Name":"Ford","Model_ID":9218,"Model_Name":"LT8511"},{"Make_ID":460,"Make_Name":"Ford","Model_ID":9219,"Model_Name":"L9511"},{"Make_ID":460,"Make_Name":"Ford","Model_ID":9220,"Model_Name":"LT9511"},{"Make_ID":460,"Make_Name":"Ford","Model_ID":9221,"Model_Name":"L8513"},{"Make_ID":460,"Make_Name":"Ford","Model_ID":9222,"Model_Name":"LT8513"},{"Make_ID":460,"Make_Name":"Ford","Model_ID":9223,"Model_Name":"L9513"},{"Make_ID":460,"Make_Name":"Ford","Model_ID":9224,"Model_Name":"LT9513"},{"Make_ID":460,"Make_Name":"Ford","Model_ID":9225,"Model_Name":"L9522"},{"Make_ID":460,"Make_Name":"Ford","Model_ID":9226,"Model_Name":"LT9522"},{"Make_ID":460,"Make_Name":"Ford","Model_ID":9227,"Model_Name":"A8513"},{"Make_ID":460,"Make_Name":"Ford","Model_ID":9228,"Model_Name":"AT8513"},{"Make_ID":460,"Make_Name":"Ford","Model_ID":9229,"Model_Name":"A9513"},{"Make_ID":460,"Make_Name":"Ford","Model_ID":9230,"Model_Name":"AT9513"},{"Make_ID":460,"Make_Name":"Ford","Model_ID":9231,"Model_Name":"A9522"},{"Make_ID":460,"Make_Name":"Ford","Model_ID":9232,"Model_Name":"AT9522"},{"Make_ID":460,"Make_Name":"Ford","Model_ID":9268,"Model_Name":"B800"},{"Make_ID":460,"Make_Name":"Ford","Model_ID":9270,"Model_Name":"F-Super Duty"},{"Make_ID":460,"Make_Name":"Ford","Model_ID":9272,"Model_Name":"F-700"},{"Make_ID":460,"Make_Name":"Ford","Model_ID":9282,"Model_Name":"P700"},{"Make_ID":460,"Make_Name":"Ford","Model_ID":9283,"Model_Name":"P800"},{"Make_ID":460,"Make_Name":"Ford","Model_ID":9284,"Model_Name":"FT900"},{"Make_ID":460,"Make_Name":"Ford","Model_ID":9285,"Model_Name":"L8000"},{"Make_ID":460,"Make_Name":"Ford","Model_ID":9286,"Model_Name":"L9000"},{"Make_ID":460,"Make_Name":"Ford","Model_ID":9287,"Model_Name":"LL9000"},{"Make_ID":460,"Make_Name":"Ford","Model_ID":9288,"Model_Name":"LLA9000"},{"Make_ID":460,"Make_Name":"Ford","Model_ID":9289,"Model_Name":"LLS9000"},{"Make_ID":460,"Make_Name":"Ford","Model_ID":9290,"Model_Name":"LS8000"},{"Make_ID":460,"Make_Name":"Ford","Model_ID":9291,"Model_Name":"LS9000"},{"Make_ID":460,"Make_Name":"Ford","Model_ID":9292,"Model_Name":"LT8000"},{"Make_ID":460,"Make_Name":"Ford","Model_ID":9293,"Model_Name":"LT9000"},{"Make_ID":460,"Make_Name":"Ford","Model_ID":9294,"Model_Name":"LTS8000"},{"Make_ID":460,"Make_Name":"Ford","Model_ID":9295,"Model_Name":"LTS9000"},{"Make_ID":460,"Make_Name":"Ford","Model_ID":9296,"Model_Name":"LTL9000"},{"Make_ID":460,"Make_Name":"Ford","Model_ID":9297,"Model_Name":"LTLA9000"},{"Make_ID":460,"Make_Name":"Ford","Model_ID":9298,"Model_Name":"LTLS9000"},{"Make_ID":460,"Make_Name":"Ford","Model_ID":9299,"Model_Name":"LA8000"},{"Make_ID":460,"Make_Name":"Ford","Model_ID":9300,"Model_Name":"LA9000"},{"Make_ID":460,"Make_Name":"Ford","Model_ID":9301,"Model_Name":"LTA9000"},{"Make_ID":460,"Make_Name":"Ford","Model_ID":9302,"Model_Name":"LN7000"},{"Make_ID":460,"Make_Name":"Ford","Model_ID":9303,"Model_Name":"LN8000"},{"Make_ID":460,"Make_Name":"Ford","Model_ID":9304,"Model_Name":"LN9000"},{"Make_ID":460,"Make_Name":"Ford","Model_ID":9305,"Model_Name":"LNT8000"},{"Make_ID":460,"Make_Name":"Ford","Model_ID":9306,"Model_Name":"LNT9000"},{"Make_ID":460,"Make_Name":"Ford","Model_ID":9307,"Model_Name":"CF8000"},{"Make_ID":460,"Make_Name":"Ford","Model_ID":9308,"Model_Name":"CFT8000"},{"Make_ID":460,"Make_Name":"Ford","Model_ID":9312,"Model_Name":"CF7000"},{"Make_ID":4533,"Make_Name":"Fords Trailer Sales","Model_ID":9637,"Model_Name":"Fords Trailer Sales"},{"Make_ID":460,"Make_Name":"Ford","Model_ID":10378,"Model_Name":"Festiva"},{"Make_ID":460,"Make_Name":"Ford","Model_ID":11942,"Model_Name":"Low Cab Forward"},{"Make_ID":460,"Make_Name":"Ford","Model_ID":13958,"Model_Name":"Tempo"},{"Make_ID":460,"Make_Name":"Ford","Model_ID":13977,"Model_Name":"B600"},{"Make_ID":460,"Make_Name":"Ford","Model_ID":13978,"Model_Name":"B700"},{"Make_ID":460,"Make_Name":"Ford","Model_ID":13979,"Model_Name":"F-600"},{"Make_ID":460,"Make_Name":"Ford","Model_ID":13980,"Model_Name":"Recreational Vehicle"},{"Make_ID":460,"Make_Name":"Ford","Model_ID":13981,"Model_Name":"F-590"},{"Make_ID":460,"Make_Name":"Ford","Model_ID":13983,"Model_Name":"P600"},{"Make_ID":460,"Make_Name":"Ford","Model_ID":14148,"Model_Name":"CL9000"},{"Make_ID":460,"Make_Name":"Ford","Model_ID":14149,"Model_Name":"CLT9000"},{"Make_ID":460,"Make_Name":"Ford","Model_ID":14150,"Model_Name":"Bronco II"},{"Make_ID":460,"Make_Name":"Ford","Model_ID":14152,"Model_Name":"FT800"},{"Make_ID":460,"Make_Name":"Ford","Model_ID":14153,"Model_Name":"CT8000"},{"Make_ID":460,"Make_Name":"Ford","Model_ID":14154,"Model_Name":"C800"},{"Make_ID":460,"Make_Name":"Ford","Model_ID":14155,"Model_Name":"C8000"},{"Make_ID":460,"Make_Name":"Ford","Model_ID":14196,"Model_Name":"Laser"},{"Make_ID":460,"Make_Name":"Ford","Model_ID":14199,"Model_Name":"LTD"},{"Make_ID":460,"Make_Name":"Ford","Model_ID":14614,"Model_Name":"Fairmont"},{"Make_ID":460,"Make_Name":"Ford","Model_ID":14618,"Model_Name":"Granada"},{"Make_ID":5697,"Make_Name":"ASHFORD MFG ","Model_ID":14916,"Model_Name":"Travel Park "},{"Make_ID":460,"Make_Name":"Ford","Model_ID":14930,"Model_Name":"'34 "},{"Make_ID":460,"Make_Name":"Ford","Model_ID":15160,"Model_Name":"CF6000"},{"Make_ID":460,"Make_Name":"Ford","Model_ID":15194,"Model_Name":"B7000"},{"Make_ID":460,"Make_Name":"Ford","Model_ID":15195,"Model_Name":"C7000"},{"Make_ID":460,"Make_Name":"Ford","Model_ID":15197,"Model_Name":"F7000"},{"Make_ID":460,"Make_Name":"Ford","Model_ID":15198,"Model_Name":"F8000"},{"Make_ID":460,"Make_Name":"Ford","Model_ID":15203,"Model_Name":"FT8000"},{"Make_ID":460,"Make_Name":"Ford","Model_ID":15204,"Model_Name":"Courier"},{"Make_ID":460,"Make_Name":"Ford","Model_ID":15205,"Model_Name":"B6000"},{"Make_ID":460,"Make_Name":"Ford","Model_ID":15206,"Model_Name":"C600"},{"Make_ID":460,"Make_Name":"Ford","Model_ID":15208,"Model_Name":"C700"},{"Make_ID":460,"Make_Name":"Ford","Model_ID":15209,"Model_Name":"F6000"},{"Make_ID":460,"Make_Name":"Ford","Model_ID":15223,"Model_Name":"LN600"},{"Make_ID":460,"Make_Name":"Ford","Model_ID":15225,"Model_Name":"LN700"},{"Make_ID":460,"Make_Name":"Ford","Model_ID":15251,"Model_Name":"L800"},{"Make_ID":460,"Make_Name":"Ford","Model_ID":15252,"Model_Name":"LT800"},{"Make_ID":460,"Make_Name":"Ford","Model_ID":15254,"Model_Name":"LN800"},{"Make_ID":460,"Make_Name":"Ford","Model_ID":15255,"Model_Name":"LNT800"},{"Make_ID":460,"Make_Name":"Ford","Model_ID":15273,"Model_Name":"E-100"},{"Make_ID":460,"Make_Name":"Ford","Model_ID":15280,"Model_Name":"F-100"},{"Make_ID":460,"Make_Name":"Ford","Model_ID":15291,"Model_Name":"CT800"},{"Make_ID":6163,"Make_Name":"Swinford Mfg","Model_ID":16298,"Model_Name":"Swinford Mfg"},{"Make_ID":460,"Make_Name":"Ford","Model_ID":17774,"Model_Name":"Ecosport"},{"Make_ID":6579,"Make_Name":"CRANFORD RADIATOR INC.","Model_ID":17910,"Model_Name":"CRANFORD RADIATOR INC."},{"Make_ID":6674,"Make_Name":"BRADFORD BUILT","Model_ID":18064,"Model_Name":"BRADFORD BUILT"},{"Make_ID":6792,"Make_Name":"Stanford Customs","Model_ID":18235,"Model_Name":"Malibu Sedan"},{"Make_ID":6792,"Make_Name":"Stanford Customs","Model_ID":18236,"Model_Name":"Cordova Sedan"},{"Make_ID":6792,"Make_Name":"Stanford Customs","Model_ID":18237,"Model_Name":"Classic Sedan"},{"Make_ID":8578,"Make_Name":"Bradford #1","Model_ID":22551,"Model_Name":"Bradford #1"},{"Make_ID":8948,"Make_Name":"Stafford's Trailers","Model_ID":23075,"Model_Name":"Stafford's Trailers"},{"Make_ID":9569,"Make_Name":"Medford Steel","Model_ID":23878,"Model_Name":"Medford Steel"},{"Make_ID":460,"Make_Name":"Ford","Model_ID":25742,"Model_Name":"Expedition EL"},{"Make_ID":10240,"Make_Name":"Affordable Trailers","Model_ID":26910,"Model_Name":"Affordable Trailers"},{"Make_ID":460,"Make_Name":"Ford","Model_ID":27411,"Model_Name":"Bronco Sport"},{"Make_ID":460,"Make_Name":"Ford","Model_ID":27440,"Model_Name":"Mustang Mach-E"}]}
I'm trying to get the model name of the result. But as shown on image below I get undefined in my second dropdown. How can I get the information I need?
First of all, remove this:
var data;
You already have a variable called data in that function, so if this line is doing anything it's overwriting that variable and making the whole thing undefined. (And if it's not doing that, it's still entirely superfluous and will serve to only confuse you.)
Aside from that, from your JSON it looks like what you want to loop over isn't data, but data.Results:
$.each(data.Results, function(i, e){
//...
});

Getting div data which has empty spaces in its class field [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 6 years ago.
Improve this question
I have a page that I need to parse which is :
<div class="shadowBox someOtherBox">
.
.
.
</div>
.
.
.
<div class="shadowBox other">
<h2>OTHERS</h2>
<ul>
<li>
TITLE #1
</li>
<li>
TITLE #2
</li>
<li>
TITLE #3
</li>
</ul>
</div>
I would like to get each link inside <div class="shadowBox other"> and its TITLE. I tried to do this in many different ways, but at the end I couldn't managed to do it. Here is the code for one of my tries;
function parse(crn)
{
request("LINK_OF_PAGE", function(error, response, html)
{
if(!error)
{
var $ = cheerio.load(html);
var title, news_url, url_hash;
var json = { title : "", news_url : ""};
var links = [];
var data = $('div').filter('.shadowBox').last();
//var data = $('.shadowBox.other').children('ul').children('li').children('a');
console.log(data);
news_url = data.prev().text();
url_hash = md5(news_url);
}
});
}
Why my logic doesn't work? How would I achieve what I want?
Looks like you are trying to populate the links array with the href and text value of anchor elemnets then
var links = $('.shadowBox.other li a').map(function(){
var $this = $(this);
return { title : $this.attr('href'), news_url : $this.text()}
}).get();

Update attribute with ajax call rails

I appreciate that there may be many answers out there but if there are i cant quite interpret them to suit my needs..I am looking to update a records attribute in my model using ajax.
In my case the attribute selected: true
In my scenario i am clicking on an element and looking to update that elements record in the model
<ul class="components">
<% #component.each do |c| %>
<li id="<%= c.component_name %>" data-id="<%= c.id %>" data-remote="true"><%= c.component_name %></li>
<% end %>
</ul>
jQuery
$('#Literacy, #Numeracy').on('click', function(){
var component_name = $(this).text();
var component_id = $(this).data('id');
data_send = { id: component_id }
$.ajax({
method: 'put',
url: '/components/selected_component',
data: data_send,
success: function(data) {
console.log(data)
}
});
});
Controller
def selected_component
#component = Component.find(params[:id])
#component.update_attributes(selected: true)
end
Maybe im looking at this incorrectly but how can i get the id via the click event (stored as component_id) into my selected_component method
Im missing a few bits here but struggling with what today
At the moment I am getting an error
Missing template components/selected_component
but i dont want to render that page after successful update, I just want to stay on the same page
EDIT
to solve my issue i just had to add this to my format.js
{ render nothing: true }
A missing template error corresponds to not having a view that knows what to do when it gets a JavaScript request.
In your views/components/, just make an selected_components.js.erb file and that should clear the error you're getting.

Categories

Resources