当我合并两个分支并且它们无法自动合并时,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
.
我有这两个班.
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)
上面给了我一个错误.我找不到正确的语法.
我使用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上运行.
我正在尝试编写一个帮助器来将字符串转换"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"
.
我正在将一个应用程序升级到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) 当我引用此块时,此弃用消息对我意味着什么变化?
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
改用
我试图根据与用户的接近程度显示某些位置时遇到轻微问题.在我的控制器中我有这个:
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行中没有正确的参数,但我无法弄清楚它们应该是什么.
任何帮助,将不胜感激.
干杯!
我在我的应用程序中有很多这样的代码:
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) 我将一堆记录导出到 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 Name
not 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) 我有一个上传文件的回调。相当标准:
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)