wrong content loaded due to browser cache - ruby-on-rails

I used Ruby on Rails for my website. In one web page, it will load a poll information based on the poll id, which set in url like "http://mywebsite/polls/1". The poll information includes the poll owner name, the poll title, the item pictures in the poll and the people's name who voted on the poll.
I found sometimes it loaded wrong information. That is, it loaded the wrong poll owner name, poll title and voted people from the other poll while the item pictures are correct.I checked the back end and found there was nothing wrong in rails controller. All the variables got the right values. But the chrome browser told me the view is wrong.
If I cleared all the cache and reload the page then it would work normally.Anyone knows why does it happen and what should I do? Thanks
The relavant action codes:
def show
#poll=Poll.where("is_deleted = false AND id = #{params[:id]}")[0]
#items=#poll.items.where("is_deleted = false")
#voted_user_ids = #poll.audiences.where('has_voted != 0').map(&:user_id).uniq
#voted_users = User.where('id IN (?)',#voted_user_ids)
#voted_user_names = #voted_users.map(&:user_name)
if current_user.nil?
#poll_vote_url = "/voted_choice"
else
#current_user_name = current_user.user_name
#poll_vote_url = "/audiences/"+#poll.id.to_s
#if_current_user_voted = #voted_users.include?(current_user)
#is_poll_owner = (current_user == #poll.user)
check_item_id_in_cookies
end
end
def check_item_id_in_cookies
if !cookies.signed[:item_id].nil?
item = Item.find(cookies.signed[:item_id].to_i)
#create audience if the voter is not the poll owner
if current_user == item.poll.user
flash.now[:alert] = "You can't vote on your own poll."
cookies.signed[:item_id] = nil
else
create_audience cookies.signed[:item_id]
end
end
end
def create_audience item_id
#item_id = item_id.to_i
#item = Item.find(#item_id)
#poll = #item.poll
#voted_user_ids = #poll.audiences.where('has_voted != 0').map(&:user_id).uniq
if #voted_user_ids.include?(current_user.id)
flash.now[:alert]="You already voted."
else
audience = #item.poll.audiences.find{|audience| audience.user_id == current_user.id} || Audience.create(:poll_id => #poll.id,:user_id => current_user.id)
#update audience
audience.is_following = true
audience.has_voted = #item.id
audience.save
cookies.signed[:item_id]=nil
flash[:alert]="Thank you for your vote"
redirect_to "/polls/#{#poll.id}"
end
end

Please monitor the network while loading and reloading the page. Especially to request to request to http://mywebsite/polls/1. Check the response headers as well. Even if you don't do on application side the web server or a proxy server may be modifying the request.
You can find help on who to use the network panel of chrome here.

Related

Rails - Not hitting while loop when creating classes

