小编oka*_*56k的帖子

Git checkout -b,分支已经存在

当我合并两个分支并且它们无法自动合并时,Github提供以下指令:

步骤1:从项目存储库中引入更改并进行测试.

git fetch origin
git checkout -b master origin/master
git merge develop
Run Code Online (Sandbox Code Playgroud)

第2步:合并更改并在GitHub上更新.

git checkout develop
git merge --no-ff master
git push origin develop
Run Code Online (Sandbox Code Playgroud)

但是,在这种情况下,分支master已经存在于本地,并且该行git checkout -b master origin/master返回以下消息:

git checkout -b master origin/master
fatal: A branch named 'master' already exists.
Run Code Online (Sandbox Code Playgroud)

在这种情况下,更换该行是正确的做法git checkout master吗?我有点想知道这一点,有点担心git checkout master可能会做什么,而不是-b.

git github

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

Rails:如果has_many关系发生了变化

我有这两个班.

class Article < ActiveRecord::Base
  attr_accessible :body, :issue, :name, :page, :image, :video, :brand_ids
  has_many :publications
  has_many :docs, :through => :publications
end

class Doc < ActiveRecord::Base
  attr_accessible :issue_id, :cover_id, :message, :article_ids, :user_id, :created_at, :updated_at, :issue_code, :title, :template_id
  has_many :publications, dependent: :destroy
  has_many :articles, :through => :publications, :order => 'publications.position'
  has_many :edits, dependent: :destroy
  accepts_nested_attributes_for :articles, allow_destroy: false
end
Run Code Online (Sandbox Code Playgroud)

如何编写条件语句以查看@doc.articles更新后是否已更改@doc

if @doc.articles.changed?
  ...
end
Run Code Online (Sandbox Code Playgroud)

上面给了我一个错误.我找不到正确的语法.

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

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

使用Prawn在pdf中添加图像

我使用Prawn作为pdf生成器将图像添加到PDF中时遇到问题.我正在尝试使用以下代码添加图像:

def header
  text "something"
  image "#{Prawn::DATADIR}/images/logo_small.png"
end
Run Code Online (Sandbox Code Playgroud)

但app回复我的错误如下:

 uninitialized constant Prawn::DATADIR
Run Code Online (Sandbox Code Playgroud)

哪里出错了?

PS我的应用程序在Rails 3.1和Ruby 1.9.2上运行.

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

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

从标题案件到案件案件

我正在尝试编写一个帮助器来将字符串转换"something_like_this""Something like this".我正在使用"something_like_this".titlecase它,"Something Like This"但除了第一个之外,我还低调了每个大写字母.

我想我正在寻找这样的东西:

def write_sentence
  string.titlecase.gsub!(/UPPERCASE-TO-LOWERCASE-EXCEPT-FIRST/)
  #that should be something to lowercase everything except the first letter
  return string
end
Run Code Online (Sandbox Code Playgroud)

所以在视图中我可以写string.write_sentence,并让它返回我想要的.有什么想法吗?

谢谢!

编辑

我应该提到字符串有时只能是一个单词,在这种情况下字符串应该从转换"something""Something".

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

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

Rails 4:这个方法有什么问题?

我正在将一个应用程序升级到Rails 4,我不能在我的生活中弄清楚这个方法有什么问题.罪犯的更新方法:

def update
  respond_to do |format|
    if @doc.articles.find_index { |a| a.changed? }
      @doc.publications.destroy_all
    end
    if @doc.update_attributes(params[:doc])
      @doc.create_activity :update, owner: current_user
      if current_user.brand.editable? && params[:editing]
        format.html { redirect_to editing_url(@doc) }
      else 
        format.html { redirect_to share_url(@doc.user.ftp, @doc) }
      end
    end
  end
end
Run Code Online (Sandbox Code Playgroud)

单击会submit生成此错误:

ActionController::UnknownFormat in DocsController#update
Run Code Online (Sandbox Code Playgroud)

并突出显示这一行:

respond_to do |format|
Run Code Online (Sandbox Code Playgroud)

create方法工作正常,它看起来像这样:

