我认为这是一个艰难的问题.我有一个评论控制器,我想用于我的所有其他控制器:如书籍,标题等.
问题是,评论中的创建操作是:
def create
@book = Book.find(params[:book_id])
@comment = @book.comments.create!(params[:comment])
respond_to do |format|
format.html {redirect_to @book}
format.js
end
Run Code Online (Sandbox Code Playgroud)
结束
那么,如果它明显使用书籍属性,我如何使用动作和评论控制器作为标题?
我正在使用的gem插入一个我想删除的after_save回调.在我看来,从数组中删除符号比使用monkeypatch修复问题更清晰.如何访问回调数组?
可能重复:
Grails - 从控制器获取消息值
我想在类中使用g.message()方法Service,而不是Controller.但我不能.
代码示例:
// this is a method OUTSIDE a Controller
def show = {
def msg = message(code:"foo.bar") // I'm getting error here
}
Run Code Online (Sandbox Code Playgroud)
有可能这样做,或者我必须使用旧ResourceBundle班级?
所以我有一条看起来像这样的路线:
scope "4" do
scope "public" do
scope ":apikey" do
resources :shops
end
end
end
Run Code Online (Sandbox Code Playgroud)
还有一堆控制器规格,其中一个例子如下:
describe ShopsController do
describe "when responding to a GET" do
context "#new" do
it "should create a new instance of the shop class" do
get :new
@shop.should_not_be_nil
end
end
end
end
Run Code Online (Sandbox Code Playgroud)
在rake路由中,以及通过Web浏览器,此控制器/操作正常工作.然而,RSpec抛出:
1) ShopsController when responding to a GET#new should create a new instance of the shop class
Failure/Error: get :new
ActionController::RoutingError:
No route matches {:controller=>"shops", :action=>"new"}
# ./spec/controllers/shops_controller_spec.rb:9:in `block (4 levels) in <top …Run Code Online (Sandbox Code Playgroud) 我想从控制器调用另一个控制器中的函数.
class FirstController extends Controller {
public function test1() { return 'OK'; }
}
class SecondController extends Controller {
public function callTest1() { First::test1(); }
}
Run Code Online (Sandbox Code Playgroud)
- >服务器错误
帮我解决它.
我在我的控制器中有bellow登录功能,我试图将登录日期/时间保存到字段last_login下的用户数据库表中,该字段是DATETIME字段,登录工作正常,但该字段永远不会被填充..任何关于什么的想法停止这个或如何解决应该输出什么?
public function login() {
$this->layout = 'login';
if ($this->request->is('post')) {
if ($this->Auth->login()) {
$this->User->id = $this->Auth->user('id'); // target correct record
$this->User->saveField('last_login', date(DATE_ATOM)); // save login time
$this->redirect($this->Auth->redirect());
} else {
$this->Session->setFlash('Invalid username or password, try again', 'default', array('class' => 'warning'));
}
}
Run Code Online (Sandbox Code Playgroud) 在我的people_controller_spec.rb中我有
before(:each) do
@office = FactoryGirl.create(:office)
@organization = FactoryGirl.create(:organization)
@user = FactoryGirl.create(:user, organization: @organization)
@request.session['user_id'] = @user.id
@request.session['current_organization_id'] = @user.organization.id
end
Run Code Online (Sandbox Code Playgroud)
我有这个application_controller.rb
class ApplicationController < ActionController::Base
protect_from_forgery
private
def current_user
@current_user ||= User.find(session[:user_id]) if session[:user_id]
end
def current_organization
if session[:current_organization_id]
Organization.find(session[:current_organization_id])
else
@current_organization ||= Organization.find(current_user.organization_id)
end
end
helper_method :current_user
helper_method :current_organization
end
Run Code Online (Sandbox Code Playgroud)
会话哈希似乎没有在application_controller.rb中持久存在,这就是为什么我得到这些类型的测试错误,其中application_controller.rb中的@current_user是nil
6) PeopleController index sorts all people alphabetically by first_name
Failure/Error: get :index, {search: {meta_sort: "first_name.asc"}}, valid_session
NoMethodError:
undefined method `organization_id' for nil:NilClass
# ./app/controllers/application_controller.rb:15:in `current_organization'
# ./app/controllers/people_controller.rb:107:in …Run Code Online (Sandbox Code Playgroud) 我想cms/index/noRoute从我的一个自定义模块的控制器操作中调用Action,我该怎么做?
我试过了,
$this->_redirectUrl('cms/index/noRoute')
Run Code Online (Sandbox Code Playgroud)
和
$this->_forward('cms/index/noRoute')
Run Code Online (Sandbox Code Playgroud)
重定向网址的变化也很少,例如"*/cms/index/noRoute"等,其中没有变化.我该怎么做?
我试图根据参数在我的控制器中插入一个标题sub,但没有成功.我试过这两行,这里是代码:
public ActionResult Index(string sub)
{
if (!string.IsNullOrEmpty(sub))
{
HttpContext.Response.Headers.Add("sub", sub);
Response.AddHeader("sub", sub);
}
return View();
}
Run Code Online (Sandbox Code Playgroud)