标签: actionview

如何为Rails的部分渲染查找添加视图路径?

我想要以下目录结构:

views/
  app1/
    users/_user.html.erb
    users/index.html.erb

  app2/
    users/index.html.erb

  shared/
    users/_user.html.erb
    users/index.html.erb
Run Code Online (Sandbox Code Playgroud)

在我看来,我打电话

# app1/users/index.html
<%= render :partial => "user" %>
# => /app1/users/_user.html.erb


# app2/users/index.html
<%= render :partial => "user" %>
# => /shared/users/_user.html.erb
Run Code Online (Sandbox Code Playgroud)

所以基本上,如何告诉Rails检查/ app2/users dir然后检查共享目录,然后才会引发它丢失的模板错误?

更新


我绕过了这个(正如Senthil建议的那样,使用 File.exist?

这是我的解决方案 - 欢迎提出反馈和建议

# application_helper.rb

# Checks for a partial in views/[vertical] before checking in views/shared
def partial_or_default(path_name, options={}, &block)
  path_components         = path_name.split("/")
  file_name               = path_components.pop
  vertical_file_path      = File.join(vertical}, path_components, file_name)
  shared_file_path        = File.join("shared", path_components, file_name)
  full_vertical_file_path = File.join("#{Rails.root}/app/views/", "_#{vertical_file_path}.html.erb")
  attempt_file_path …
Run Code Online (Sandbox Code Playgroud)

ruby-on-rails partial-views actionview actionviewhelper ruby-on-rails-3

20
推荐指数
1
解决办法
2万
查看次数

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

如何从FunctionalTest中调用'time_ago_in_words'?

我在视图中使用'time_ago_in_words'函数,我需要在FunctionalTest中测试输出.

但测试无法看到'time_ago_in_words'辅助函数.

为了使用FunctionalTests中的这些辅助方法,我该怎么办?

ruby-on-rails actionview actionviewhelper

17
推荐指数
1
解决办法
9307
查看次数

rails - 如何在视图中呈现JSON对象

现在我正在创建一个数组并使用:

render :json => @comments
Run Code Online (Sandbox Code Playgroud)

这对于一个简单的JSON对象来说没什么问题,但是现在我的JSON对象需要几个帮助器,它们破坏了一切,并且需要在控制器中包含帮助器,这似乎会导致更多问题而不是解决.

那么,我如何在视图中创建这个JSON对象,在使用帮助器时我不必担心做任何事情或破坏任何东西.现在我在控制器中制作JSON对象的方式看起来像这样的东西?帮我把它迁移到一个视图:)

# Build the JSON Search Normalized Object
@comments = Array.new

@conversation_comments.each do |comment|
  @comments << {
    :id => comment.id,
    :level => comment.level,
    :content => html_format(comment.content),
    :parent_id => comment.parent_id,
    :user_id => comment.user_id,
    :created_at => comment.created_at
  }
end

render :json => @comments
Run Code Online (Sandbox Code Playgroud)

谢谢!

json ruby-on-rails actionview ruby-on-rails-3

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

指定格式:使用render_to_string忽略"xml"

我有一个需要将视图呈现为字符串的操作.该视图称为index.xml.erb.我试图用render_to_string来实现这个目的:

my_string = render_to_string(layout: false, format: "xml")
Run Code Online (Sandbox Code Playgroud)

render_to_string是渲染index.html.erb的内容并将其分配给my_string.我错过了什么?

注意:我知道我可以这样做:

my_string = render_to_string(:action => "#{self.action_name}.xml.erb")
Run Code Online (Sandbox Code Playgroud)

但我很好奇为什么"格式"选项不适用于render_to_string.

ruby-on-rails erb actionview actioncontroller ruby-on-rails-3

17
推荐指数
1
解决办法
3533
查看次数

如何通过Intent在Android中打开文件

如何打开以前存储在"privat"文件系统中的文件?该文件由Web服务下载,应存储在本地fs中.尝试通过Intent(Action_View)打开文件时出现"奇怪"错误.虽然文件存在于文件系统中(在模拟器/ eclipse中看到文件浏览器中的文件),但它不会显示在启动的调用galery活动中.而不是图片,galery显示黑色屏幕没有内容(除了操作栏).尝试通过此方法打开pdf/movie/mp3文件时会出现同样的情况(pdf表示文件已损坏).

BTW:存储在本地fs上的数据有效(没有损坏),我从调试器下载文件(通过pull方法),文件可以在我的工作站上打开...

