小编yer*_*syl的帖子

如何在Rails中的控制器方法中传递变量进行渲染

我试图传递一个变量来渲染像这样:

def monitor
  @peripheries = Periphery.where('periphery_type_name=?','monitor')
  render 'index', type: 'Monitor'
end
Run Code Online (Sandbox Code Playgroud)

这里我想在索引视图中使用'type'变量,它看起来像这样:

<%= render 'some_partial', periphery_type: type %>
Run Code Online (Sandbox Code Playgroud)

这也是一些东西.但我想使用那个'type'变量

ruby-on-rails ruby-on-rails-4

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

来自ISO8601的Python3 strptime带有时区

如何将ISO8601时间格式的字符串转换为python3 datetime.这是我的时间:

2017-03-03T11:30:00+04:00
Run Code Online (Sandbox Code Playgroud)

我尝试的方式:

datetime.strptime(appointment['datetime'], '%Y-%m-%dT%H:%M:%S%z')
Run Code Online (Sandbox Code Playgroud)

这里的问题是,我不知道如何在.strptime方法的格式参数中表示+4:00时区.

python date python-3.x

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

在 Rails 4 中生成复选框

我想根据选择值生成一些复选框。所以我有选择标签:

<%= f.collection_select :type, RequestType.order(:typeName), :id, :typeName,
 {include_blank:true }, {:class => "types"} %>
Run Code Online (Sandbox Code Playgroud)

当 select 的值更改时,我想生成一些复选框,为此我有 div:

<div id="sub_types"> </div>
Run Code Online (Sandbox Code Playgroud)
我想在哪里生成我的复选框

def show_sub_types
	@rtype = params[:id];
        @stypes = RequestSubType.where("request_type_id=?", @rtype).all
	 respond_to do |format|
  	 format.js
	 format.html
	end
end
Run Code Online (Sandbox Code Playgroud)
我的方法抓取所有子类型并将它们传递给 js 文件 show_sub_types.js.erb

$("#sub_types").html("");
$("#sub_types").append("<%= j render 'show_sub_types', stypes: @stypes %>");
Run Code Online (Sandbox Code Playgroud)
在我的 js 文件中,我渲染了部分 show_sub_types.html.erb,我想在其中生成我的复选框:

<% stypes.each do |type| %>
	<%= check_box_tag "subtype", type.id %>
	<%= type.subTypeName %>
	<br>
<% end %>
Run Code Online (Sandbox Code Playgroud)

在我的部分我做这样的事情。此代码生成我的复选框。他们看起来像这样:

<input type="checkbox" name="subtype" id="subtype" value="1">
Run Code Online (Sandbox Code Playgroud)

但现在我不知道如何用我的表单提交这些复选框值。我想在 db 中存储多个复选框值作为数组。

html javascript checkbox ruby-on-rails

2
推荐指数
1
解决办法
200
查看次数

JavaScript 字符串连接在末尾添加了额外的空格

var code ='';
alert(branch+"t"); // resutl: 123t
for(var i=0;i<endVar;i++){
  code = code+branch;
}
alert(code);// result: 123 123 123 etc..
Run Code Online (Sandbox Code Playgroud)
我有分支字符串 var 和代码 var。如果我执行警报分支+“t”,我会得到123t,所以我想我的分支变量末尾没有任何空格。但是在执行 for 循环并警告 code var 之后,我得到 123 123 123,因此在每次将分支连接到代码 var 后我都会添加空格。可能是什么问题?

javascript

2
推荐指数
1
解决办法
2902
查看次数

在Flask-SqlAlchemy中联接表

假设我有几个表并想要执行联接查询:

schedule_calendars = ScheduleCalendar.query\
            .join(Schedule)\
            .join(ClinicBranchHasDoctor)\
            .filter_by(clinic_branch_id=clinic_id, doctor_has_specialty_id=doctor_speciality_id).all()
Run Code Online (Sandbox Code Playgroud)

问题是我的结果将仅包含ScheduleCalendar类的属性。如何查询,以便我的结果将包含所有联接表的属性。

时间表:

    id = Column(db.Integer, primary_key=True)
    added_date = Column(db.DateTime(timezone=True), default=get_current_time, nullable=False)
    start_date = Column(db.Date, nullable=False)
    name = Column(db.String(128), nullable=False)
    is_valid = Column(db.Boolean, default=IS_VALID, nullable=False)
    slot_size = Column(db.Integer, default=30)
