环境:Rails 3.0.4和Ruby 1.9.2
我有以下表格:
<%= form_tag( {:action => 'show', :format => :pdf}, :method => :post) do %>
.. list of items ...
<%= submit_tag "Show", :onclick => "return checkAllFields(4);", :remote => true %>
<select name="format">
<option name="HTML">HTML</option>
<option name="PDF">PDF</option>
</select>)
<% end %>
Run Code Online (Sandbox Code Playgroud)
如您所见,我已在URL中将格式指定为"pdf".我想要的是根据select选项从控制器请求HTML或PDF响应.这两个查询都是单独工作的,即我可以渲染HTML或PDF但不能使其成为动态用户选择.(我甚至无法使用两个单独的硬编码按钮)
控制器代码很明显
def show
# code to locate items here
respond_to do |format|
format.html
format.pdf { render :layout => false }
prawnto :filename => "list.pdf", :prawn => { }
end
end
Run Code Online (Sandbox Code Playgroud) 我正在学习Python.
我很想知道这是什么意思?
count = 10**6
Run Code Online (Sandbox Code Playgroud)
我尝试打印变量"count"但是给了我一个错误.
有人能给我一个线索吗?
最好的祝福,
我是IoC和DI的初学者.我希望能够使用autofac(或任何其他合适的.NET IoC工具)动态解析连接和连接工厂.
一个场景可能是将连接实现更改为另一个具有更多跟踪设施的实现.
当我将DI和IoC应用于下面的代码时,我在构造函数等中得到了一堆namedParameter.连接工厂返回一个带有唯一端口的新连接(愚蠢的例子,只是为了表明我需要在工厂中保持某种状态)
我想我可以使用属性注入的IP和端口范围,但是这样,我不能保证连接将具有IP或端口,这是构造函数的要点.此外,命名参数也使我依赖于参数的名称.
想法,模式,IoC指针非常感谢!
更新:
更具体:如何将连接类更改为可注入?我应该选择房产吗?或者我能做的任何技巧都可以通过构造函数参数获得更加类型安全的解析?
public interface IConnection {
void Open();
void Close();
string Execute(string command);
}
public interface IConnectionFactory {
IConnection CreateConnection();
}
public class Connection : IConnection {
...
public Connection(String ip, int port) {
_ip = ip;
_port = port;
}
public string Execute() {}
public void Open() {}
public void Close() {}
}
public class ConnectionFactory : IConnectionFactory {
//How would I resolve this?
public ConnectionFactory(string ip, int fromPort) {
...
}
public …Run Code Online (Sandbox Code Playgroud) 我仍然相当熟悉hibernate并且似乎无法找到如何做到这一点,也许我正在使用错误的术语进行搜索.我从我的UI传递用户提供的值,在下面的示例中,这是变量testvalue.这是为了进行搜索,所以如果用户输入的内容包含在条目中的任何位置,e.filenametxt我希望它将该行作为匹配返回.
例如,testvalue = "file1"我希望它能够在哪里找到一行e.filenametxt = "file1/someotherinfo/"
String SQL_QUERY = "from EdrmTest e where e.filenametxt is :foreignkeytest ";
Query query = session.createQuery(SQL_QUERY);
query.setParameter("foreignkeytest", testvalue);
Run Code Online (Sandbox Code Playgroud)
任何建议表示赞赏.
我有这个小问题,在过去一小时左右一直困扰着我.
string = b'-'
t = struct.pack(">h%ds" % len(string), len(string), string)
print(t)
Run Code Online (Sandbox Code Playgroud)
这个包的结果是b'\ x00\x01-'
我遇到的问题是我无法弄清楚如何解压缩结果b'\ x00\x01-'以便它只是' - ',是的.我知道我可以删除前面的废话,但它会变得有点复杂.我试着在这里简化它.希望有人可以帮助我.:)
我在Android中构建了一个简单的音乐播放器.每首歌曲的视图都包含一个SeekBar,实现方式如下:
public class Song extends Activity implements OnClickListener,Runnable {
private SeekBar progress;
private MediaPlayer mp;
// ...
private ServiceConnection onService = new ServiceConnection() {
public void onServiceConnected(ComponentName className,
IBinder rawBinder) {
appService = ((MPService.LocalBinder)rawBinder).getService(); // service that handles the MediaPlayer
progress.setVisibility(SeekBar.VISIBLE);
progress.setProgress(0);
mp = appService.getMP();
appService.playSong(title);
progress.setMax(mp.getDuration());
new Thread(Song.this).start();
}
public void onServiceDisconnected(ComponentName classname) {
appService = null;
}
};
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.song);
// ...
progress = (SeekBar) findViewById(R.id.progress);
// ...
}
public void run() { …Run Code Online (Sandbox Code Playgroud) 我想使用链接触发模型的远程操作.基本上所有这个链接需要做的是用一个参数触发一个方法.
这是我的代码:
= link_to 'Move Up', reorder_collection_folder_path(@collection, folder), :reorder => :up, :remote => true
Run Code Online (Sandbox Code Playgroud)
这确实按预期触发了文件夹#reorder控制器操作,但是:reorderparam没有被传递.我的日志说:
Started GET "/collections/1/folders/1/reorder" for 127.0.0.1 at 2011-03-01 18:03:31 -0600
Processing by FoldersController#reorder as JS
Parameters: {"collection_id"=>"1", "id"=>"1"}
Run Code Online (Sandbox Code Playgroud)
那么,我如何通过远程链接传递参数?我在这做错了什么?
我有一个应用程序需要发送用户事件邀请.当用户邀请朋友(用户)参加活动时,如果尚不存在将用户连接到该事件的新记录,则创建该记录.我的模型由user,event和events_user组成.
class Event
def invite(user_id, *args)
user_id.each do |u|
e = EventsUser.find_or_create_by_event_id_and_user_id(self.id, u)
e.save!
end
end
end
Run Code Online (Sandbox Code Playgroud)
用法
Event.first.invite([1,2,3])
Run Code Online (Sandbox Code Playgroud)
我不认为以上是完成任务的最有效方法.我设想了一种类似的方法
Model.find_or_create_all_by_event_id_and_user_id
Run Code Online (Sandbox Code Playgroud)
但是一个不存在.
没有验证的模型
class User
has_many :events_users
has_many :events
end
class EventsUser
belongs_to :events
belongs_to :users
end
class Event
has_many :events_users
has_many :users, :through => :events_users
end
Run Code Online (Sandbox Code Playgroud) 我明白这有点微不足道但......
如果存在,那么获取引用的最佳方法是什么?假设集合包含引用类型的项.
代码示例1:
if (collection.Any())
{
var firstItem = collection.First();
// add logic here
}
Run Code Online (Sandbox Code Playgroud)
上面的示例在集合上有两个单独的调用,开始迭代,一旦检测到第一个就完成.
代码示例2:
var firstItem = collection.FirstOrDefault();
if (firstItem != null)
{
// add logic here
}
Run Code Online (Sandbox Code Playgroud)
上面的示例只对集合进行了一次调用,但引入了一个不必要地在更广范围内的变量.
是否存在与此方案相关的最佳实践?有更好的解决方案吗?
我需要逃避"?" 网址中的字符,以便与搜索/ why-is-it?-1.html等重写网址一起使用
我目前在.htaccess中有以下内容
RewriteEngine on
RewriteRule ^search/(.*)-([0-9]+).html$ index.php?search=$1&page=$2 [L]
Run Code Online (Sandbox Code Playgroud)
谢谢