Tag: 视图

Rails 3 Form Helpers:UTF8和其他隐藏字段

风景: ‘text’, :id => ‘title’, :size => ”, :limit => ‘255’ %> LevihackwithCom Title 我正在搞乱表格助手。 上面的代码显示了我的视图文件以及它生成的HTML。 什么是可怕的div充满内联CSS填充隐藏字段我没有明确要求? 什么设置会导致生成这些字段? 有没有办法删除内联CSS?

按升序订单导轨排序

嗨,我有这个模型 型号项目 class Inventory::Item “ItemType” attr_accessible :name end 模型item_type class Inventory::ItemType < ActiveRecord::Base belongs_to :item attr_accessible :number end 然后在控制器中说,我想根据项目名称按升序对类型(具有类ItemType)进行排序。 我怎么做? 例如, ItemType number = 1具有Item name = Table ItemType number = 2具有Item name = Chair ItemType number = 3具有Item name = Window ItemType number = 4具有Item name = Computer 所以不是从数字中对它进行排序,而是希望它基于item.name(ASC)进行排序,如下所示: ItemType number = 2具有Item name […]

rails结构化嵌套资源的路由/控制器/视图

我有以下结构的rails应用程序: user has_many posts post has_many post_comments post_comment has_many comment_replies 我打算使用以下路线来避免深度嵌套。 resources :posts do resources :post_comments, module: :posts end resources :comments do resources :comment_replies, module: :post_comments #is this module a good choice? end 这给出了以下控制器结构 post_comments GET /posts/:post_id/comments(.:format) posts/comments#index POST /posts/:post_id/comments(.:format) posts/comments#create new_post_comment GET /posts/:post_id/comments/new(.:format) posts/comments#new edit_post_comment GET /posts/:post_id/comments/:id/edit(.:format) posts/comments#edit post_comment GET /posts/:post_id/comments/:id(.:format) posts/comments#show PATCH /posts/:post_id/comments/:id(.:format) […]

在Rails应用程序中绕过application.html.erb

我有一个application.html.erb文件,它为我的应用程序中的每个页面(页眉,页脚等)设置布局,就像典型的Rails应用程序一样。 但是,我想有一个登陆页面,我不想使用这个文件。 我应该如何绕过application.html.erb? 谢谢。

Rails布局名称在视图内

如何在视图中打印出当前布局的名称? 例 puts controller.current_layout 谢谢

在一个动作中渲染不同的视图

我想在我的rails应用程序中为同一post提供两种视图。 例如 – 在登录用户可以更新和编辑post的一个用户中,任何用户都可以只查看它并对其进行评论或选择它。 我该怎么办呢? 我需要一个单独的课吗? 我知道我需要一个单独的视图,但模型和控制器怎么样?

Rails 3:在视图中显示控制器中的变量

我对rails非常陌生,我想知道如何将控制器中的变量显示到我的视图中。 基本上,我有2个文件: articles_controller.rb和_article.html.erb 在我的控制器文件中,我有一个变量@article_votes ,我希望在我的视图中显示。 我尝试过使用但它根本没有显示任何内容。 有人会有任何指导吗? 谢谢!

Rails:通过表单创建新记录时validation失败后的URL

让我们说我正在使用一个表单和一个标准的Rails restful控制器创建一个新的Foo,它看起来像这样: class FoosController ‘Created a foo.’ else render ‘new’ end end … end 所以,如果我使用标准的restful控制器(如上所述),那么当我创建Foo时,我在example.com/foos/new ,如果我提交表单并且它保存正确我在example.com/foos显示索引操作。 但是,如果未正确填充表单,则会再次呈现表单并显示错误消息。 这都是普通的香草。 但是,如果显示错误,将呈现表单页面,但URL将为example.com/foos ,因为CREATE操作会发布到该URL。 但是,人们期望在example.com/foos上找到Foos #index,而不是他们刚刚提交的表单,并添加了错误消息。 这似乎是Rails的标准行为,但它对我来说并没有多大意义。 显然我可以重定向回到new而不是从create动作渲染new,但问题是错误消息等会随着内存中部分完整的Foos而丢失。 这个问题是否有一个干净的解决方案,当他们提交的新Foo表单中存在错误时,可以将人们发送回example.com/foos/new ? 谢谢!

编辑方法正在创建新记录,而不是仅更新现有记录

我的问题是:当我编辑个人资料页面时,如何让我的个人资料控制器的编辑/更新方法停止创建新记录? 我有一个用户模型和配置文件模型。 user has_one :profile 。 profile belongs_to :user 。 我的routes.rb看起来像这样:map.resources:users,:has_one =>:profile 当我的应用程序的访问者创建用户并点击提交时,他们将被定向到“创建个人资料”屏幕。 以下控制器创建一个配置文件,以便创建的配置文件URL为:localhost:3000 / users / [user_id] / profile def new @profile = `current_user.build_profile` end def create @profile = current_user.build_profile(params[:profile]) if @profile.save flash[:notice] = ‘Profile was successfully created.’ redirect_to user_profile_path else flash[:notice] = ‘Error. Something went wrong.’ render “new” end end 一切正常。 编辑配置文件时出现问题。 起初看起来很好。 […]

Rails在视图中切换案例

我想在我的视图中写一个switch case: “one”) %> “two”) %> “three”) %> 但它不起作用。 我是否必须在每行的某处添加 ? 有任何想法吗 ? 谢谢 !