def create
  @doc = Doc.new(params[:doc])
  respond_to do |format|
    if @doc.save
      @doc.create_activity :create, owner: current_user
      if current_user.brand.editable? && params[:editing]
        format.html { redirect_to doc_editing_url(@doc) }
      else 
        format.html { redirect_to share_url(@doc.user.ftp, …
Run Code Online (Sandbox Code Playgroud)

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

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

Rails 6.1将返回Content-Type标头,而无需进行修改...使用`#media_type`代替

当我引用此块时,此弃用消息对我意味着什么变化?

def json_response(object, status = :ok)
  render json: object, status: status
end
Run Code Online (Sandbox Code Playgroud)

编辑

消息:

Rails 6.1将返回Content-Type标头,而无需进行修改……请 #media_type改用

ruby ruby-on-rails ruby-on-rails-6

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

Rails,Geocoder和附近

我试图根据与用户的接近程度显示某些位置时遇到轻微问题.在我的控制器中我有这个:

  if User.find(current_user)
    @user = User.find(current_user)
    @locations = Location.near(params[:latitude => @user.latitude, :longitude => @user.longitude], 50, :order => :distance)
  end
Run Code Online (Sandbox Code Playgroud)

用户存储纬度和经度.我想我在Location.near行中没有正确的参数,但我无法弄清楚它们应该是什么.

任何帮助,将不胜感激.

干杯!

ruby ruby-on-rails

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

如何在Rails中清理凌乱的视图和控制器?

我在我的应用程序中有很多这样的代码:

if @document.template.name == "Newsletter"
  ...
end
Run Code Online (Sandbox Code Playgroud)

我意识到的是糟糕而丑陋的代码.我不确定这种代码有哪些替代方案.它有什么最佳实践吗?希望如此.干杯!

样本控制器代码

在此控制器代码示例中,如果名称是,则将图像发布到Twitter "Newsletter".我知道它很乱,而且很多代码都应该移到模型中.我更关心有条件的.

if @document.template.name == "Newsletter"
  source = Magick::Image.read(@document.component.image_newsletter.path).first
  overlay = Magick::Image.read(@document.user.logo.path).first.resize_to_fit(source.columns)
  rec = Magick::Draw.new
  rec.stroke = "##{@document.user.colour1}"
  rec.fill = "##{@document.user.colour1}"
  rec.rectangle 0, 0, source.rows, 5
  lank = source.extent(source.columns, source.rows+overlay.rows, 0 ,0)
  combo = lank.composite(overlay, Magick::SouthGravity, 0, 0, Magick::OverCompositeOp)
  rec.draw(combo)
  client.update_with_media("#{@document.title}: #{@document.remove_html(@document.components.first.body[0..100])}...", open(combo.to_blob))
else
  client.update("#{@document.title}: #{@document.remove_html(@document.components.first.body[0..100])}... http://domain.com#{share_path(@document.user.ftp, @document)}")
end
Run Code Online (Sandbox Code Playgroud)

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

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

Rails:CSV.生成和重命名标头

我将一堆记录导出到 CSV,如下所示:

def self.to_csv
  attributes = %w(full_name first_name last_name created_at status)

  CSV.generate(headers: true) do |csv|
    csv << attributes
    all.each do |lead|
      csv << attributes.map { |attr| lead.send(attr) }
    end
  end
end
Run Code Online (Sandbox Code Playgroud)

但我需要标题读起来像Full Namenot full_name。我需要一个哈希来匹配名称,这很好,但是如何将新的标头名称写入其中的 CSV 文件 CSV.generate

更新查找哈希。

def lookup
  {
    full_name: 'Full Name', first_name: 'First Name', last_name: 'Last Name', created_at: 'Generation Date', status: 'Status'
  }
end
Run Code Online (Sandbox Code Playgroud)

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

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

未定义formData

我有一个上传文件的回调。相当标准:

function sendFile(file) {
    formData = new FormData()
    formData.append('photo[uploaded_image]', file)
    $.ajax({
      data: formData,
      type: 'POST',
      url: '/photos',
      contentType: false,
      processData: false,
      success: function(response) {
          console.log(`uploaded ${response.photo}`)
      },
      error: function(response) {
          console.log(`couldn't upload: ${response}.`)
      }
})
Run Code Online (Sandbox Code Playgroud)

为什么会出现此错误

未捕获的ReferenceError:未定义formData

在功能的第2行上?

formData = new FormData()
Run Code Online (Sandbox Code Playgroud)

javascript ajax

5
推荐指数
0
解决办法
6232
查看次数