我正在做一个简单的测试项目,为我的测试做好准备.我对嵌套资源相当新,在我的例子中我有一个newsitem,每个newsitem都有注释.
路由如下所示:
resources :comments
resources :newsitems do
resources :comments
end
Run Code Online (Sandbox Code Playgroud)
我正在为评论设置功能测试,但我遇到了一些问题.
这将获得newsitem的注释索引.@newsitem在setupc中声明.
test "should get index" do
get :index,:newsitem_id => @newsitem
assert_response :success
assert_not_nil assigns(:newsitem)
end
Run Code Online (Sandbox Code Playgroud)
但问题出在这里,在"应该变得新".
test "should get new" do
get new_newsitem_comment_path(@newsitem)
assert_response :success
end
Run Code Online (Sandbox Code Playgroud)
我收到以下错误.
ActionController::RoutingError: No route matches {:controller=>"comments", :action=>"/newsitems/1/comments/new"}
Run Code Online (Sandbox Code Playgroud)
但是当我查看路线表时,我看到了这个:
new_newsitem_comment GET /newsitems/:newsitem_id/comments/new(.:format) {:action=>"new", :controller=>"comments"}
Run Code Online (Sandbox Code Playgroud)
我不能使用名称路径或我在这里做错了什么?
提前致谢.
resources nested ruby-on-rails functional-testing nested-resources
在我的Ruby on Rails代码中,我有以下edit.html.erb文件用于任务:
<%= render 'form' %>
Run Code Online (Sandbox Code Playgroud)
然后我在同一目录中有一个_form模板,代码如下:
<%= form_for @task do |f| %>
<%= fl.label :title %><br />
<% end %>
Run Code Online (Sandbox Code Playgroud)
问题是我在尝试导航到编辑页面时遇到错误.错误显示"未定义的task_path",所以我可以告诉Rails没有正确识别我的任务的路径.
程序结构的方式是我有一个包含许多任务的List,每个任务都有一个列表.routes文件以这种方式声明结构:
resources :lists do
resources :tasks
end
Run Code Online (Sandbox Code Playgroud)
如何让form_for识别我正在尝试编辑/ lists /:list_id/tasks /:task_id/edit中的任务?
谢谢您的帮助!
我有一个List对象,具有嵌套的任务.我创建了一个显示单个任务的页面,还有一个允许用户编辑单个任务的页面.我现在想要添加从任务编辑页面上的列表中删除任务的功能.使用以下代码
<%= link_to 'Delete this task',@task, confirm: 'Are you sure?', method: :delete %>
Run Code Online (Sandbox Code Playgroud)
产量
undefined task_path method
Run Code Online (Sandbox Code Playgroud)
这段代码在show.html.erb页面上,我在那里调用@task来显示任务中存储的所有数据,所以我相信这个问题可能是某种路由错误,但我似乎无法想象它出.
相关的控制器方法是
def destroy
@task = Task.find(params[:id])
@task.destroy
respond_to do |format|
format.html { redirect_to list_tasks_path(@task) }
format.json { head :ok }
end
end
Run Code Online (Sandbox Code Playgroud)
我认为使用delete方法我提供的@task只会通过params发送到destroy方法,但是这个错误似乎表明这并不是它的工作原理.那么如何在Rails中正确销毁嵌套资源呢?
编辑: 这是具有嵌套资源的路径文件:
MyApp::Application.routes.draw do
resources :lists do
resources :tasks
end
get "home/index"
root :to => 'home#index'
end
Run Code Online (Sandbox Code Playgroud)
谢谢您的帮助!
正如下面所述的错误消息,我不使用"user_profiles_path"作为复数,因为我在嵌套资源中定义了"resource:profile".
NoMethodError in Profiles#new
Run Code Online (Sandbox Code Playgroud)
显示/home/smileymike/rails_projects/bffmapp_v2/app/views/profiles/new.html.erb其中线#20提出:
undefined method `user_profiles_path' for #<#<Class:0x90266ac>:0xa041294>
Run Code Online (Sandbox Code Playgroud)
模型:
class User < ActiveRecord::Base
has_one :profile
class Profile < ActiveRecord::Base
attr_accessible :name, :surname
belongs_to :user
Run Code Online (Sandbox Code Playgroud)
routes.rb中:
resources :users do
resource :profile (note: has_one)
end
Run Code Online (Sandbox Code Playgroud)
查看:profiles/new.html.erb
<div class="row">
<div class="span6 offset3">
<%= form_for([@user, @profile]) do |f| %>
<%= f.label :name %>
<%= f.text_field :name %>
<%= f.label :surname %>
<%= f.text_field :surname %>
<%= f.submit "Create my profile", class: "btn btn-large btn-primary" %>
<% end %>
</div>
</div>
Run Code Online (Sandbox Code Playgroud)
路线
user_profile …Run Code Online (Sandbox Code Playgroud) controller ruby-on-rails form-for nested-resources ruby-on-rails-3
我通过 LoadLibrary 加载一个模块(exe/dll)并在其中获取一个二进制资源的指针。
微软注意到应该使用三个步骤:
我不明白为什么 MS 设计这个过程如此奇怪?
如果要检测 resource 的长度,必须使用 SizeofResource 和 first step 返回的指针,但不能输入 step2 和 step3 返回的指针。
如果检查这些步骤输出的指针地址,我得到了结果:
谁能解释一下这些函数究竟做了什么?
我在Rails 3应用程序中使用以下路由配置.
# config/routes.rb
MyApp::Application.routes.draw do
resources :products do
get 'statistics', on: :collection, controller: "statistics", action: "index"
end
end
Run Code Online (Sandbox Code Playgroud)
将StatisticController有两个简单的方法:
# app/controllers/statistics_controller.rb
class StatisticsController < ApplicationController
def index
@statistics = Statistic.chronologic
render json: @statistics
end
def latest
@statistic = Statistic.latest
render json: @statistic
end
end
Run Code Online (Sandbox Code Playgroud)
这将生成由/products/statistics成功处理的URL StatisticsController.
如何定义导致以下URL的路由:/products/statistics/latest?
可选:我尝试将工作定义置于关注点但它失败并显示错误消息:
undefined method 'concern' for #<ActionDispatch::Routing::Mapper ...
collections ruby-on-rails separation-of-concerns nested-resources nested-routes
我无法通过构建获得rails 4,强大的参数来处理嵌套资源.任何建议都会非常受欢迎.
RSPEC告诉我
创建动作创建动作失败/错误:click_button"创建动作"NoMethodError:未定义的方法permit' for "create":String
# ./app/controllers/actions_controller.rb:24:inaction_params'#./ app/console/actions_controller.rb:10:in create'
# ./spec/features/creating_actions_spec.rb:16:inblock(2 levels)in'
我的浏览器告诉我:
ActionsController#中的NoMethodError为"create"创建未定义的方法`permit':String
提取的来源(第24行):
def action_params params.require(:action).permit(:title,:description)结束
动作控制器包含:
def create
@action = @project.actions.build(action_params)
if @action.save
flash[:notice] = "Action has been created."
redirect_to [@project, @action]
else
flash[:alert] = "Action has not been created."
render "new"
end
end
private
def action_params
params.require(:action).permit(:title, :description)
end
Run Code Online (Sandbox Code Playgroud)
我正在使用嵌套资源:
resources :projects do
resources :actions
end
Run Code Online (Sandbox Code Playgroud)
表格如下:
<%= form_for [@project, @action] do |f| %>
<% if @action.errors.any? %>
<div id="error_explanation" >
<h2><%= …Run Code Online (Sandbox Code Playgroud) 这是routes.rb:
map.resources :assignments, :shallow => true do |assignment|
assignment.resources :problems
end
Run Code Online (Sandbox Code Playgroud)
如何在代码中获取编辑问题的URL(/ assignments/xyz/problems/abc/edit)?我已经尝试了
edit_assignment_problem_path(赋值,问题)
和edit_problem_path(问题).
虽然第一个适用于我的本地设置,但在服务器上它表示没有定义edit_assignment_problem_path方法.有任何想法吗?
所以在我的rails应用程序中,我有两个属于用户的资源(租赁和预订).这是我的routes.rb中的代码,用于设置嵌套路由.
map.resources :users, :has_many => :reservations, :shallow => true
map.resources :users, :has_many => :rentals, :shallow => true
map.resources :rentals, :only => [:index]
map.resources :reservations, :only => [:index]
Run Code Online (Sandbox Code Playgroud)
是否有更好的方法来做到这一点.我做了一些谷歌搜索,但我找不到一个明确的答案.
提前致谢.
-射线
# routes.rb
resources :assets, only: [:new, :create, :delete]
# asset.rb
class Asset < ActiveRecord::Base
belongs_to :post
end
# rake routes
Prefix Verb URI Pattern Controller#Action
post_comments POST /posts/:post_id/comments(.:format) comments#create
new_post_comment GET /posts/:post_id/comments/new(.:format) comments#new
posts GET /posts(.:format) posts#index
POST /posts(.:format) posts#create
new_post GET /posts/new(.:format) posts#new
edit_post GET /posts/:id/edit(.:format) posts#edit
post GET /posts/:id(.:format) posts#show
PATCH /posts/:id(.:format) posts#update
PUT /posts/:id(.:format) posts#update
DELETE /posts/:id(.:format) posts#destroy
post_form POST /post_form(.:format) posts#form
root GET / posts#index
Run Code Online (Sandbox Code Playgroud)
没有路由显示assets,但我需要能够删除它们而不指定post_id,因为它们可以在没有帖子的情况下存在(为了能够在新帖子中上传文件和接收这些文件所必需的).
路线assets#new和assets#create工作正常,但不是assets#destroy(我得到一个错误说DELETE …
我的建筑内,所有型号的Rails应用程序Users,Album(如相册)和Photo嵌套的顺序.我希望能够找到用户相册使用的总存储空间.
从Album关卡中,我可以找到该相册中照片使用的总存储空间.即:
album = Album.first
album.photos.sum(:image_file_size)
Run Code Online (Sandbox Code Playgroud)
但是,我希望能够从User所有专辑的照片水平上做到这一点.
这样做有一种优雅的方式吗?
谢谢!
sum ruby-on-rails paperclip nested-resources rails-activerecord
nested-resources ×11
ruby ×3
form-for ×2
collections ×1
controller ×1
destroy ×1
nested ×1
paperclip ×1
resources ×1
routes ×1
routing ×1
sum ×1
windows ×1