I have a create method in Rails where I am trying to create multiple objects in a while loop. For some reason it doesn't seem to be hitting the while loop so no objects are being created. The code is below:
def create
#user = User.find(params[:participant][:user_id])
#activity = Activity.find(params[:activity_id])
weeks = #activity.weeks
i = 1
while i <= weeks do
puts "Test"
participant = Participant.new
participant.user_id = #user.id
participant.activity_id = #activity.id
participant.attended = false
participant.paid = false
participant.week = i
participant.save
i = i+1
end
redirect_to user_activities_path(#user, :id => #activity.id)
end
The form I am using to submit is working fine as I can see from the console, and the redirect_to method at the end is working, so it just seems to be missing the loop. If it helps, the value of weeks is 10. Any help would be greatly appreciated.
If multiple Test have been output, try participant.save!, i think the participant save might fail, like some column not valid, so no objects are being created.
Please check if activity record is get fetched. I think your 3rd statement should be as follow.
#activity = Activity.find(params[:participant][:activity_id])

How to retrieve sixth record from database in rails using Where query

I want to retrieve sixth, seventh and eighth record from database using ruby on rails, but it's possible only till fifth, after that undefined method sixth is coming. Please suggest me if there is any possible way.
Following is my code which I tried:
#reporting_masters_travel_requests4 = ReportingMastersTravelRequest.where(travel_request_id: #travel_request.id,status: nil).fifth
if #reporting_masters_travel_requests2 = ReportingMastersTravelRequest.where(travel_request_id: #travel_request.id,status: nil)[1]
ReportingMastersTravelRequest.where(reporting_master_id: #reporting_masters_travel_requests2.reporting_master_id).update_all(status: "true",daily_bill_comment: #comment)
TravelRequest.where(id: #travel_request.id).update_all(reporting_master_id: #reporting_masters_travel_requests3.reporting_master_id)
flash[:notice] = 'Daily Bill Request Send To Higher Authority For Approval'
elsif #reporting_masters_travel_requests3 = ReportingMastersTravelRequest.where(travel_request_id: #travel_request.id,status: nil)[2]
ReportingMastersTravelRequest.where(reporting_master_id: #reporting_masters_travel_requests3.reporting_master_id).update_all(status: "true",daily_bill_comment: #comment)
TravelRequest.where(id: #travel_request.id).update_all(reporting_master_id: #reporting_masters_travel_requests4.reporting_master_id)
flash[:notice] = 'Daily Bill Request Send To Higher Authority For Approval'
elsif #reporting_masters_travel_requests4 = ReportingMastersTravelRequest.where(travel_request_id: #travel_request.id,status: nil)[3]
ReportingMastersTravelRequest.where(reporting_master_id: #reporting_masters_travel_requests4.reporting_master_id).update_all(status: "true",daily_bill_comment: #comment)
TravelRequest.where(id: #travel_request.id).update_all(reporting_master_id: #reporting_masters_travel_requests5.reporting_master_id)
flash[:notice] = 'Daily Bill Request Send To Higher Authority For Approval'
else
flash[:alert] = 'No Reporting Manager is present'
end
I have used the above code and it works perfectly,but its not so dynamic as i manually need to specify for how many time if want to check in table via array [4].But problem is what if 10 records are present in database then for this the above logic will fail.
You are using where to get records from database and it returns array of objects so you can use something like this:
#reporting_masters_travel_requests4 = ReportingMastersTravelRequest.where(travel_request_id: #travel_request.id,status: nil)[5]
You can do as follows:
#reporting_masters_travel_requests = ReportingMastersTravelRequest.where(travel_request_id: #travel_request.id,status: nil)
#reporting_masters_travel_requests.each do |record|
#Something like record.update record.save
end
#set a msg like no more records after loop
msg="No more records"

How to save data using Redmine issue model

I need to modify the issue's start_date and due_date some how,
But I haven't used Rails before, so I don't know how the it run in server,
And I need to implement it in a short time.
So I add these code in the controller,
def date
issue = Issue.find(params[:id])
issue.start_date = params[:start_date]
issue.due_date = params[:due_date]
ntc_str = "Fail to save!" + params[:id]
if issue.save
ntc_str = 'Issue saved!'
end
flash[:notice] = ntc_str;
redirect_to :controller => 'gantts', :action => 'show', :project_id => params[:p_id]
end
It runs when I access it by myself
It always failed and the "ntc_str" always is "Fail to save!" if I use javascript to access it.
For example:
It runs when I input the url "http://xxx.xxx.xxx.xxx/date?id=6&project_id=test&start_date=2016-06-08&due_date=2016-06-30" by hands,
But it failed when I use javascript "window.location.href='/date?id=6&project_id=test&start_date=2016-06-08&due_date=2016-06-30'"
It runs when I input the params in the form I create and click to submit it,
But it failed when I use javascript "document.getElementById('start_date').value = '2016-06-30'; /..../ $('#test-form').submit()"
Could you tell me why it always fails and how can I use the issue model? I have be crazy now.
It would be useful, if you provide some logs with each cases you try.
Also, you can see what goes wrong with issue, when you try to save it, with:
if issue.save
ntc_str = 'Issue saved!'
else
Rails.logger.error(issue.errors.full_messages)
end

Express Checkout Order Total is Missing

I keep getting error #10400 (Order total is missing) but am not sure what I am leaving out. Everything appears to be processing correctly. This is where the payment is setup:
def setcheckout
api = PayPal::SDK::Merchant::API.new
#set_express_checkout = api.build_set_express_checkout(params[:SetExpressCheckoutRequestType])
# Find Item Total and Order Total
details = #set_express_checkout.SetExpressCheckoutRequestDetails
pay = details.PaymentDetails[0]
pay.PaymentDetailsItem[0].Name = 'Item'
pay.PaymentDetailsItem[0].Amount = 1
pay.PaymentDetailsItem[0].Quantity = 1
pay.ItemTotal = pay.PaymentDetailsItem[0].Amount
pay.OrderTotal.currencyID = pay.ItemTotal.currencyID
pay.OrderTotal.value = pay.ItemTotal.value.to_f
# Notify url
#pay.NotifyURL ||= ipn_notify_url
# Return and cancel url
details.ReturnURL ||= 'http://localhost:3000/confirm'
details.CancelURL ||= 'http://localhost:3000/failed'
#set_express_checkout_response = api.set_express_checkout(#set_express_checkout)
if #set_express_checkout_response.success?
redirect_to "https://www.sandbox.paypal.com/cgi-bin/webscr?cmd=_express-checkout&token=#{#set_express_checkout_response.Token}"
end
end
This takes me to paypal, authenticates the user, and returns to the confirmation url as expected. That looks like this:
def confirm
session[:token] = params[:token] if params[:token]
session[:PayerID] = params[:PayerID] if params[:PayerID]
api = PayPal::SDK::Merchant::API.new
#do_express_checkout_payment = api.build_do_express_checkout_payment(params[:DoExpressCheckoutPaymentRequestType])
details = #do_express_checkout_payment.DoExpressCheckoutPaymentRequestDetails
details.Token = session[:token]
details.PayerID = session[:PayerID]
#details.PaymentDetails[0].NotifyURL ||= ipn_notify_url
#do_express_checkout_payment_response = api.do_express_checkout_payment(#do_express_checkout_payment) if request.post?
end
Once the "Confirm and Pay" button is clicked and the above is posted to, the transaction fails with a 10400 Order total is missing. error. It looks to me like I specified the order total above, and the total is displayed when I am taken to paypal. What am I missing?
I don't see the total getting sent in your DoExpressCheckoutPayment request..?? You need to include those same details in DECP that you do in SEC.
As of version 112.0 they introduced the USESESSIONPAYMENTDETAILS parameter, which is supposed to allow to tell DECP to just use what you sent in SEC if you set it to true or 1. There seems to be some discrepancy about whether or not it works, though. I've yet to test it myself.

reassigning variable even though it should already be defined

I am cross referencing my users' Facebook friends with user's signed up with my site. Don't ask me why I did it this way, but I have a piece of code that I want to basically run once and then "cache" so that it doesn't have to cross reference every time the page loads.
if defined? #friends
p "did NOT have to reload"
#friends
else
p "had to reload"
#friends = FbGraph::User.me(current_user.oauth_token).friends
#friends.map! do |friend|
friend if User.find_by_uid(friend.identifier) != nil
end
#friends.compact!
end
Say this loads every time the friends_list action is loaded. Why does it continually have to reload #friends?
#friends is just an instance variable that gets created then destroyed with every request.
Your best bet is to cache the response like so:
#friends = Rails.cache.fetch("friends-#{current_user.cache_key}") do
friends = FbGraph::User.me(current_user.oauth_token).friends
friends.map! do |friend|
friend if User.find_by_uid(friend.identifier) != nil
end
friends.compact!
end

Resources