作为Rails的新手,我很难找到一个网站或参考资料,给出了Ruby on Rails的简要总结.我理解MVC,ActiveRecord以及基本级别的那些东西,但我很难理解一些关系和基础知识.例如:
:content_for或render部分)呈现信息的最佳方法是什么?我不应该使用哪些方法?lib文件夹的用途吗?我已经在StackOverflow上阅读了关于这个问题的一些回复,但是所有这些回复只是指向我需要阅读的300多页的书,而我只想简要概述什么是重要的.
我已经了解的一些资源,但没有为新用户提供基本概念的简明摘要:
感谢您提供的任何帮助,参考或指导!
P.S. I would like this wiki to become a living document, so please add to it, edit it, etc. as you feel necessary.
比方说,我有一个IBAction,它连接到界面构建器中的UIButton.
- (IBAction)functionToBeCalled:(id)sender
{
// do something here
}
Run Code Online (Sandbox Code Playgroud)
在我的代码中,例如在另一种方法中说,调用IBAction的最佳方法是什么?
如果我试着像这样调用它,我收到一个错误:
[self functionToBeCalled:];
Run Code Online (Sandbox Code Playgroud)
但是,如果我试着像这样打电话(我觉得有点作弊),它运作正常:
[self functionToBeCalled:0];
Run Code Online (Sandbox Code Playgroud)
正确调用它的正确方法是什么?
我试图访问一个404事件,我可以看到回来作为404与firebug,但错误功能没有踢,用我的下面的代码我总是得到错误:成功?
即.
$.ajax({
type: 'get',
url: 'url: 'https://admin.instantservice.com/resources/smartbutton/5702/10945/available.gif?' + Math.floor(Math.random()*10001),
success: function(data, textStatus, XMLHttpRequest){
console.log('Error: ' + textStatus);
},
error:function (xhr, ajaxOptions, thrownError){
alert(xhr.status);
alert(xhr.statusText);
alert(xhr.responseText);
}
});
Run Code Online (Sandbox Code Playgroud)
我再次知道1000%,我在firebug中获得404 Not Found,它永远不会触发错误.
我错过了什么吗?
我正在实现current_page?一个视图来测试当前控制器和动作是否等于某个值,但是当在该控制器/动作组合上时它不会返回true.
- if current_page?(:controller => 'pages', :action => 'main')
# doesn't return true when on that controller/action combination
Run Code Online (Sandbox Code Playgroud)
它工作的唯一方法是如果我使用更详细的方法,如下所示:
- if controller.controller_name == 'pages' && controller.action_name == 'main'
# this works just fine
Run Code Online (Sandbox Code Playgroud)
我的语法错了还是在这里发生了其他事情?有没有更好的方法来做到这一点,比如设置BOOL或者这是正确的方法吗?
最终目标是仅在主着陆页上显示某个标题,同时在所有其他页面上显示不同的标题.
编辑:相关输出来自rake routes:
pages_main GET /pages/main(.:format) {:controller=>"pages", :action=>"main"}
root /(.:format) {:controller=>"pages", :action=>"main"}
Run Code Online (Sandbox Code Playgroud)
此外,这是渲染时的服务器输出:
Started GET "/" for 127.0.0.1 at 2011-03-03 16:54:40 -0500
Processing by PagesController#main as HTML
Rendered pages/main.html.haml within layouts/application (203.8ms)
Run Code Online (Sandbox Code Playgroud) 我的项目使用Objective-C和Swift代码.当用户登录时,它会为用户首选项调用一组apis,我有一个DataCoordinator.swift类来调度API操作,我从UserDetailViewController.m类调用此类来加载用户首选项.在使用Xcode 9 beta 4将我的代码迁移到Swift 4之前,这种用法正常工作.现在当我登录时,我在DataCoordinator类中给出了这个错误.下面是我的DataCoordinator和Viewcontroller类的示例.
DataCoordinator.swift
import UIKit
@objcMembers
class DataCoordinator: NSObject {
//MARK:- Private
fileprivate var user = myDataStore.sharedInstance().user
fileprivate var preferenceFetchOperations = [FetchOperation]()
fileprivate func scheduleFetchOperation(_ operation:FetchOperation, inFetchOperations operations:inout [FetchOperation]) {
guard operations.index(of: operation) == nil else { return }
operations.append(operation)
}
fileprivate func completeFetchOperation(_ fetchOperation:FetchOperation, withError error:Error?, andCompletionHandler handler:@escaping FetchCompletionHandler) {
func removeOperation(_ operation:FetchOperation, fromOperations operations:inout [FetchOperation]) {
if operations.count > 0 {
operations.remove(at: operations.index(of: fetchOperation)!)
handler(error)
}
}
if preferenceFetchOperations.contains(fetchOperation) {
removeOperation(fetchOperation, fromOperations: &preferenceFetchOperations)
}
} …Run Code Online (Sandbox Code Playgroud) 滚动选择器视图w /零数据(零行)时,我收到断言失败.在滚动选择器视图时,我遇到了这个崩溃.在iOS 6上测试
*断言失败 - [UITableViewRowData rectForRow:inSection:],/ SourceCache/UIKit_Sim/UIKit-2372/UITableViewRowData.m:1630
*由于未捕获的异常'NSInternalInconsistencyException'而终止应用程序,原因:'请求无效索引路径处的rect(2个索引[0,0])'
任何帮助都很明显.
在我详细介绍之前,我会明确指出:有没有人想出办法让Carrierwave保存文件,其名称为时间戳或每个文件唯一的任意字符串?
默认情况下,Carrierwave将每个文件及其备用版本保存在其自己的目录中(以型号ID号命名).我不是这个的粉丝,因为为了使用大的圆形数字而不是一个1000的目录,文件(在我的情况下是图片)中我们得到一个目录,其中有1,000个子目录,每个子目录有一个或两个文件.呸.
现在,当您覆盖Uploader的store_dir方法时,如下所示:
def store_dir
"uploads/#{model.class.to_s.underscore}/#{mounted_as}"
end
Run Code Online (Sandbox Code Playgroud)
你最终得到了我想要的确切行为.所有文件(图片)都进入一个大的快乐文件夹.当对象被删除时,不再有子文件夹.
只有一个问题.文件冲突.如果你上传delicious_cake.jpg两次,那么即使它们是美味蛋糕的两张不同的照片,它也会覆盖第一个!这显然是为什么该store_dir方法/#{model.id}在它返回的值的末尾有额外的限制.
那么该怎么办?在阅读了一下后,我发现在生成的上传器文件中有一个明显的解决方案被注释掉了.
# Override the filename of the uploaded files:
# Avoid using model.id or version_name here, see uploader/store.rb for details.
# def filename
# "something.jpg" if original_filename
# end
Run Code Online (Sandbox Code Playgroud)
经过一番搜索,我发现有人做了以下事情
def filename
@name ||= "#{secure_token}.#{file.extension}" if original_filename
end
Run Code Online (Sandbox Code Playgroud)
这让我思考,为什么不这样做呢
def filename
@name ||= "#{(Time.now.to_i.to_s + Time.now.usec.to_s).ljust(16, '0')}#{File.extname(original_filename)}"
end
Run Code Online (Sandbox Code Playgroud)
事情变得非常糟糕.这个问题filename显然是为文件的每个版本调用,所以我们最终得到文件名,如1312335603175322.jpg和thumb_1312335603195323.jpg.请注意细微差别?每个文件名都基于filename为该特定版本调用的时间.那根本不会做.
我接下来厌倦了使用model.created_at时间戳的基础.只有一个问题,即第一个版本返回nil,因为它还没有放入数据库.
经过一番思考后,我决定在我的照片控制器中尝试以下操作.
def create
if params[:picture] and params[:picture][:image]
params[:picture][:image].original_filename = …Run Code Online (Sandbox Code Playgroud) 我有一个ruby哈希,我想用RABL渲染.哈希看起来像这样:
@my_hash = {
:c => {
:d => "e"
}
}
Run Code Online (Sandbox Code Playgroud)
我试图用一些RABL代码渲染它:
object @my_hash => :some_object
attributes :d
node(:c) { |n| n[:d] }
Run Code Online (Sandbox Code Playgroud)
但我收到了 {"c":null}
如何使用RABL渲染?
在我的应用程序中,MFMailComposeViewController工作正常,但创建MFMessageComposeViewController的新实例失败.
以下是两者的代码:
-( IBAction)sendSMS: (id)sender
{
MFMessageComposeViewController *picker = [[[MFMessageComposeViewController alloc] init] autorelease];
picker.messageComposeDelegate = self;
NSArray *toRecipients = [NSArray arrayWithObject: cell.currentTitle ];
picker.recipients = toRecipients;
[self presentModalViewController:picker animated:YES];
}
-( IBAction)sendEmail: (id)sender
{
MFMailComposeViewController *picker = [[[MFMailComposeViewController alloc] init] autorelease];
picker.mailComposeDelegate = self;
NSArray *toRecipients = [NSArray arrayWithObject: email.currentTitle ];
[picker setToRecipients:toRecipients];
[self presentModalViewController:picker animated:YES];
}
Run Code Online (Sandbox Code Playgroud)
由于电子邮件视图控制器工作正常,所以一切都正确链接似乎很明显.是否有一些我错过的配置明智?
crash ×2
iphone ×2
ruby ×2
swift ×2
action ×1
carrierwave ×1
cocoa-touch ×1
controller ×1
conventions ×1
current-page ×1
filenames ×1
ibaction ×1
ios ×1
ios11 ×1
ios4 ×1
ios6 ×1
jquery ×1
mfmailcomposeviewcontroller ×1
objective-c ×1
rabl ×1
reference ×1
rendering ×1
summary ×1
swift4 ×1
uipickerview ×1
xcode9-beta ×1