I want to get next thing...
# For ArticlesController > ApplicationController
# in view
render 'articles/edit/form'
# tries 'app/views/articles/edit/_form.html.erb'
# then tries 'app/views/articles/_form.html.erb'
# then what it wants
Or maybe render with array partial option:
# For ArticlesController > ApplicationController
# in view
render_exists ['articles/edit/form', 'articles/new/form']
# tries 'app/views/articles/edit/_form.html.erb'
# then tries 'app/views/articles/new/_form.html.erb'
# then what it wants
This isn't realized, is this? But maybe some gems for 3.2 or monkeypatches... And don't you know pull requests to rails about it? Thanks!
UPD That's isn't controller-based view inheritance. This should work for (at the same page):
render `articles/edit/form`
render `comments/edit/form`
I'm using the mechanism I described in an article in the rails forum
It works a treat for me though I hear there is now some built in support in the latest versions or at least effort is under way to do add such a feature.
That already exists, and it's very similar to the controller inheritance.
You must follow a conventional strategy, however. You would put your global partial in app/views/application, then you can put a more specific one at each inherited level, like app/views/articles.
Take a look at the following railscast for more details: #269 Template Inheritance
Related
I am trying to define some helper methods to be used in the app's controller, but it seems that rails don't even call the controller. just for the test I have the following controller in my app/controllers/my_engine/application_controller.rb and as the documents say rails should find it first and an error should raise because THIS_SHOULD_PRODUCE_ERROR is unknown, but the rspec happily executing without any errors!
class ApplicationController < ActionController::Base
THIS_SHOULD_PRODUCE_ERROR
end
I even tried to mimic the devise's way but the results are the same!
The guide section on the app directory suggests that the application_controller in an engine "will provide any common functionality for the controllers of the engine".
So I wouldn't expect that any additions to that controller will be available to all controllers in an application.
That also means that your application_controller is, I suspect, not getting called when you're running your test. Which would explain why you're not seeing an error.
In terms of how devise does it I think you need to be looking at how define_helpers works. The code you've linked to in your question is the application controller in the test app for the devise gem.
I noticed that I have got things wrong, and the application_controller in the engine does not get applied to application_controller in the app! Also, I couldn't figure out how the devise did it, but I have come up with the simple workaround for this:
require_relative 'controllers/helpers'
module Acu
module Injectors
class << self
ActiveSupport::Notifications.subscribe "start_processing.action_controller" do |**args|
eval((Acu::Configs.get :base_controller).to_s).class_eval do
include Acu::Controllers::Helpers
end
end
end
end
end
This will inject controller helpers to the user's base controller (which I get from the user, default: :ApplicationController) at the end of the class, which is perfect for me (but don't know how to add it to begging of the class if anyone needs it)
When I generate a scaffold in rails, I noticed that the methods are prefaced with # remarks. I haven't been able to find any documentation as to if they actually do anything of if they are similar to what looks like remarks in application.js that look like remarks but are really code.
For example:
# POST /attachments
# POST /attachments.json
def create
and
# GET /attachments/1
# GET /attachments/1.json
def show
end
I am using rubymine as my editor.
No, they don't do any magic behind the scenes. They are just comments to help you out.
By default, scaffolding will direct POST requests to create(), and GET of a particular resource (e.g. /resources/<id>) to show(). These associations are defined in your routes, and scaffolding applies this convention. You're free to change them in your routes if you wish.
Those are just comments in Ruby. They are ignored by the Ruby interpreter and are meant for the developer.
I wasnt sure what to call the title, but I guessed. :P
Basically I wanted to setup a somewhat complicated website, and I dont know how to setup the routes. Here is how it would work.
/ruby would show info on Ruby programming language
/ruby/rails would show info on ROR
/ruby/sinatra would show info on sinatra
/php would show info on PHP
/php/laravel would show info on Laravel
ETC.
Im not sure exactly how everything would work. I want a controller for each framework/language. I am thinking to making a folder for each language and having controller in it, but im not really sure if thats the best option.
EDIT: I forgot to mention that each langugae will have its own set of pages. EX.
/ruby/rails/models shows info on rails models
/ruby/rails/controllers shows info on rails controllers
/ruby/rails/routing shows info on rails routing
/ruby/oop talks about oop
/ruby/variables shows how to define variables
ETC. Its essentially going to be sort of like documentation.
Thanks!
Try http://guides.rubyonrails.org/routing.html Section 3.2, "Dynamic Segments"
get ':controller/:action'
Now create a controller for each language. For example, php.rb, ruby.rb, etc. Each framework is an action in that controller
class Ruby < ApplicationController
def rails
end
def sinatra
end
end
EDIT
I don't have the time to test this one at the moment, but let me know if it works or not :-)
# Routes.rb:
get ':controller/:action/:pagename'
And then for the Ruby class
class Ruby < ApplicationController
def rails
if template_exists?("#{params[:pagename]}", _prefixes)
render params[:pagename]
end
end
end
Recently I scaffold-ed two of my models/controllers/view, let's call them xxx and yyy. Now I look under the controller file, I see absolutely nothing ! but it was still functioning, upon investigation I found that was due to the
inherited_resources Gem
So the controllers look like this currently
class xxx < InheritedResources::Base
end
so if I change
InheritedResources::Base to ApplicationController
like I have it other controller, would it behave like normal controller ? I tried looking up on the docs, I was referred here for questions.
What is the best way to get normal controller/models back for those two scaffolds ?
Thanks for your time and help.
in config/application.rb add:
#use rails scaffolding generator
config.app_generators.scaffold_controller = :scaffold_controller
When you use InheritedResources, the gem registers a controller generator that generates just that, your controller definition. The point of using InheritedResources::Base is to clean your controllers and move all of the seven REST actions to a common class. You don't need to define any of the following methods if you extend InheritedResources::Base
index
new
create
edit
update
show
destroy
They're all the defined for you. Go ahead and make a test, scaffold a resource and go to it's index method, add a couple records, play around...
Now if you really want to go back to the old way and have your code generated by the bundled controller generator, remove inherited_resources from your Gemfile, bundle install, and generate your scaffolds again.
Hope it helps (:
P.S. if you decide to use InheritedResources (which I suggest you do) you can overwrite any methods you want to customize. Have a look at the docs, everything is more clear over there.
I've tried a handful of captchas for Rails 3 and none tend to play nicely with Mongoid. I don't need anything too fancy just something to do a quick human check.
How do you guys get one working with Mongoid? Are there alternative solutions?
That's outside mongoid scope, but still applicable. Have a look at Negative Captcha:
Negative captchas create a form that has tasks that only bots can perform, but humans cannot. This has the exact same effect, with (anecdotally) a much lower false positive identification rate when compared with positive captchas. All of this comes without making humans go through any extra trouble to submit the form. It really is win-win.
You can use simple-captcha v1rtual's branch with mongo support. Simple and clean setup and usage:
Just add to your Gemfile as:
gem 'wolcanus-simple_captcha', :require => 'simple_captcha', :git => 'git://github.com/v1rtual/simple-captcha.git'
Run generator:
rails generate simple_captcha
For Controller Based, add the following line in the file "app/controllers/application.rb":
ApplicationController < ActionController::Base
include SimpleCaptcha::ControllerHelpers
end
In the view file within the form tags add this code:
<%= show_simple_captcha %>
and in the controller’s action authenticate it as
if simple_captcha_valid?
do this
else
do that
end
See the branch for more options: https://github.com/v1rtual/simple-captcha