有谁知道我为什么得到
undefined method `my_method' for #<MyController:0x1043a7410>
Run Code Online (Sandbox Code Playgroud)
当我从ApplicationController子类中调用my_method("string")时?我的控制器看起来像
class MyController < ApplicationController
def show
@value = my_method(params[:string])
end
end
Run Code Online (Sandbox Code Playgroud)
和我的帮手
module ApplicationHelper
def my_method(string)
return string
end
end
Run Code Online (Sandbox Code Playgroud)
最后,ApplicationController
class ApplicationController < ActionController::Base
after_filter :set_content_type
helper :all
helper_method :current_user_session, :current_user
filter_parameter_logging :password
protect_from_forgery # See ActionController::RequestForgeryProtection for details
Run Code Online (Sandbox Code Playgroud) 我需要在我的ApplicationController中设置cookie,但我不确定如何.我尝试过使用cookies - 没什么,使用ActionController :: Cookies - 没什么.我不需要设置和获取cookie,但我需要的是在ApplicationController中设置它们.
编辑:
找到答案: request.cookies['help'] = 'yes'
我想我会想出一个灵活的方法来扩展Rails 3.x gem中的ApplicationController.
在我的宝石中lib/my_namespace/my_controller.rb
,我有:
class MyNamespace::MyController < ApplicationController
before_filter :some_method
after_filter :another_method
def initialize
# getting classname of the subclass to use for lookup of the associated model, etc.
# and storing the model_class in an instance variable
# ...
end
# define :some_method, :another_method, etc.
# ...
private
attr_accessor :subclass_defined_during_initialize # etc.
# etc.
end
Run Code Online (Sandbox Code Playgroud)
但是当加载Gem时,app/controllers/application_controller.rb
尚未加载,因此失败:
/path/to/rvm/gemset/gems/activesupport-3.2.6/lib/active_support/dependencies.rb:251:
in `require': cannot load such file -- my_gem_name/application_controller (LoadError)
Run Code Online (Sandbox Code Playgroud)
作为一种解决方法,我在我的gem中定义了ApplicationController lib/gem_namespace/application_controller.rb
:
class ApplicationController < ActionController::Base
end
Run Code Online (Sandbox Code Playgroud)
我假设即使我在那里定义它,它也会在我的Rails …
gem ruby-on-rails applicationcontroller ruby-on-rails-3 load-order
在config/application_controller.rb
我的Rails应用程序目录中的文件中,我找到了以下代码:
class ApplicationController < ActionController::Base
protect_from_forgery
end
Run Code Online (Sandbox Code Playgroud)
任何人都能告诉我project_from_forgery
它的用法和原因是什么?
我正在使用gem rails3-jquery-autocomplete并且没有任何问题,但是我现在已经将自动完成表单移动到应用程序模板中,因此ajax调用现在由应用程序控制器处理,因此我的路由已从以下更改:
home/autocomplete_category_name
Run Code Online (Sandbox Code Playgroud)
现在需要删除主页并从以下路径:
home_autocomplete_category_name_path
Run Code Online (Sandbox Code Playgroud)
至:
autocomplete_category_name_path
Run Code Online (Sandbox Code Playgroud)
有人有任何想法吗?还在学习Rails的来龙去脉,所以现在这对我来说是个死胡同.
谢谢.
我正在升级我的Rails插件,使其成为最新3.0RC1版本的引擎,我在找出最佳(也是最正确的)扩展方法时遇到了一些麻烦ActionController
.我在DHH 看过这篇文章,这个问题在这里,但我的问题更多是关于如何正确调用代码ActionController
.
例如,我需要在我的引擎控制器中调用以下内容:
class ApplicationController < ActionController::Base
helper :all
before_filter :require_one_user
after_filter :store_location
private
def require_one_user
# Code goes here
end
def store_location
# Code goes here
end
end
Run Code Online (Sandbox Code Playgroud)
我知道如何正确地包含我的两个私有函数,但我找不到让它正确调用的方法helper
,before_filter
并且after_filter
.
我非常感谢一些链接或一种方法来使这项工作.我已经尝试过命名我的控制器以外的东西ApplicationController
并且真正ApplicationController
扩展它,但这似乎也不起作用.我真的很想找到能让发动机用户的生活变得尽可能简单的任何解决方案.理想情况下,他们不必扩展我的课程,但他们拥有自己的所有功能ApplicationController
.
ruby-on-rails rails-engines applicationcontroller ruby-on-rails-3
我想在我的应用程序控制器中使用一个简单的方法,它要求所有用户在继续访问站点的任何部分之前登录.我正在使用Devise进行身份验证.
我试过了:
class ApplicationController < ActionController::Base
...
unless user_signed_in?
redirect_to login_path
end
...
end
Run Code Online (Sandbox Code Playgroud)
这成功地重定向了所有人,但问题是它还阻止了创建新用户会话所需的发布请求.
所以我的问题是,除了登录视图和登录后请求之外,您将如何阻止所有请求?
redirect ruby-on-rails request applicationcontroller ruby-on-rails-3
我正在尝试通过调用返回多个对象:
def index
beers = Beer.all
micros = Micros.all
render json: {beers: beers, micro: micros}
end
Run Code Online (Sandbox Code Playgroud)
但是,对于这两个对象仅返回受尊重的序列化程序中列出的属性,而不是受尊重的序列化程序中的任何has_many、belongs_to 等关系。
如果我只是想返回一个对象,例如:
def index
beers = Beer.all
render json: beers
end
Run Code Online (Sandbox Code Playgroud)
然后它工作正常并返回序列化程序中列出的所有关系。
如何使用多个对象修复调用以返回序列化程序中的所有内容,而不仅仅是属性?
ruby json ruby-on-rails applicationcontroller active-model-serializers
我拼命地尝试将一组默认值合并到我的嵌套参数中。不幸的是, usingdeep_merge
不再适用于 Rails 5,因为它不再继承自Hash
.
所以这并不能正常工作:
class CompaniesController < ApplicationController
def create
@company = current_account.companies.build(company_params)
if @company.save
flash[:success] = "Company created."
redirect_to companies_path
else
render :new
end
end
private
def company_params
params.require(:company).permit(
:name,
:email,
:people_attributes => [
:first_name,
:last_name
]
).deep_merge(
:creator_id => current_user.id,
:people_attributes => [
:creator_id => current_user.id
]
)
end
end
Run Code Online (Sandbox Code Playgroud)
它给了我这个错误:
ActionController::Parameters:0x007fa24c39cfb0 的未定义方法`deep_merge'
那么怎么做呢?
我有一个Rails 5应用程序构建为api应用程序:
# config/application.rb
module MyApp
class Application < Rails::Application
config.api_only = true
end
end
Run Code Online (Sandbox Code Playgroud)
在我的控制器动作之一中,我需要呈现一些Ruby风格的html,然后将其重新提供给json响应。
标准的Rails应用程序可以执行以下操作:
# app/controllers/my_controller.rb
def my_action
html = ApplicationController.new.render_to_string("my_erb_or_haml_file", locals: {foo: "bar"}, layout: false)
render :json => {html: html}, :status => :ok
end
Run Code Online (Sandbox Code Playgroud)
但是,对于我瘦小的api应用来说,ApplicationController.new.render_to_string
似乎只是render " "
。这使我感到困惑,因为我希望它可以提供内容或引发异常。
有什么建议我应该采取什么方式来生成Ruby风格的html?
我有这个index
方法在我的TasksController
:
def index
@tasks = current_account.tasks
@count = @tasks.length
respond_to do |format|
format.html do
...
end
format.zip do
if @count > 100
flash[:notice] = "Please reduce the number of tasks!"
redirect_to :action => "index", :format => "html"
else
DownloadArchive.call(@tasks)
end
end
end
end
Run Code Online (Sandbox Code Playgroud)
html
如果有 100 个以上的任务,如何呈现索引操作的版本?
我上面的代码不起作用。它不是重定向和显示 Flash 消息,而是下载html
文件。我不明白为什么。如果可以,请你帮助我。
ruby-on-rails ×11
ruby ×2
cookies ×1
gem ×1
helper ×1
helpers ×1
json ×1
load-order ×1
nested ×1
parameters ×1
redirect ×1
request ×1
routes ×1