假设我有一个名为Thing的Rails模型.Thing有一个url属性,可以选择将其设置为Internet上的某个URL.在视图代码中,我需要执行以下操作的逻辑:
<% if thing.url.blank? %>
<%= link_to('Text', thing_path(thing)) %>
<% else %>
<%= link_to('Text', thing.url) %>
<% end %>
Run Code Online (Sandbox Code Playgroud)
视图中的这种条件逻辑很难看.当然,我可以构建一个辅助函数,它会将视图更改为:
<%= thing_link('Text', thing) %>
Run Code Online (Sandbox Code Playgroud)
这解决了冗长问题,但我真的更喜欢模型本身的功能.在这种情况下,视图代码将是:
<%= link_to('Text', thing.link) %>
Run Code Online (Sandbox Code Playgroud)
显然,这需要模型上的链接方法.这是它需要包含的内容:
def link
(self.url.blank?) ? thing_path(self) : self.url
end
Run Code Online (Sandbox Code Playgroud)
就问题而言,thing_path()是Model代码中未定义的方法.我假设可以将一些辅助方法"拉入"模型中,但是如何?是否有一个真正的原因,路由只在控制器上运行并查看应用程序层?我可以想到许多模型代码可能需要处理URL(与外部系统集成等)的情况.
我希望我的网址使用破折号-而不是下划线_作为单词分隔符.例如,controller/my-action而不是controller/my_action.
我对两件事感到惊讶:
-到_路由.或者是吗?我将使用的最佳解决方案:as或命名路线.
我的想法是修改Rails路由以检查该全局配置并在分派到控制器操作之前更改-为_.
有没有更好的办法?
我在路径文件中添加了:
map.show_book "/show_book/:name/year/:year", :controller => "book", :action => "show_version"
Run Code Online (Sandbox Code Playgroud)
我还补充说:
map.show_book "/show_book/:name", :controller => "book", :action => "show_version"
Run Code Online (Sandbox Code Playgroud)
显示最新的书而不指定年份.
但它不起作用,如果我没有过年,它就无法在"show_book/NAME"中找到路线.
你有一些想法为什么它不起作用?
谢谢 !
PS.我知道我可以使用year作为"?year = XXXX"的参数,但我想将年份用作URL的一部分
我正在使用Rails 4创建一组服务,我正在使用JavaScript浏览器应用程序.跨源GETS工作正常,但我的POST未通过预检OPTIONS检查404错误.至少,我认为这是正在发生的事情.以下是控制台中出现的错误.这是Mac上的Chrome 31.0.1650.63.
OPTIONS http://localhost:3000/confessor_requests 404 (Not Found) jquery-1.10.2.js:8706
OPTIONS http://localhost:3000/confessor_requests No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'http://localhost' is therefore not allowed access. jquery-1.10.2.js:8706
XMLHttpRequest cannot load http://localhost:3000/confessor_requests. No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'http://localhost' is therefore not allowed access. main.html:1
Run Code Online (Sandbox Code Playgroud)
我已经搜索了关于启用CORS的指令的高低,我很难过.通常的建议似乎是将这样的东西放在Application控制器中,我做了.
before_filter :cors_preflight_check
after_filter :cors_set_access_control_headers
def cors_set_access_control_headers
headers['Access-Control-Allow-Origin'] = '*'
headers['Access-Control-Allow-Methods'] = 'POST, PUT, GET, OPTIONS'
headers['Access-Control-Allow-Headers'] = '*'
headers['Access-Control-Max-Age'] = "1728000"
end
def cors_preflight_check
if request.method == :options
headers['Access-Control-Allow-Origin'] = …Run Code Online (Sandbox Code Playgroud) xmlhttprequest rails-routing http-method cors ruby-on-rails-4
我是Ruby on Rails的新手,我已经完成了博客教程.
我现在正在尝试向控制器添加一个名为"start"的附加操作.
def start
end
Run Code Online (Sandbox Code Playgroud)
我添加了一个视图页面"app/views/posts/start.html.erb",只包含简单的html.
当我去/ posts/start时,我得到以下错误.
ActiveRecord::RecordNotFound in PostsController#show
Couldn't find Post with ID=start
Run Code Online (Sandbox Code Playgroud)
我理解错误,正在执行show动作并且start不是有效的ID.为什么启动操作没有执行,是否存在我缺少的MVC架构或配置的某些部分?
下面是我的posts_controller.rb
class PostsController < ApplicationController
# GET /posts/start
def start
end
# GET /posts
# GET /posts.xml
def index
@posts = Post.find(:all)
respond_to do |format|
format.html # index.html.erb
format.xml { render :xml => @posts }
end
end
# GET /posts/1
# GET /posts/1.xml
def show
@post = Post.find(params[:id])
respond_to do |format|
format.html # show.html.erb
format.xml { render :xml => …Run Code Online (Sandbox Code Playgroud) 我有一个invoices_controller有资源路线.如下:
resources :invoices do
resources :items, only: [:create, :destroy, :update]
end
Run Code Online (Sandbox Code Playgroud)
现在我想在发票中添加一个发送功能,如何添加一个自定义路由作为invoices/:id/send调度请求的说明invoices#send_invoice以及如何在视图中链接到它.
什么是传统的轨道方式来做到这一点.谢谢.
在我的应用程序中,我有一些API在api域下.现在在其中一个API中我想生成一个指向主域的URL,比方说
test.com/blabla...
Run Code Online (Sandbox Code Playgroud)
我试图使用 url_for,但似乎默认的root_url或request.host在api域中.Url_for将成为现实
api.test.com/blabla..
Run Code Online (Sandbox Code Playgroud)
虽然我想要它
test.com/blabla...
Run Code Online (Sandbox Code Playgroud)
Url_for可以带参数
host: ...
Run Code Online (Sandbox Code Playgroud)
将它设置为test.com/,问题是如何获取主机的root/base url(test.com)? root_url或request.host都是api.test.com.
有任何想法吗?谢谢.
我无法在Rails 3.2.12中解决这个问题,也许我错过了一些东西.
配置/ routes.rb中
get "home/index"
root :to => "home#index"
devise_for :users, :only => :omniauth_callbacks
match 'users/auth/:provider/callback' => 'authentications#create'
match '/auth/:provider/signout' => 'authentications#signout'
Run Code Online (Sandbox Code Playgroud)
应用程序/控制器/ authentication_controller.rb
class AuthenticationsController < ApplicationController
...
end
Run Code Online (Sandbox Code Playgroud)
应用程序/模型/ authentication.rb
class Authentication < ActiveRecord::Base
...
end
Run Code Online (Sandbox Code Playgroud)
我认为它应该符合我目前的知识,但有些东西我想念.
我的问题是告诉我有什么问题.
计数错误
uninitialized constant AuthenticationsController
这是一条显示在的消息 http://localhost:3000/auth/facebook/signout
在Rails的路由指南没有明确规定什么:on => :collection意思.
我无法找到:on关键是什么的解释,也不能解释:collection那个背景中的含义.
我有一个Rails 3应用程序,其中包含多个包含附加功能的引擎.每个引擎都是一个单独的服务,客户可以购买.
但是,我对来自引擎的路径存在问题,这些路由不容易用于控制器和视图.
控制器:
class ClassroomsController < ApplicationController
..
respond_to :html
def index
respond_with(@classrooms = @company.classrooms.all)
end
def new
respond_with(@classroom = @company.classrooms.build)
end
..
end
Run Code Online (Sandbox Code Playgroud)
app/views/classrooms/new.html.haml:
= form_for @classroom do |f|
..
f.submit
Run Code Online (Sandbox Code Playgroud)
config/routes.rb 在引擎中:
MyEngineName::Engine.routes.draw do
resources :classrooms
end
Run Code Online (Sandbox Code Playgroud)
config/routes.rb 在app中:
Seabed::Application.routes.draw do
mount MyEngineName::Engine => '/engine'
...
end
Run Code Online (Sandbox Code Playgroud)
lib/my_engine_name.rb 在引擎中:
module MyEngineName
class Engine < ::Rails::Engine
end
end
Run Code Online (Sandbox Code Playgroud)
试图进入/classrooms/new结果
NoMethodError in Classrooms#new
Showing app/views/classrooms/_form.html.haml where line #1 raised:
undefined method `hash_for_classrooms_path' for #<Module:0x00000104cff0f8>
Run Code Online (Sandbox Code Playgroud)
并尝试classrooms_path …
ruby-on-rails url-routing rails-routing rails-engines ruby-on-rails-3