Run Code Online (Sandbox Code Playgroud)

ScheduleCalendar:

  schedule_id = Column(db.Integer, db.ForeignKey("schedules.id"), nullable=False)
Run Code Online (Sandbox Code Playgroud)

ClientBranchHasDoctor:

schedule_id = Column(db.Integer, db.ForeignKey("schedules.id"), nullable=False)
Run Code Online (Sandbox Code Playgroud)

我在这里跳过了一些属性。我认为最重要的是我的表具有适当的约束,否则联接将失败。

python sqlalchemy flask

2
推荐指数
1
解决办法
5735
查看次数

Meteor:未捕获 RangeError:超出最大调用堆栈大小

我对流星很陌生。我正在做简单的应用程序。这是我遇到的问题:

Template.newFeedForm.events({
   'submit #new-feed-form'(event) {
       event.preventDefault();

       const target = event.target;
       const text = target.text;

       Meteor.call('feeds.insert', text);

       target.text.value = '';
   }
});
Run Code Online (Sandbox Code Playgroud)

所以我有 newFeedForm 模板,在我的 feeds.js 中我有

Meteor.methods({
    'feeds.insert'(text){
        check(text, String);
        //check(hashtag, String);

        // Make sure the user is logged in before inserting a task
        if (! this.userId) {
            throw new Meteor.Error('not-authorized');
        }
        console.log(this.userId);

        // Feeds.insert({
        //     text: text,
        //     owner: this.userId,
        //     username: Meteor.users.findOne(this.userId).username,
        //     createdAt: new Date()
        // });
    }
});
Run Code Online (Sandbox Code Playgroud)

我在这里注释掉了 Feeds.insert ,认为它会导致问题。看来是有些不一样了。每当执行 Meteor.call 时,我都会得到以下信息:

Uncaught RangeError: Maximum …
Run Code Online (Sandbox Code Playgroud)

meteor

2
推荐指数
1
解决办法
5592
查看次数

Postgres LIMIT/OFFSET奇怪的行为

我正在使用PostgreSQL 9.6.我有一个查询像这样:

SELECT anon_1.id AS anon_1_id, anon_1.is_valid AS anon_1_is_valid, anon_1.first_name AS anon_1_first_name, anon_1.last_name AS anon_1_last_name,
anon_1.patronymic_name AS anon_1_patronymic_name,
anon_1.experience AS anon_1_experience, anon_1.user_id AS anon_1_user_id, anon_1.rating_points as rating_points

