在我的第一个rails应用程序中,我正在尝试使用form_for并fields_for创建嵌套对象表单.到目前为止一直很好,但我无法弄清楚如何在fields_for块中访问子对象.我已经在子对象中预先填充了一个字段,其中包含我想在用户指令中显示的数据.
车库模型
:
has_many :cars, :dependent => :destroy
accepts_nested_attributes_for :cars
Run Code Online (Sandbox Code Playgroud)
汽车:
belongs_to :garage
Run Code Online (Sandbox Code Playgroud)
车库控制器
def new
@garage = Garage.new
for i in 1..5
@garage.cars.build :stall_number => i
end
end
Run Code Online (Sandbox Code Playgroud)
_form.html.erb
<%= form_for @garage do |f| %>
<%= f.label :title, "Garage Name" %><br />
<%= f.text_field :title %>
<% f.fields_for :cars do |builder| %>
<p>Enter license for car parked in stall: <%= car.stall_number %></p>
<%= f.label :license, "License #:" %><br />
<%= f.text_field :license …Run Code Online (Sandbox Code Playgroud) 我正在IRB中进行一些设计/调试,需要登录用户,然后才能在我的努力中使用current_user.
从Brian Deterling 对另一个问题的回答,我已经能够成功登录并使用以下顺序访问页面响应:
>> ApplicationController.allow_forgery_protection = false
>> app.post('/sign_in', {"user"=>{"login"=>"some-login-id", "password"=>"some-password"}})
>> app.get '/some_other_path_that_only_works_if_logged_in'
>> pp app.response.body
Run Code Online (Sandbox Code Playgroud)
注意:如果您收到200响应,则表示您尚未登录.您需要302重定向才能表示登录成功.看Tim Santeford的回答.
我已经能够得到会话信息:
1.9.3-p125 :009 > app.session
=> {"_csrf_token"=>"1yAn0jI4VWzUH84PNTH0lVhjpY98e9echQGS4=", "session_id"=>"89984667d30d0fec71f2a5cbb9017e24"}
Run Code Online (Sandbox Code Playgroud)
我用尽了一切我能想到的,试图去current_user通过app和app.session,但没有运气.我该怎么current_user办?
在搜索javascript问题的解决方案时,我看到了有关link_to_function在Rails 3中被弃用的多条评论.但是,我已经能够使用完成基于Rails-3的项目的一部分link_to_function.它工作正常.
作为新的Rails,我担心的是我可能会使用长期不支持的东西,或者可能成为传统黑客.在看api.rubyonrails.org,我看link_to_function显然是::的ActionView ::助手模块JavaScriptHelper作为on Rails的v3.0.4一个Ruby支持的公共方法叫出来.没有关于功能寿命的警告或其他声明.
是否有其他方法/方法我应该使用而不是link_to_function在Rails 3中?或者link_to_function可以使用?
谢谢.
在我的index.html.erb文件中,我试图显示我的对象的标题("列表")和正常的"显示","编辑"和"销毁"链接和/或按钮.使用:method =>:delete and:confirm =>"你确定吗?",link_to或button_to都不会显示javascript确认框.这是我的index.html.erb:
<h2><a href="#" onclick="alert('Hello world!'); return false;">Click Here for a Javascript test</a></h2>
<table>
<tr>
<th>List Title</th>
<th></th>
<th></th>
<th></th>
</tr>
<% @listings.each do |listing| %>
<tr>
<td><%= listing.title %></td>
<td><%= button_to 'Show', listing %></td>
<td><%= button_to 'Edit', edit_listing_path(listing) %></td>
<td><%= button_to( 'Destroy', listing,
:confirm => 'Are you sure?', :method => :delete)%> </td>
<td><%= link_to "Destroy", listing, :confirm => "Are you sure?", :method => :delete %> </td>
</tr>
<% end %>
</table>
Run Code Online (Sandbox Code Playgroud)
列表顶部的"Hello World"JS确认链接工作正常,因此我非常确定不引人注目的javascript对我的应用程序运行正常.(我还确认我有必要的--javascript_include_tag:defaults和csrf_meta_tag在我的应用程序模板中.)Rails文档确认支持我正在使用的参数(url,:confirm,:method).但是,button_to或link_to都不会生成由以下指示的JS警报:confirm.另外一点奇怪,使用完全相同的参数,button_to将删除记录而link_to则不会.
任何有关如何调查以解决此问题的建议将不胜感激.
谢谢!
我正在尝试用一个简单的家庭聚会网站:"帖子","家庭","孩子"和"图片".理想情况下,我希望以这种方式构建路由/关系:
resources :posts do
resources :pictures
end
resources :fams do
resources :pictures
resources :kids do
resources :pictures
end
end
Run Code Online (Sandbox Code Playgroud)
在模型中我有必要的" belongs_to"和" has_many"之间设置的关系fams和kids. Fams,kids和posts所有都定义为"has_many :pictures, :as => :imageable"而图片定义为:belongs_to :imageable, :polymorphic => true
在尝试做link_to "Edit"和link_to "Destroy"在pictures视图中我遇到各种各样的_path问题. polymoric_path在两个级别上工作正常,即for posts-pictures和fams-pictures但它无法处理三级情况fams-kids-pictures.我猜它不是为了处理imageable对象上方的两个" "对象而设计的picture.另一个问题是,在一个实例中,pictures控制器必须处理"一级"资源嵌套情况,而在另一个实例中,它必须处理"两级"情况.不知道如何处理这个问题.
我尝试过的一件事就是根据Ruby Guides的指示,不要将资源嵌套到一个以上.我把它们组织成这样:
resources :posts do
resources :pictures
end
resources …Run Code Online (Sandbox Code Playgroud) polymorphic-associations belongs-to nested-routes ruby-on-rails-3
我听说有关ruby 1.9.3p125有解决ruby-debug19问题的传言,所以根据RVM网站上的说明,我重新安装了1.9.3:
$ rvm reinstall 1.9.3 --patch debug --force-autoconf
$ ruby -v
ruby 1.9.3p125 (2012-02-16 revision 34643) [x86_64-darwin11.2.0]
Run Code Online (Sandbox Code Playgroud)
然后:
gem install ruby-debug19
Run Code Online (Sandbox Code Playgroud)
在我的Gemfile中添加了这个条目:
gem 'ruby-debug19'
Run Code Online (Sandbox Code Playgroud)
然后:
$ rails server -u
=> Booting WEBrick
=> Rails 3.1.3 application starting in development on http://0.0.0.0:3000
=> Call with -d to detach
=> Ctrl-C to shutdown server
You need to install ruby-debug to run the server in debugging mode. With gems, use 'gem install ruby-debug'
Exiting
Run Code Online (Sandbox Code Playgroud)
为了解决这个错误,我将我的Gemfile条目更改为:
gem 'ruby-debug19', :require => …Run Code Online (Sandbox Code Playgroud) 在多个视图中,我包含一个带有此HTML的外部JavaScript库:
<script type="text/javascript"
src="http://static.jstree.com/v.1.0pre/jquery.jstree.js">
</script>
Run Code Online (Sandbox Code Playgroud)
在阅读Rails 3.1 Asset Pipeline文档和讨论时,我得到的印象vendor/assets/javascripts是该文件应该被引用.我猜我可以下载文件(jstree.js)的副本并将其放在该目录中.但是,我想将它从项目站点加载,而不是制作它的本地副本.
我将什么放在vendor/assets/javascripts中从远程服务器上提取jstree.js的副本?我是否使用某种远程加载代码创建.js文件?似乎有各种各样的方法和/或混淆如何最好地做到这一点(请参阅这个问题的答案的长列表: 如何在另一个JavaScript文件中包含JavaScript文件?)
这样做是否有"Rails标准"惯例/库/流程?我是一个javascript新手,所以请明确,谢谢.
我正在尝试创建一个"标记"功能,允许用户"标记"他们感兴趣的项目.这是我的模特
class tag
belongs_to :user
belongs_to :item
end
Run Code Online (Sandbox Code Playgroud)
相应的DB表具有必要的:user_id和:item_id字段.
在列表中:items我想要一个:item允许用户标记的链接:item.由于我知道:user_id和:item_id,我想创建一个新:tag记录,设置id字段,并保存记录 - 所有这些都没有用户干预.我尝试了以下调用link_to,但没有记录保存在数据库中:
<%= link_to 'Tag it!', {:controller => "tracks",
:method => :post,
:action => "create"},
:user_id => current_user.id,
:item_id => item.id %>
Run Code Online (Sandbox Code Playgroud)
(此代码位于: @item.each do |item| 语句中,因此item.id有效.)
此link_to调用创建此URL:
http://localhost:3000/tags?method=post&tag_id=7&user_id=1
Run Code Online (Sandbox Code Playgroud)
哪个不会Tag在数据库中创建记录.这是我的:create行动tags_controller
def create
@tag = Tag.new
@tag.user_id = params[:user_id]
@tag.tag_id = params[:tag_id]
@tag.save
end
Run Code Online (Sandbox Code Playgroud)
如何获取link_to来创建和保存新的标签记录?
我试图application.html.erb通过将部分布局移动到局部来清理.我有以下代码来处理闪存错误/通知:
<div id="flash">
<% if flash[:notice] %>
<h3 class="info_box"><%= flash[:notice] %></h3>
<% end %>
<% if flash[:error] %>
<h3 class="error_box"><%= flash[:error] %></h3>
<% end %>
</div>
Run Code Online (Sandbox Code Playgroud)
这段代码工作得很好application.html.erb,直到我把它移到一个名为" _flash.html.erb" 的文件中并用以下内容替换它:
<%= render 'layouts/flash' %>
Run Code Online (Sandbox Code Playgroud)
在部分中,闪存哈希不是一个可识别的对象,并导致"当你没想到它时,你有一个零对象!" 错误.
我已将代码移回application.html.erb,一切都很好.但我无法找到部分访问闪存哈希的答案.看看Rails指南中的"渲染和布局"我可以看到有多种方法render()可以将变量传递给部分,但是我没有成功地搞清楚它.有任何想法吗?
javascript ×3
belongs-to ×2
link-to ×2
devise ×1
forms ×1
irb ×1
nested-forms ×1
partial ×1
ruby ×1
ruby-1.9.3 ×1
ruby-debug ×1
session ×1
views ×1