小编Huy*_*Huy的帖子

Rails 重定向到用户想在登录前查看的页面

我正在尝试将用户重定向回他们在登录之前尝试访问的页面(该页面仅对用户可见)。

我将从针对我的旅行新操作的 before 过滤器开始:

before_filter :authenticate, :only => [:new, :create, :edit, :update, :destroy]

def authenticate
  deny_access unless signed_in?
end 

def deny_access
  store_location
  redirect_to login_path, :notice => "Please log in to access this page."
end

def store_location
  session[:return_to] = request.fullpath
end
Run Code Online (Sandbox Code Playgroud)

这是我的 session_controller.rb 中的 new 和 create 操作

def new
  @title = "Log in"
end

def create
  user = User.authenticate(params[:session][:email].downcase, 
                           params[:session][:password])     
  if user.nil?
    flash.now[:error] = "Invalid email/password combination."
    @title = "Log in"
    render 'new'
  else
    sign_in user
    redirect_back_or(root_path)
  end
end

def …
Run Code Online (Sandbox Code Playgroud)

authentication session ruby-on-rails ruby-on-rails-3 ruby-on-rails-3.1

5
推荐指数
1
解决办法
3435
查看次数

使用getValue for JFormattedText在另一个JFormattedText字段中显示

我有一个程序,我根据所选的单选按钮计算2个变量中的任何一个.我正在尝试使用getValueJFormattedText字段中获取值并将其显示在另一个JFormattedText字段上(最终我将使用该数字进行一些计算).

import javax.swing.*;
import java.awt.event.*;
import java.awt.*;
import java.text.DecimalFormat;

public class FutureValueFrame extends JFrame
{
    public FutureValueFrame()
    {
        setTitle("Sample App");
        setSize(400,400);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    }