FROM (SELECT DISTINCT ON (doctors.id) doctors.id AS id, doctors.created_at AS created_at, doctors.updated_at AS updated_at, doctors.is_valid AS is_valid, doctors.pretty_url AS pretty_url, doctors.first_name AS first_name, doctors.last_name AS last_name, doctors.patronymic_name AS patronymic_name, doctors.phone AS phone, doctors.birthday AS birthday, doctors.avatar AS avatar, doctors.experience AS experience, doctors.science_degree AS science_degree, doctors.sex_id AS sex_id, doctors.yclients_staff_id AS yclients_staff_id, doctors.user_id AS user_id, …
Run Code Online (Sandbox Code Playgroud)

sql postgresql limit

2
推荐指数
1
解决办法
1234
查看次数

只有在Rails应用程序中重新加载页面后,CoffeScript才能运行

我有两个选择,第二个根据第一个的值而改变.我使用CoffeScript.

		<%= f.collection_select :type, RequestType.order(:typeName), :id, :typeName, 
{include_blank:true }, {:class => "types"} %>
   
 <%= f.grouped_collection_select :subtype, RequestType.order(:typeName), 
:RequestSubTypes, :typeName, :request_type_id, :subTypeName, 
{include_blank:true},
{:class => "subTypes" }  %>
Run Code Online (Sandbox Code Playgroud)
当select 1更改时,第二个选项中的选项会重新加载(第二个选项中的选项取决于第一个选项中选择的选项.这是我的CoffeScript代码:

jQuery ->
	subTypes = $(".subTypes").html()
	$(".subTypes").parent().hide()
	$(".types").change ->
		type = $(".types :selected").text()
		options = $(subTypes).filter("optgroup[label='#{type}']").html()
		if options 	
			$(".subTypes").html(options)
			$(".subTypes").parent().show()
		else
			$(".subTypes").empty()
			$(".subTypes").parent().hide()
Run Code Online (Sandbox Code Playgroud)
它工作,但只有在我重新加载页面后.如果我从一般link_to访问页面,似乎CoffeScript无法正常工作.因此,当导航到该页面时,我总是必须重新加载页面.使用link_to导航到页面看起来像ajax,但我没有使用ajax.这是我的link_to:

<%= link_to "link", new_request_path %>
Run Code Online (Sandbox Code Playgroud)

javascript jquery ruby-on-rails coffeescript

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

控制器#方法中的ActiveRecord :: StatementInvalid

我正在进行复杂的搜索,为此我有特殊的模型:

create_table "cmdb_searches", force: :cascade do |t|
  t.string   "client"
  t.string   "in"
  t.datetime "created_at", null: false
  t.datetime "updated_at", null: false
end
Run Code Online (Sandbox Code Playgroud)

和evms_controller中的方法搜索:

def search
  @cisearch = CmdbSearch.new(search_params)
  @evms = Evm.order('id DESC')
  @evms = @evms.where('client_name=?',@cisearch.client) if #cisearch.client.present?
  @evms = @evms.where('in=?',@cisearch.in) if @cisearch.in.present?
  respond_to do |format|
    format.js
  end
end
Run Code Online (Sandbox Code Playgroud)

并且我的js文件用响应填充了适当的div.如果我将表格留空或仅填写:客户字段,但是如果我填写:它在字段中它将返回错误:

SQLite3::SQLException: near "in": syntax error: SELECT "evms".* FROM "evms" WHERE (in='234543')  ORDER BY id DESC
Run Code Online (Sandbox Code Playgroud)

ruby ruby-on-rails ruby-on-rails-4

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

Rails 中 select 的 onchange 传递参数

<%= f.select :id, options_from_collection_for_select(
              @rtypes, "id", "typeName"), 
              {include_blank: true },																				                            
:onchange => ShowSubTypes() %>	
Run Code Online (Sandbox Code Playgroud)

这里我有 select 的选项,我想要的是将 id 作为参数传递给ShowSubTypes()函数。我的@rtypes变量返回一个数组,因此我不能只传递@rtypes.id.

javascript jquery ruby-on-rails

0
推荐指数
1
解决办法
2537
查看次数

无法访问Rails 4中记录的列

我有带墨盒名称的墨盒模型:

t.string   "cartridge_name", null: false
Run Code Online (Sandbox Code Playgroud)

我对它执行查询:

@cartridge = Cartridge.where('cartridge_name=?', "#{value[:cartridge_id]}")
Run Code Online (Sandbox Code Playgroud)

它返回一个对象:

<ActiveRecord::Relation [#<Cartridge id: 1, cartridge_name: "HP laserjet 3000", note: "", created_at: "2015-04-14 08:05:37", updated_at: "2015-04-14 08:05:37">]>
Run Code Online (Sandbox Code Playgroud)

但是当我尝试访问@ cartridge.id或@ cartridge.cartridge_name时,它会返回错误:

NoMethodError: undefined method `cartridge_name' for #<Cartridge::ActiveRecord_Relation:0x007fbd77bf38f8>
Run Code Online (Sandbox Code Playgroud)

我不知道为什么会这样.什么可能导致错误?

ruby-on-rails

0
推荐指数
1
解决办法
50
查看次数

如何区分rails 4中的方法和属性

我有一个cartridgedosing_edge模型,他们分别有关系has_many和belongs_to.通过这样做我可以打电话:

@cartridge.dosing_edges
Run Code Online (Sandbox Code Playgroud)

检索属于盒式磁带的所有dosing_edges.这样可行,但墨盒模型也有一个dosing_edges属性.如何区分方法和属性.

我的意思是,如果我想访问@cartridge.dosing_edges不调用方法.dosing_edges来检索所有dosing_edges.

或者我应该将我的dosing_edges属性重命名为不同.

ruby-on-rails ruby-on-rails-4

0
推荐指数
1
解决办法
49
查看次数