Tag: javascript

单页应用程序和CSRF令牌

我需要使用单页应用程序(React,Ember,Angular,我不关心)和Rails CSRF保护机制。 我想知道是否需要在ApplicationController创建令牌evey时间,如下所示: class ApplicationController < ActionController::Base after_action :set_csrf_cookie def set_csrf_cookie cookies["X-CSRF-Token"] = form_authenticity_token end end 或者我可以只创建一次令牌 。 每会话或每(非GET)请求? 我认为令牌在会话有效之前仍然有效,对吧? 澄清 : 每当我浏览页面时,我都会看到Rails默认应用程序(服务器呈现页面)更新csrf-token。 所以每次都改变。 因此,在我的情况下,如果我为每个after_action创建一个新令牌,那么之前的CSRF令牌仍然适合该会话。 那么,如何使前一个令牌无效? 我必须? 因为只有我无效才有意义,对吧?

如何从Sinatra中的特定页面重定向网页?

在Sinatra,如何从特定页面重定向网页? require ‘sinatra’ get “/A” do redirect ‘/B’ end get “/B” do # if comes from A # “IT COMES FROM A” # else not from A # “NOT FROM A , REDIRECT TO C” # redirect ‘/C’ # end end 我想学习如何做到这一点? 我可以使用JavaScript或HTML来做我想做的事吗? 或者,必须在Sinatra完成? 我试过这个,但它保持为null, get “/B” do mypath = URI(request.referer || ”).path if mypath […]

如何使用rails对象的id在选择下拉列表中设置选项的值

我在Rails中有一个模态,用于构建公司对象,将其返回到我的选择下拉列表(具有正确的名称),预先设置并自动选择它。 但是,数据库中的现有对象在列表中显示为rails生成的选项,并且同时包含选项的名称和值 – 该值是它们的object.id,因此我可以将两个模型关联在一起。 我不能为我的生活找出如何编写javascript来更新下拉列表中的值以及它的文本。 我目前的jquery: //close the modal $(‘#competitor_modal’).modal(‘hide’); //prepend the name of the company to the select dropdown $(‘#sales_opportunity_company_id’).prepend($(”, { text: , })); //select the newly prepended option $(‘#sales_opportunity_company_id>option:eq(0)’).attr(‘selected’, true); //show the submit button (I hide it whilst they are creating a new company to stop them accidentally submitting with the value “add […]

Rails / Stripe – 多笔付款

我正在使用Rails和Stripe构建一个事件应用来处理付款。 我已经在我的预订页面使用了javascript,以便允许用户预订并支付多个空格而不是一次只支付一个空格。 您可以在此处看到预订页面 – 但是,在我的Stripe仪表板上,只有一个空间付款 – 如何解决此问题,以便在预订视图中显示全额付款? 这是我的控制器和视图代码 – bookings_controller.rb class BookingsController @event.number_of_spaces flash[:warning] = “Sorry, this event is fully booked.” raise ActiveRecord::Rollback, “event is fully booked” end end if @booking.save # CHARGE THE USER WHO’S BOOKED # #{} == puts a variable into a string Stripe::Charge.create(amount: @event.price_pennies, currency: “gbp”, card: @booking.stripe_token, description: “Booking number […]

CORS – 不允许JSONP的跨域AJAX,允许在服务器上使用Origin

我在同一台服务器上有两个独立的应用程序,EmberJS试图对我的后端API进行跨域调用。 我设置了后端API以允许来自该特定来源的跨域请求。 但有没有办法避免在这样的设置中使用JSONP? $.ajax在发送之前阻止跨域请求。 如果没有,CORS有什么意义,我实现了哪些服务器端接受来自我的JS前端源的请求? 编辑 AJAX请求: $.ajax({ url: “api.lvh.me:3000/accounts/login”, data: cred, type: “POST”, xhrFields: { withCredentials: true }, success: function(response){ alert(‘succeeded!’); console.log(response); alert(response); }, failure: function(message){ alert(“failed”); console.log(message); alert(message); } });

