其目的是创建以下两个urls:
其中“post”是资源,而“widget”不是(它只是客户端想要的URL )
我希望这两条路都去同一个控制器。
我在routes.rb中有这样的设置:
namespace :widgets do
get :edit
end
resources :posts do
namespace :widgets do
get :edit, to: "widgets#edit"
end
end
一个快速的rake路由/grep显示这些路由到两个独立的控制器:
rake routes | grep wi
我正在尝试创建简单的Ruby on Rails REST API。
应用程序/控制器/api/vi/product_controller er.rb
module Api
module V1
class ProductController < ApplicationController::API
def index
render json: {message: 'Welcome!'}
end
end
end
end
config/routes.r
Rails 5.0.0.beta 4在包含动态:action和:控制器段的路由上引入了一个弃用警告:
DEPRECATION WARNING: Using a dynamic :action segment in a route is deprecated and will be removed in Rails 5.1.
声明:
允许:控制器和:动作值将通过config/routeres.rb中的路径指定,这是Rails中导致安全发布的许多问题的根本原因。鉴于此,更好的做法是,控制器和行动是明确的白名单,而不是试图列入黑名单或消毒‘坏’的价值观。
您将如何“白名单”一组动作参数?我
当我从浏览器访问我的页面(控制器操作)时,页面呈现得很好。当我尝试从控制器规范访问页面时,它失败了。
浏览器工作页面的日志信息:
Started GET "/listings/2" for ::1 at 2017-12-30 17:10:13 -0500
Processing by Web::ListingsController#show as HTML
Parameters: {"id"=>"2"}
Listing Load (0.3ms) SELECT "listings".* FROM "listi
我希望根据控制器中定义的操作“自动”生成路由。资源路由(据我所知)只会自动为http谓词生成路由和助手。在本例中,我使用rails提供大多数静态页面,并且不需要在控制器中使用http谓词。
具体地说:
在控制器中,我定义了引用那些主要是静态页面的操作。
def action
end
在routes文件中,我有一堆
match "/url" => 'controller#action'
我希望所有这些匹配的路由都能根据控制器中的操作自动生成。从概念上讲,类似于:
for actions in controller.erb do |action|
在学习Rails的过程中,我创建了一个应用程序,其中一个Domains控制器嵌套在一个Customers控制器之下。我使用的是Rails 2.3.4,这是一次学习经历。我成功地设置了以下路由:
customer_domains GET /customers/:customer_id/domains(.:format) {:controller=>"domains", :action=>"index"}
POST /customers/:customer_id/domains
我正在做一个rails任务,它要求我创建一个项目列表。我为项目创建了控制器和模型,但仍然有一些问题。我一直收到以下错误:
undefined method `items_path' for
以下是我的一些代码:
class ItemsController < ApplicationController
def new
@item = Item.new
end
def create
@item = Item.new(params.require(:item).permit(:name))
if @item.save
flash
在我的rails 3应用程序中使用我的路线时遇到了困难,我想要像这样的浅路线:
/san-francisco/union-square
但是我的路由器坚持这样做:
/cities/san-francisco/neighborhoods/union-square
我已经在我的routes.rb中使用了这个
shallow do
resources :cities do
resources :neighborhoods do
resources :locations
end
end
end
但我仍然有这样的想法:
city_neighborhood_locati
我正面临一个关于参数的问题。为了在我的应用程序中实现注释,我遵循rails指南,但当我试图发布评论时,我会收到错误:
ActionController::ParameterMissing at /posts param is missing or the value is empty: post
这是控制器中的代码,它将引发错误。
def post_params
params.require(:post).permit(:title, :text).tap do |post_params|
post_params.require(:title, :text)
e
我是rails的新手,我很难理解rails的原理,也很难找到“合适”的方法来创建一个对象作为另一个对象的依赖项。
我有一个Backlog模型:
class Backlog < ActiveRecord::Base
has_many :user_stories
end
和一个UserStory模型:
class UserStory < ActiveRecord::Base
belongs_to :backlog
end
在Backlog控制器的show操作中,我希望显示一个链接,用于创建属于当前Backlog的UserStory。我希望它重定向到UserStory创建表单。
在rails 2.x中,我使用了浅层路由,但是rails 3(至少在API 中)缺少了这一点。
当我在rails 3中传递这个选项时,它不会抛出任何错误,但我也没有得到我所期望的所有路由。
Rails 3 routes.rb
resources :users, :shallow=>true do
resources :recipe do
resources :categories do
resources :sections do
resources :details do
end
end
这是我的route.rb文件:
Rails.application.routes.draw do
resources :invoices
devise_for :users
root to: 'pages#home'
resources :users do
resources :properties do
resources :invoices
end
end
end
虽然在创建新属性时,属性控制器中的Create方法没有问题:
class PropertiesController < ApplicationController
b
使用rails url助手,我将如何链接到home_Controller.rb的联系人操作?
如何生成链接如何获得路径?如何在操作中进行重定向?
谢谢你澄清这件事!
到目前为止,我的家庭控制器看起来是:
resources :home do
collection do
get 'about'
get 'contact'
end
end
rails在控制器中定义了7个参数,
如果我想要更多的参数,我可以添加变量到控制器,或者我应该创建一个新的控制器吗?
如果我必须创建另一个控制器,我试图通过以下方法来定义它:
创建了控制器/taskseachPer.rb,其中包含:
class TaskseachpersonController < ApplicationController
def index
end
end
助手/taskseachPerson.rb包含:
module TaskseachpersonHelper
end
views/taskseachperson/index.html.erb包含:
&l
在rails 4.2中,我有一个控制器,我想用非资源路由填充它,如下所示
class TwilioController < ApplicationController
def add_to_queue
end
def another_action
end
end
然后,我想像这样访问这些操作
http://appdomain/twilio/add-to-queue和http://appdomain.com/twilio/another-action
我意识到我可以在routes文件中这样做
get 'twilio/add-to-queue', to:
我的Rails 5.1API应用程序代码回购Dish和Comment模型如下所示。我需要帮助将一个新的Comment添加到Dish中。
Post
class Dish < ApplicationRecord
has_many :comments
end
注释
class Comment < ApplicationRecord
belongs_to :dish
end
后序列化器(使用ActiveModel序列化程序)
class DishSerializer < ActiveModel::Serializer
attributes :id, :name,