public void onAttachment(Context context, Attachment attachment) {
    try {
        //Attachment is a structured data object that contains of a byte[], filename and mimetype (like image/jpeg)
        FileOutputStream fos = FileOutputStream fos = context.openFileOutput(attachment.getAttachmentFileName(), Context.MODE_PRIVATE);
        fos.write(attachment.getBinary());
        fos.flush();
        fos.close();
        File f = context.getFileStreamPath(attachment.getAttachmentFileName());
        Uri uri = Uri.fromFile(f);          
        Intent intent = new Intent(Intent.ACTION_VIEW);
        intent.setDataAndType(uri,attachment.getAttachmentMimetype());
        context.startActivity(intent);                  
    } catch (IOException e) {
        e.printStackTrace();
    }
}           
Run Code Online (Sandbox Code Playgroud)

filesystems android actionview android-intent

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

ActionView :: MissingTemplate错误,仅在Bot访问时?

我有一个为我的主页提供服务的动作.它在正常访问时(即通过Web浏览器中的用户)工作正常,但是当由特定Web爬网程序访问时,它会引发以下错误:

 A ActionView::MissingTemplate occurred in tags#promoted:

 Missing template tags/promoted with {:handlers=>[:erb, :rjs, :builder, :rhtml, :rxml], :formats=>["text/*"], :locale=>[:en, :en]} in view paths "/Apps/accounts/app/views", "/usr/local/rvm/gems/ruby-1.9.2-p180@accounts/gems/devise-1.3.0/app/views"
 actionpack (3.0.4) lib/action_view/paths.rb:15:in `find'
Run Code Online (Sandbox Code Playgroud)

似乎机器人正在尝试获取text/*格式,没有模板,这是有道理的,所以我尝试在我的行动中执行以下操作:

  def promoted
   request.format = :html #force html to avoid causing missing template errors
   # more action stuff....
  end
Run Code Online (Sandbox Code Playgroud)

本质上,我试图强制请求的格式为HTML,因此它提供html模板.

然而,每次这些机器人请求此页面时,都会发生丢失的模板错误.

这不是什么大不了的事,但理想情况下我想解决这个错误,如果只是这样我就不再从我的应用程序中收到这些错误电子邮件了.

是唯一的方法来调用文件my_action.text.erb并在其中添加一些乱码吗?或者我可以更优雅地解决这个问题吗?

ruby ruby-on-rails actionview

15
推荐指数
1
解决办法
1459
查看次数

Rails3 ActionView模板处理程序不适用于Production Server

我正在使用Rails 3.2.3/Ruby 1.9.3p125/jbuilder(0.4.0)

在我的view/mycontroller文件夹中,我有一个show.json.jbuilder文件.当我在本地机器上测试一切时,rails s -e production一切正常.JSON被渲染为受到尊重.

但是当我部署到Ubuntu LTS(nginx/unicorn)时,我收到以下错误消息:

ActionView::MissingTemplate (Missing template mycontroller/show, application/show with {:locale=>[:de, :en], :formats=>[:json], :handlers=>[:erb, :builder]}. Searched in:
  * "/home/deployer/apps/myapp/releases/#############/app/views"
):
Run Code Online (Sandbox Code Playgroud)

当我检查我的服务器上是否安装了jbuilder gem时,bundle show jbuilder一切似乎都正确.

奇怪的是错误消息没有显示:handlers=>[:erb, :builder, :jbuilder] jbuilder处理程序显然是丢失的.但是我该如何解决这个问题呢?

编辑:问题不是基于Jbuilder.我试了一下rabl,出现了同样的问题.

有没有提示,如何调试?

这里有更多信息:

的Gemfile

source 'https://rubygems.org'

gem 'rails', '3.2.3'
gem 'jquery-rails'
gem 'mysql2'
gem 'simple_form'

# Gems used only for assets and not required
# in production environments by default.
group :assets do
  gem 'sass-rails',   '~> 3.2.3' …
Run Code Online (Sandbox Code Playgroud)

ruby ruby-on-rails actionview production-environment

14
推荐指数
1
解决办法
3242
查看次数

如何在Ruby on Rails中实现特定于部分的导航?

我有一个Ruby/Rails应用程序,有两个或三个主要"部分".当用户访问该部分时,我希望显示一些子导航.所有三个部分都使用相同的布局,因此我无法"硬编码"导航到布局中.

我可以想到几种不同的方法来做到这一点.我想为了帮助人们投票,我会把它们作为答案.

还有其他想法吗?或者你投票给谁?

ruby templates ruby-on-rails actionview

12
推荐指数
3
解决办法
4905
查看次数

Rails问题与人性化

我试图将作为哈希键的符号人性化

c.each_key{ |f| humanize(f.to_s)}

但由于某种原因,我得到了这样的错误

ActionView::Template::Error (undefined method 'humanize' for #<#<Class:0xb5b77a4>:0xb5b6598>)

知道这里出了什么问题吗?

ruby actionview actionviewhelper ruby-on-rails-3

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