在过去的两个月里,我一直在搞乱铁轨,到目前为止一切进展顺利 - 但有一个方面我有点怀疑.
我一直听说RESTful rails资源的乐趣:即config/routes中的'resource:foo',以及控制器中的7个restful操作.
除了非常简单的事情(例如通过运行'生成脚手架'来完成99%的事情),我发现尝试将项目功能压缩到该方法中比仅仅逐个匹配配置/路由中的URL更方便每个动作都需要.
但我一直认为我错了,除了最极端的情况之外,RESTful资源才是最佳选择.
所以:
(a)任何人都可以对此提出意见吗?
(b)对于经验丰富的铁路人员,典型项目中您的路线的百分比是:资源和行动编码的百分比是多少?干杯...
我正在使用Rails 3.2
我希望路由几乎和github一样,所以:
root/(username)
root/(username)/(projectname)
root/(username)/(projectname)/issus
Run Code Online (Sandbox Code Playgroud)
等等
我正在尝试这样的事情:
resources :publishers do
resources :magazines do
resources :photos
end
end
Run Code Online (Sandbox Code Playgroud)
但是这给出了这样的路线:
/publishers/1/magazines/2/photos/3
Run Code Online (Sandbox Code Playgroud)
我正在看的一个项目做了以下似乎有效,但似乎不适合我.
resources :projects, :constraints => { :id => /[^\/]+/ }, :except => [:new, :create, :index], :path => "/" do
member do
get "team"
get "wall"
get "graph"
get "files"
end
resources :wikis, :only => [:show, :edit, :destroy, :create] do
member do
get "history"
end
end
Run Code Online (Sandbox Code Playgroud) ruby-on-rails rails-routing ruby-on-rails-3 ruby-on-rails-3.2
我目前正在使用Ruby on Rails 3.2.8并在config/routes.rb中进行此重定向:
root :to => redirect("/home/index.html")
Run Code Online (Sandbox Code Playgroud)
适用于在开发中将http:// localhost:3000 /重定向到http:// localhost:3000/home/index.html.但是在我的测试环境中,我正在使用代理和子路径,在config/environments/test.rb中设置relative_url_root,如下所示:
config.action_controller.relative_url_root = '/testpath'
Run Code Online (Sandbox Code Playgroud)
所以我希望从http://testdomain.com/testpath重定向到http://testdomain.com/testpath/home/index.html,而是重定向到http://testdomain.com/home/index.html.
如果设置了,我如何更改我的重定向语句以使用relative_url_root?
我试图用概括的方法,这个帖子与结合url_for,以确定当前的路径是在安装的发动机,但我有一个很难搞清楚究竟如何使用Journey::Path::Pattern(这是由返回的mounted_path概括方法在另一篇文章中).
class Rails::Engine
def self.mounted_path
route = Rails.application.routes.routes.detect do |route|
route.app == self
end
route && route.path
end
end
Run Code Online (Sandbox Code Playgroud)
除了官方文档之外,在任何地方似乎都没有太多的讨论,这不是特别有用.我确定解决方案相对简单,我正在尝试编写的辅助方法的要点如下:
def in_engine? engine
current_url.include?(engine.mounted_path)
end
Run Code Online (Sandbox Code Playgroud)
编辑:
我的一些引擎作为子域安装,一些安装在应用程序本身内,这使我无法简单地检查当前子域是否与安装的路径相同,或者使用path_for.
我有一个 Rails 4.0 应用程序,它允许用户通过子域访问博客。我的路线目前是这样的:
match '', to: 'blogs#show', via: [:get, :post], constraints: lambda { |r| r.subdomain.present? && r.subdomain != 'www' }
resources :foobars
Run Code Online (Sandbox Code Playgroud)
现在,当我导航到somesubdomain.example.com我确实像预期的那样show采取blogs控制器动作的动作时。
当我导航到时,example.com/foobars我可以按预期访问控制器的index操作foobars。
但是,我只得到了一个我不想要的行为:当我导航到 时somesubdomain.example.com/foobars,我仍然可以访问控制器的index操作foobars。
有没有办法限制或排除我没有特别允许用于特定子域的所有资源(即,somesubdomain.example.com/foobars除非另有说明,否则将不起作用)。
谢谢!
我是使用基础的新手,我使用脚手架创建了简单的帖子应用程序,我已经完成了以下步骤:
rails new blog
Run Code Online (Sandbox Code Playgroud)
然后在Gemfile中添加了以下内容
gem 'foundation-rails'
group :development do
gem 'rails_layout'
end
Run Code Online (Sandbox Code Playgroud)
然后
$ bundle install
$ rails generate layout:install foundation5 --force
$ rails g scaffold Post title desc:text
$ rake db:migrate
Run Code Online (Sandbox Code Playgroud)
现在app运行良好@本地主机端口3000 /帖子
但是当我点击导航栏中的"主页"按钮时,它会产生错误:

application.html.erb文件:
<!DOCTYPE html>
<html>
<head>
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title><%= content_for?(:title) ? yield(:title) : "Found Rails" %></title>
<meta name="description" content="<%= content_for?(:description) ? yield(:description) : "Found Rails" %>">
<%= stylesheet_link_tag 'application', media: 'all', 'data-turbolinks-track' => true %>
<%# Modernizr is required for Zurb Foundation %> …Run Code Online (Sandbox Code Playgroud) 鉴于我在routes.rb中定义了以下路由
get "signup" => "users#new"
post "signup" => "users#create"
Run Code Online (Sandbox Code Playgroud)
我的erb视图中的以下条件
<%if current_page?(login_path) or current_page?(signup_path)%>
<p> NOW YOU'RE IN</p>
<% end %>
Run Code Online (Sandbox Code Playgroud)
如何让current_page识别GET和POST路径?现在无论何时用户#create,p标签都不显示.我假设它是因为路由定义/注册为用户获取#new,而post完全是其他东西.
我尝试过以下内容,但似乎没有效果
<%if current_page?(login_path) or current_page?(signup_path) or current_page?(controller: "users", action: "create")%>
<p> NOW YOU'RE IN</p>
<% end %>
Run Code Online (Sandbox Code Playgroud)
编辑:我有帖子的原因是因为如果注册表单有错误,它会重定向回注册页面,但这次由于某种原因,该操作不再是新的,而是创建
这里是新的Web开发人员,我想我可能会遗漏一些非常基础的知识.鉴于代码
def create
@post = Post.new(post_params)
if @post.save
redirect_to @post
else
render "new"
end
end
Run Code Online (Sandbox Code Playgroud)
为什么视图模板重定向到def show动作?如果我没有定义def show及其相应的视图,rails会给我一个错误.
我只是不明白为什么即使在我保存帖子后代码是redirect_to @post,它似乎在创建帖子后重定向到显示页面.这只是其中之一,我应该把它当作它,或者我错过了一些基本的HTML协议知识(我真的不知道很多)?
编辑:为了进一步澄清我的问题,我看到@post已经在create方法中定义,并被定义为Post.new(post_params).当我redirect_to @post时,它不会再简单地调用那行吗?
我知道有关重定向特定路由的信息:
put 'users/:user_id', to: redirect('/api/v1/users/:user_id')
Run Code Online (Sandbox Code Playgroud)
如何将重定向应用于由生成的所有路由resources?寻找类似的东西
resources :users, to: redirect('/api/v1')
Run Code Online (Sandbox Code Playgroud)
我可以使用match实现一种解决方法,但这有点笨拙:
match 'users/*path', to: redirect('/api/v1/users/%{path}'), via: [:GET, :POST, :PUT, :DELETE]
Run Code Online (Sandbox Code Playgroud) 我有用于控制用户任务的Rails 5 API项目,我有以下错误,但并不总是对于相同的控制器和路由.
ActionController::RoutingError: uninitialized constant Api::V1::ApiController
Run Code Online (Sandbox Code Playgroud)
我向您描述了一些我的项目,以更详细地解释错误.
应用结构
路线
scope module: 'api' do
namespace :v1 do
# => Login routes
scope module: 'login' do
match 'login', to: 'sessions#login', as: 'login', via: :post
end
# => Team routes
scope module: 'team' do
# => no admin routes
resources :tasks, except: [:index] do
collection do
match ':view', to: 'tasks#index', as: 'tasks', via: [:get, :post]
end
end
end
end
end
Run Code Online (Sandbox Code Playgroud)
API控制器
module Api
class ApiController < ApplicationController
def respond_with_errors(object)
render json: {errors: …Run Code Online (Sandbox Code Playgroud)