我已经开始使用rails 3 beta3了,我发现控制器内的@template变量是零.如何在控制器内调用助手方法?谢谢
我正在编写一个Rails帮助器方法,它将包装器html添加到捕获的内容块并替换content_for方法,例如
- content_for :header do
//haml code
Run Code Online (Sandbox Code Playgroud)
..会成为
- content :header do
//haml code
Run Code Online (Sandbox Code Playgroud)
为了做到这一点,我使用的是Haml和Ruby块.这就是它的样子
def content(name,&block)
content_for name do
capture_haml do
haml_tag "div",{:id=>name.to_s} do
haml_tag "div",{:id=>"#{name.to_s}_group"} do
block
end
end
end
end
end
Run Code Online (Sandbox Code Playgroud)
但我不能让这个工作.没有错误消息.它只是没有显示块!我不确定我做错了什么.我很欣赏第二个意见.
所有!也许有人知道为什么这样:
def index_link(object, content = t("#{object.to_s.pluralize}.index"))
#link_to(content, {:controller=>object.to_s.pluralize}, :scope=>"") if can?(:read, p(object)) #this doesn't work too
link_to(content, :controller=>"trademarks")
#link_to(content, trademarks_path) #this do work, but I need to set path from object
end
Run Code Online (Sandbox Code Playgroud)
像这样的例外:
No route matches {:controller=>"devise/trademarks"}
Run Code Online (Sandbox Code Playgroud)
堆:
actionpack (3.0.3) lib/action_dispatch/routing/route_set.rb:424:in `raise_routing_error'
actionpack (3.0.3) lib/action_dispatch/routing/route_set.rb:406:in `generate'
actionpack (3.0.3) lib/action_dispatch/routing/route_set.rb:453:in `generate'
actionpack (3.0.3) lib/action_dispatch/routing/route_set.rb:481:in `url_for'
actionpack (3.0.3) lib/action_dispatch/routing/url_for.rb:131:in `url_for'
actionpack (3.0.3) lib/action_view/helpers/url_helper.rb:99:in `url_for'
actionpack (3.0.3) lib/action_view/helpers/url_helper.rb:236:in `link_to'
app/helpers/application_helper.rb:53:in `index_link'
app/views/layouts/application.html.erb:20:in `_app_views_layouts_application_html_erb___741328535__615736668_0'
actionpack (3.0.3) lib/action_view/template.rb:135:in `send'
actionpack (3.0.3) lib/action_view/template.rb:135:in `render'
Run Code Online (Sandbox Code Playgroud)
以及如何使它工作?
我有一个select_tag从我用来执行搜索的@users数组中填充.当用户首次登陆页面时,我希望它显示空白或自定义标签,而不是数组中的第一个项目?
这是使用select_tag助手的选项吗?要插入空白""选项?
我的助手到目前为止:
<%= select_tag :search_user, options_from_collection_for_select(@users, "id", "name"), :class => 'submittable'%>
Run Code Online (Sandbox Code Playgroud) 我试图通过帮助器呈现content_for块的结果.
我有一个模板(HAML)和布局如下:
# app/views/books/show.html.haml
-content_for(:page_header) do
%h1= @book.title
# app/views/application.html.haml
...
=yield(:page_header)
...
Run Code Online (Sandbox Code Playgroud)
这非常好.
我想要做的是在帮助器中进行调用.所以我的目标是:
# app/views/books/show.html.haml
-content_for(:page_header) do
%h1= @book.title
# app/views/application.html.haml
....
=page_header(block)
....
# app/helpers/application.rb
....
def page_header(&block)
# Some view logic
# ...
=yield(:page_header)
end
....
Run Code Online (Sandbox Code Playgroud)
我可以通过调用帮助程序来实现部分结果:
# app/views/application.html.haml
=page_header { yield(:page_header) }
# app/helpers/application.rb
def page_header(&block)
yield
end
Run Code Online (Sandbox Code Playgroud)
但这让我觉得难看.
有任何想法吗?提前致谢.
解答:再次使用content_for(:page_header)进行渲染.
看起来我的助手(有时是我的模特)在每次使用Spork时都没有重新加载.我应该把什么放进我的"Spork.each_run"区块?
我的index.html.erb中有以下代码
<%= select_tag 'team_id', options_for_select(@teams.map{|team| ["#{team.name} #
{team.nick}", team.id] }) %>
Run Code Online (Sandbox Code Playgroud)
我会在哪个块中添加link_to帮助器?我可以为select_tag添加link_to吗?
所需的link_to将转到'/ Teamleader/ID_OF_OPTION_PICKED'
更新:
更清楚; 当用户从select标签中选择一个选项时,我想将页面重定向到所需的链接(来自link_to).
在我的application_helper.rb
文件中,我有一个这样的函数:
def internal_request?
server_name = request.env['SERVER_NAME']
[plus more code...]
end
Run Code Online (Sandbox Code Playgroud)
控制器,模型和视图中需要此功能.所以,我把这段代码放在lib/
目录中的实用程序函数文件中.然而,这不起作用:我抱怨request
没有被定义.
如何访问目录中request
文件中的对象lib/
?
在MVC 3项目中使用剃刀助手时出现错误(确实将cshtml文件放在app_code中).看起来生成的代码使用了错误的程序集引用.
使用WebMatrix.Data;
使用WebMatrix.WebData;
编译说:
CS0246:找不到类型或命名空间名称'WebMatrix'(您是否缺少using指令或程序集引用?)
将它们放入GAC并没有改变任何事情.我没有得到它吗?或者这是一个错误?有任何想法吗?
经过几个小时的搜索,我仍然找不到我的答案L5
.
我的问题是:
我想建立一个像这样的链接:
localhost:800/songs/you-drive-me-crazy
Run Code Online (Sandbox Code Playgroud)
但是得到的是:
localhost:800/songs?you-drive-me-crazy
Run Code Online (Sandbox Code Playgroud)
我的路由参数正在变为查询字符串.
//routes.php
$router->bind('songs', function($slug)
{
return App\Song::where('slug', $slug)->first();
});
$router->get('songs', ['as' => 'songs.index', 'uses' => 'SongsController@index'] );
$router->get('songs/{songs}', ['as' => 'songs.show', 'uses' => 'SongsController@show'] );
Run Code Online (Sandbox Code Playgroud)
我在用:
{!! link_to_route('songs.index', $song->title, [$song->slug]) !!}
Run Code Online (Sandbox Code Playgroud)
我已经尝试了一切但尚未成功,你的建议可能会有所帮助.
谢谢.