小编Pap*_*nho的帖子

rails 4使用没有控制器的强参数

Rails 4:我来自rails 3.2.x我有一个问题.如何在没有控制器的情况下使用Strong参数.

我有这个型号:

Track (the only one that has a Controller  )
  has_many :tracksegments, :dependent => :destroy
  has_many :points, :through => :tracksegments
Tracksegment
  belongs_to :track
  has_many :points, :dependent => :destroy
points
  belongs_to :tracksegment
Run Code Online (Sandbox Code Playgroud)

Track是唯一一个拥有Controller的轨道,因此它具有一些强参数.

我想知道在哪里可以放置属于"tracksegment"和"points"的参数在Rails 3.x中它直接在模型中但在rails 4中我没有控制器.

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

5
推荐指数
2
解决办法
1857
查看次数

Django:DetailView 实现 get_queryset()

我收到以下错误:

ImproperlyConfigured at /elearning/7447932a-6044-498a-b902-97cbdd0a4001/
DetailView is missing a QuerySet. Define DetailView.model, DetailView.queryset, or override DetailView.get_queryset().
Run Code Online (Sandbox Code Playgroud)

按照DetailView上的 Django 文档,除非我想覆盖它,否则 get_query 不是强制性的。

查看.py

class CourseDetailView(DetailView):

    model = Course
    template_name='elearning/detail.html'

    def get_object(self):
        course = get_object_or_404(Course, pk=self.kwargs['pk'])
        return self.model.objects.filter(pk=pk)

    def get_context_data(self, **kwargs):
        context = super(CourseDetailView, self).get_context_data(**kwargs)
        context['now'] = timezone.now()
        return context
Run Code Online (Sandbox Code Playgroud)

网址.py

url(r'^(?P<pk>[0-9a-z-]+)/$', views.DetailView.as_view(), name='course-detail'),
Run Code Online (Sandbox Code Playgroud)

列表视图模板

 <a href="{% url 'elearning:course-detail' article.course_id %}">{{ article.title }}</a>
Run Code Online (Sandbox Code Playgroud)

模型.py

class Course(models.Model):
    course_id = models.UUIDField(default=uuid.uuid4, editable=False)
    ...
Run Code Online (Sandbox Code Playgroud)

我想知道为什么要实现 get_queryset()?

添加 get_queryset() 时仍然出现相同的错误