    public static void main(String[] args)
    {
        JFrame f = new FutureValueFrame();

        //GUI and BUTTONS
        JRadioButton monthlyRadioButton = new JRadioButton("Monthly Payment");
        JRadioButton loanAmountButton = new JRadioButton("Loan Amount");
        ButtonGroup selection = new ButtonGroup();
        selection.add(monthlyRadioButton);
        selection.add(loanAmountButton);

        JFormattedTextField loanAmountField = new JFormattedTextField(new DecimalFormat("####.##"));
        JFormattedTextField interestRateField = new JFormattedTextField(new DecimalFormat("####.##"));
        JFormattedTextField yearField = new JFormattedTextField(new DecimalFormat("####.##")); …
Run Code Online (Sandbox Code Playgroud)

java swing jformattedtextfield

5
推荐指数
1
解决办法
6071
查看次数

Mongoid - save和update_attribute不会保留

我正在尝试从Mongo集合中的记录更新属性,但是没有保存新值.

a = GraphEngine::UserPlace.where(place_id:5000000701039).first
a.place_id = 5000000257690
a.save!
=> true
Run Code Online (Sandbox Code Playgroud)

如果我检查a,则place_id反映新值,5000000257690但是当我再次加载记录时,新的place_id不会持久存在.

知道为什么会这样吗?我已经检查过以确保没有重复的记录.

我也试过a.update_attribute(:place_id,5000000257690)但也没有运气.它返回=> true,但值不会持续.

ruby ruby-on-rails mongodb ruby-on-rails-3 mongoid3

5
推荐指数
1
解决办法
2395
查看次数

在 Rails 控制器中执行原始 MongoDB 查询

我有以下原始 mongo 查询,我想直接在 Rails 控制器中执行:

 db.user_places.update({place_id: destroy_model.id}, {$set: {place_id: keep_model.id}}, false, true)
Run Code Online (Sandbox Code Playgroud)

对于续集,我用来ActiveRecord::Base.connection.execute(sql)执行我的 sql 查询。

使用 MongoDB/mongoid gem 执行此操作的等效方法是什么?我尝试按照如何直接在 Ruby 中触发原始 MongoDB 查询,但我没有使用 MongoMapper。

谢谢。

编辑:我对这个问题的解决方案是从 Mongoid 文档中运行以下命令:

UserPlace.where(place_id: destroy_model.id).update_all(place_id: keep_model.id)

这将生成上面的原始 mongo 查询并以 ActiveRecord 方式执行它。

ruby ruby-on-rails mongodb ruby-on-rails-3 mongoid3

5
推荐指数
1
解决办法
1128
查看次数

在Rails中设置SQL查询的超时值

我想在Rails中执行一个非常长的查询ActiveRecord::Base.connection.execute(sql).

但是,查询会保持超时.是否可以更改此特定查询的超时值,而不必更改所有查询的超时值database.yml

谢谢

ruby-on-rails ruby-on-rails-3

4
推荐指数
2
解决办法
4200
查看次数

Mysql2 ::错误:密钥的重复条目 - ActiveRecord :: RecordNotUnique没有捕获错误?

我正在使用rails应用程序,并且经常遇到以下错误:

Mysql2::Error: Duplicate entry '3022093-2000000028003-visited' for key 'unique_user_place_relationship'
Run Code Online (Sandbox Code Playgroud)

虽然我已将问题的根源缩小到以下几行:

begin
  up = UserPlace.new(user_place_params)
  up.skip_logging
  up.save!
rescue ActiveRecord::RecordNotUnique => e
  Rails.logger.warn(e)
end
Run Code Online (Sandbox Code Playgroud)

在我的表中,我有以下索引:

key_name                         seq_in_index    column_name
unique_user_place_relationship   1               user_id
unique_user_place_relationship   2               place_id
unique_user_place_relationship   3               relationship
Run Code Online (Sandbox Code Playgroud)

问题是我没有validate_uniqueness_ofuser_id,place_id和关系user_place.rb吗?

根据我的理解,ActiveRecord:RecordNotUnique应该捕获此错误,因为事务不满足db级别的索引约束.

exception-handling ruby-on-rails ruby-on-rails-3

4
推荐指数
1
解决办法
1万
查看次数

Jasmine规范生成不同的随机数而不是执行

我有一个失败的Jasmine测试,因为正在生成一个随机数,这个随机值对于执行和规范是不同的.

fetch: function(options) {
    if(typeof options === "undefined") {
        options = {};
    }
    if(typeof options.data === "undefined") {
        options.data = {};
    }
    options.data.id = this.journalId;
    options.data.random = Math.floor(Math.random()*10000);
    col.prototype.fetch.call(this, options);
}
Run Code Online (Sandbox Code Playgroud)

下面的测试失败,因为Math.floor(Math.random()*10000)生成了不同的值.

it("should call parent fetch with default options", function() {
  this.collection.fetch();
  expect(this.fetchSpy).toHaveBeenCalledWith({
    data: {
      id: 1,
      random: Math.floor(Math.random()*10000) 
    }
  }); 
});
Run Code Online (Sandbox Code Playgroud)

有没有办法让我的测试通过我生成随机数的情况?

javascript testing tdd ruby-on-rails jasmine

4
推荐指数
1
解决办法
3045
查看次数

仅显示一个DialogFragment

我正在尝试设置一个对话框片段,当我的android用户收到推送通知时弹出该对话框片段.我下面的代码触发了对话框.我遇到的问题是,如果我的用户多次推送,他们会看到弹出多个对话框.

我想要的动作是只显示一个对话框,如果在当前关闭之前弹出另一个对话框,则应当销毁当前的一个,然后显示新的一个.

public abstract class BaseActivity extends ActionBarActivity {
  public void showShiftsDialog(String time) {
      String DIALOG_ALERT = "dialog_alert";

      FragmentTransaction transaction = getFragmentManager().beginTransaction();
      android.app.Fragment prev = getFragmentManager().findFragmentByTag(DIALOG_ALERT);

      if (prev != null) transaction.remove(prev);
      transaction.addToBackStack(null);

      // create and show the dialog
      DialogFragment newFragment = ShiftsDialogFragment.newInstance(time);
      newFragment.show(getSupportFragmentManager().beginTransaction(), DIALOG_ALERT);
  }
}
Run Code Online (Sandbox Code Playgroud)

我尝试使用Android文档中的代码(http://developer.android.com/reference/android/app/DialogFragment.html).调试时,它看起来prev总是为null.

根据我的理解,看起来我将DialogFragment附加到SupportFragmentManager:

newFragment.show(getSupportFragmentManager().beginTransaction(), DIALOG_ALERT);
Run Code Online (Sandbox Code Playgroud)

当我尝试检查是否有任何当前的DialogFragment时,我正在检查FragmentManager:

 android.app.Fragment prev = getFragmentManager().findFragmentByTag(DIALOG_ALERT);
Run Code Online (Sandbox Code Playgroud)

如果我尝试更改代码以尝试从SupportFragmentManager获取它,我会得到一个不兼容的类型错误,它会在期待android.app.Fragment,但我返回一个android.support.v4.app.Fragment:

android.app.Fragment prev = getSupportFragmentManager().findFragmentByTag(DIALOG_ALERT);
Run Code Online (Sandbox Code Playgroud)

如何管理我的DialogFragment,以便在任何给定时间只显示一个?

工作方案

public void showShiftsDialog(String time) {
    String DIALOG_ALERT = "dialog_alert";

    android.support.v4.app.FragmentTransaction …
Run Code Online (Sandbox Code Playgroud)

android android-fragments android-dialogfragment

4
推荐指数
1
解决办法
2347
查看次数

ReferenceError:使用onclick时未定义函数

我使用javascript设置了一个锚标记,以便onclick我调用一个函数showSavingsEasterEgg并传递一个值.我得到的问题是我没有正确引用我的功能:

ReferenceError: showSavingsEasterEgg is not defined

我已经尝试将showSavingsEasterEgg函数移动到showData函数中以查看它是否解决了范围问题,但事实并非如此.

有小费吗

define(["jquery", "go/util", "underscore", "jqueryui/slider"], function ($, util,_) {

  function showData(data) {
    $('#item').append('<a class="savings_link" href="#" onclick=\'showSavingsEasterEgg('+data[key]['saving']+')\'> Savings');
  }

  function showSavingsEasterEgg(data){
    console.log("SUccess");
    console.log(data);
  }

});
Run Code Online (Sandbox Code Playgroud)

javascript jquery

3
推荐指数
1
解决办法
1万
查看次数

使用 CSS 禁用 SVG mouseclick 事件

我想禁用使用 css 单击 SVG 元素:

.disabled{
  pointer-events: none;
}
Run Code Online (Sandbox Code Playgroud)

当我使用 获取 SVG 元素时$(".dasvg"),控制台显示我已选择该元素:

[
  <svg width=?"500" height=?"500" class=?"dasvg">?
  <defs>?…?</defs>?
  <defs>?…?</defs>?
  <path class=?"link dragline hidden" d=?"M0,0L0,0">?</path>?
  <g>?…?</g>?
  <g>?…?</g>?
  </svg>?
]
Run Code Online (Sandbox Code Playgroud)

但是,当我尝试执行类似$(".dasvg").addClass("disabled")或 $(".dasvg")[0].addClass("disabled") , thedisabled` 类的操作时,不会将其附加到元素中。有没有办法使用 CSS 禁用 SVG 元素?

css jquery svg

3
推荐指数
1
解决办法
8389
查看次数