如何使用重复图像预填充_form?

用户可以单击featured_inspiration以使用该features_inspiration中的数据预填充新的_form。 数据为:text和/或:image 。 这样,用户就可以将预先存在的Inspiration保存到他自己的Inspirations列表中。 :text正确预填充表单,但由于某种原因:image保持nil 。 调节器 def new existing_inspiration = Inspiration.find_by_id params[:inspiration_id] if existing_inspiration @inspiration = existing_inspiration.dup ### Attempt at Making It Works. Had No Effect. ### @inspiration.image_file_name = existing_inspiration.dup.image_file_name @inspiration.image_content_type = existing_inspiration.dup.image_content_type #Attempt at Making It Works. Had No Effect. @inspiration.image_file_size = existing_inspiration.dup.image_file_size @inspiration.image_updated_at = existing_inspiration.dup.image_updated_at ############## else @inspiration = current_user.inspirations.build end end […]

如何在Rails视图中将Ruby变量传递给JavaScript函数?

我想知道在rails视图中将变量传递给JavaScript函数的最佳实践是什么。 现在我正在做类似的事情: Event.observe(window, ‘load’, function(){ js_function(, ); )} 这是正确的方法吗?

确保用户使用cookieStore和AngularJS登录或注销的最佳实践

现在我正在Ruby on Rails上构建一个基于AngularJS的应用程序,并使用Devise进行身份validation。 当用户成功进行身份validation以及身份validation失败时,我让服务器正确响应。 我想我的问题是,使用$ cookieStore,了解用户是否登录的最佳做法是什么? 有一个由Rails设置的cookie称为“myapp_session”,但该会话并不一定意味着用户已登录。寻找有关如何使用AngularJS来保持用户在线/离线管理的想法。 无论解决方案如何,我仍然会确保需要授权的请求得到后端的授权。

如何在ember中链接到特定的rails API路由和服务器生成的内容?

我有一个混合的Ember / Rails应用程序,在API命名空间中有一个Rails路由来获取任何单个事件并将其转换为.ics文件以导入到用户的日历中( 这是一个问题 )。 Ember正在使用命令ember server –proxy http://localhost:3000 ,它将它连接到Rails服务器进程。 以下片段说明了我的设置: Rails routes.rb片段: namespace :api do # snip resources :events do # snip get ‘export_event_ical’, on: :member end end Rails events_controller.rb片段: def export_event_ical @event = Event.find(params[:id]) @calendar = Icalendar::Calendar.new ical_event = Icalendar::Event.new ical_event.dtstart = @event.start.strftime(“%Y%m%dT%H%M%S”) ical_event.dtend = @event.start.strftime(“%Y%m%dT%H%M%S”) ical_event.summary = @event.body.truncate(50) ical_event.description = @event.body # […]

根据select(Rails)分别获取并显示数据库行中的所有值

嗨,我被困在制作发票申请表。 我想有一个选项,您可以从customers表中添加现有客户,并将这些值添加到发票中。 我想要显示的值是company_name , vat_number和iban_number 。 我试过这样做: 但显然这只能得到company_name的值。 我尝试使用collection.select但是它也只获得数据库行的一个值而不是全部3个值。 我希望能够从仅包含company_name的列表或表行中进行选择,但是当我单击该company_name时,它还必须显示vat_number和iban_number (单独地,在页面的不同部分)。 我的发票/ _form中的这样的东西是最佳的: 这是我的发票控制器: class InvoicesController < ApplicationController before_action :set_invoice, only: [:show, :edit, :update, :destroy] before_action :authenticate_user! # GET /invoices # GET /invoices.json def index @invoices = current_user.invoices.all flash.now[:notice] = "U haven't added any invoices yet" if @invoices.empty? end # GET /invoices/1 # GET /invoices/1.json […]