def get_queryset(self):
    qs = super(CourseDetailView, …
Run Code Online (Sandbox Code Playgroud)

django django-class-based-views django-1.11

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

如何增加Jupyter笔记本情节的大小

使用calmap包我创建了一个热图日历,但我想知道如何增加字体或图的大小.

import numpy as np; np.random.seed(sum(map(ord, 'calmap')))
import pandas as pd
import calmap

all_days = pd.date_range('1/15/2014', periods=700, freq='D')
days = np.random.choice(all_days, 500)
events = pd.Series(np.random.randn(len(days)), index=days)

calmap.yearplot(events, year=2015)
Run Code Online (Sandbox Code Playgroud)

在此输入图像描述

python jupyter-notebook

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

Rails 4如何从Model调用accessible_attributes

嗨我在Rails演员(导入csv Excel文件)上尝试这个tuto .我在这条线上有错误product.attributes = row.to_hash.slice(*accessible_attributes)

undefined local variable or method##的accessible_attributes'

这是我的模特.

class Product < ActiveRecord::Base
  require 'roo'
  validates_presence_of :price
  #attr_accessible :name, :price, :released_on #I removed this line (rails 4)


  def self.to_csv(options = {})
    CSV.generate(options) do |csv|
      csv << column_names
      all.each do |product|
        csv << product.attributes.values_at(*column_names)
      end
    end
  end

  def self.import(file)
    spreadsheet = open_spreadsheet(file)
    header = spreadsheet.row(1)
    (2..spreadsheet.last_row).each do |i|
      row = Hash[[header, spreadsheet.row(i)].transpose]
      product = find_by_id(row["id"]) || new
      product.attributes = row.to_hash.slice(*accessible_attributes)
      product.save!
    end
  end

  def self.open_spreadsheet(file) …
Run Code Online (Sandbox Code Playgroud)

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

3
推荐指数
2
解决办法
5440
查看次数

Rails 4 railties的可执行"rails"与rails冲突

我有rails 4,这是我的默认版本(我仍然想要它).但我喜欢在我的电脑上添加rails 3.2.在以下命令上:gem install rails -v 3.2.16

我有这个警告:

railties's executable "rails" conflicts with rails
Overwrite the executable? [yN] 
Run Code Online (Sandbox Code Playgroud)

我想知道这是否会给我的4.0.1带来一些错误?

ruby-on-rails bundler rvm ruby-on-rails-3

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

github访问被拒绝访问推送

嗨,我正在关注rails应用程序的初学者教程.但我在推送存储库时遇到了问题.每次我正在做和git push -u到origin master我有这个错误.

guinslym@ubuntu:~/Documents/rails_tuto/first_app$ git remote add origin https://github.com/guinslym/first_app.git
fatal: remote origin already exists.
guinslym@ubuntu:~/Documents/rails_tuto/first_app$ git push -u origin master
Warning: Permanently added the RSA host key for IP address '192.30.252.130' to the list of known hosts.
Permission denied (publickey).
fatal: Could not read from remote repository.

Please make sure you have the correct access rights
and the repository exists.
guinslym@ubuntu:~/Documents/rails_tuto/first_app$ 

guinslym@ubuntu:~/Documents/rails_tuto/first_app$ ssh -vT git@github.com
OpenSSH_5.9p1 Debian-5ubuntu1.1, OpenSSL 1.0.1 14 Mar 2012
debug1: Reading configuration data /etc/ssh/ssh_config …
Run Code Online (Sandbox Code Playgroud)

git github git-push

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

Saas和API之间有什么区别.(SaaS vs Api)

我想知道SaaS和API之间的区别.我觉得它在某种程度上是相同的.那么让我说我构建一个Rails或Flask api,我可以说我正在建立一个Rails或Flask SaaS吗?

saas:软件即服务(SaaS)是一种软件分发模型,其中应用程序由供应商或服务提供商托管,并通过网络(通常是Internet)提供给客户.(来源)

api:API代表应用程序编程接口,它只是一种获取信息并将信息传递给可信赖合作伙伴的方法.类别.人与博客.(谷歌)

saas

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

使用python 3.7的Docker错误:生成器表达式必须括起来

嗨,我正在关注django教程(Quickstart:Compose和Django)..我有这个错误:

SyntaxError: Generator expression must be parenthesized
Run Code Online (Sandbox Code Playgroud)

追溯

root@localhost:~# docker-compose up
Starting root_db_1 ... done
Starting root_web_1 ... done
Attaching to root_db_1, root_web_1
db_1   | 2018-09-09 00:09:10.440 UTC [1] LOG:  listening on IPv4 address "0.0.0.0", port 5432
db_1   | 2018-09-09 00:09:10.440 UTC [1] LOG:  listening on IPv6 address "::", port 5432
db_1   | 2018-09-09 00:09:10.442 UTC [1] LOG:  listening on Unix socket "/var/run/postgresql/.s.PGSQL.5432"
db_1   | 2018-09-09 00:09:10.481 UTC [21] LOG:  database system was interrupted; last known up at …
Run Code Online (Sandbox Code Playgroud)

django docker

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

用Nokogiri计算xml中的元素

我想明白为什么count给我5

如果我是根本元素并且我想要了解我的孩子,它应该给我2.

doc = Nokogiri::XML(open('link..to....element.xml'))
root = doc.root.children.count
puts root



<element>
  <name>Married with Children</name>
  <name>Married with Children</name>
</element>
Run Code Online (Sandbox Code Playgroud)

ruby

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

ruby从一个对象写json文件

我收到一个错误expected ':' after property name in object at line 1 column 15,我怎么能摆脱'=>'?当我在to_json方法中用":"替换"=>"时,我收到一个错误syntax error, unexpected ':', expecting =>

require 'json'    
class Province
        attr_accessor :provOrigine, :destination, :total, 
                        :q1, :q2, :q3, :q4

        def initialize(line)
            @provOrigine =  line.split(';').first.split(",").first
            @destination =  line.split(';').at(1).split(',').first
            @q1 = (line.split(';').at(4)).to_i
            @q2 = (line.split(';').at(5)).to_i
            @q3 = (line.split(';').at(6)).to_i
            @q4 = (line.split(';').at(7)).to_i
        end
        def to_json
          {'provOrigine' => @provOrigine.to_s, 'destination' => @destination.to_s, 'q1' => @q1.to_s, 'q2' => @q2.to_s, 'q3' => @q3.to_s, 'q4' => @q4.to_s}
        end


    end
Run Code Online (Sandbox Code Playgroud)

... prov_instances =包含省的所有实例......

File.open("file_json_complete.json", …
Run Code Online (Sandbox Code Playgroud)

ruby json object

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