use engine routes instead of main app(wrapper app) - ruby-on-rails-3

I am implementing rails app in which I have a rails engine as well.
In main app I have a model Marks
class Mark < ActiveRecord::Base
end
Rake routes for Mark is (simple and index page url)
mark_path(`)
Url for this /marks/
and inside the engine also there is a model Mark.
class Test::Mark < ActiveRecord::Base
end
For engine
marks_path
Url for Engine
/test/marks/1
Query
So how can access engine marks_path in engine's model.
Currently when use the url_helper in model it return main app routes instead of engine routes.
But I need engine routes.

Related

Accessing Custom Parameters when using Devise and Rails 4

I am learning rails using the teamtreehouse tutorial. The tutorial uses 3.1 but I am trying to learn 4.0, and as a result I have run into a difficulty presumably because rails 4 forces of the use of strong parameters. I have two models, a users model and a statuses model. I have used devise to create authentication for users, and have included new parameters. They are :first_name, :last_name, and :profile_name. I have created a relationship between users and statuses.
Currently the user sign-up with the new parameters is working; i can access them using for instance current_user.last_name. However, I want to show the profile_name of the user that created the post on the statuses index view(each user does not yet have a separate page). I want to do this using
status.user.profile_name
However it just shows up blank. If I do
status.user.email(which is a preconfigured devise parameter), it shows up no problem. I am guessing I have to whitelist these parameters in some controller but I don't know where or how.
Thanks
I think, here you will find your answer: https://github.com/plataformatec/devise/tree/rails4#strong-parameters
Based on above link, I think you should insert something like this in your ApplicationController:
class ApplicationController < ActionController::Base
before_filter :configure_permitted_parameters, if: :devise_controller?
protected
def configure_permitted_parameters
devise_parameter_sanitizer.for(:user) { |u| u.permit(:profile_name) }
end
end
And I already suggested in a previous question...
Strong parameters with Rails and Devise
...that you can create your own controller which could extend devise own controller. There is a gist for that:
https://gist.github.com/bluemont/e304e65e7e15d77d3cb9
A little bit more details in Devise doc: https://github.com/plataformatec/devise/tree/rails4#configuring-controllers

Difference between Active Model, Active Record and Active Resource

Is there anyone who can help me by defining the exact difference between Active Model, Active Record and Active Resource. I have done enough googling in order to find the exact difference, but didn't get anything concrete which can tell the exact difference between them. Right now they all look same to me. So please give me the appropriate answer with some concrete points.
Rails 3 is designed with modularity in mind. Each module has its own purpose and functionality.
ActiveModel: This component was created in Rails 3. They took all the model related parts that did not have a database requirement of Rails 2 ActiveRecord and moved it into ActiveModel. So ActiveModel includes things like validations. More information: http://www.rubyinside.com/rails-3-0s-activemodel-how-to-give-ruby-classes-some-activerecord-magic-2937.html
ActiveRecord: This is the component that associates a class to the database. This will give the class functionality such as methods that make it easy to pull records from the database (An example is the find method).
ActiveResource: Similar to ActiveRecord. However, instead of being backed by a database, an ActiveResource object is backed by another application through a web service API. More information: http://ofps.oreilly.com/titles/9780596521424/activeresource_id59243.html
(Couldn't figure out about ActiveBase... where did you hear it from?)
What I understand:
ActiveModel + Database Support = ActiveRecord
ActiveModel via WebService API = ActiveResource
ActiveModel https://github.com/rails/rails/tree/master/activemodel
Think of a super model who is in constant need of validation.
ActiveModel can be used for many things, but mostly recognized for adding validation support to models / db records.
ActiveRecord https://github.com/rails/rails/tree/master/activerecord
Think record as in table record.
Sets up a mapping between a new class and an existing table in a database.
In the context of an app, these classes are commonly referred to as models. Models can also be connected to other models; this is done by defining associations.
class Firm < ActiveRecord::Base
has_many :clients
has_one :account
belongs_to :conglomerate
end
In the background, rails uses ActiveRecord for schema management and defining properties for your records, acting as an ORM (object relational mapper):
"ORM: An object that wraps a row in a database table or view, encapsulates
the database access, and adds domain logic on that data."
A schema outlines the properties for a record.
ActiveResource https://github.com/rails/activeresource
Think resource like the R in URL or of the resource routing that powers many rails backends.
Allows you to do things like Create, Retrieve, Update, or Destroy (CRUD) via HTTP.
tyler = Person.find(1)
When a request is made to a resource route, a RESTful request maps itself its corresponding HTTP verbs and their database interactions
GET => Person.find(1)
POST => Person.new(:name => 'Tyler', :favorite_page => 'stackoverflow')
PUT => tyler.save
DELETE => tyler.destroy

rails 3.1.1 engines - with mountable engines, is it possible to access parent app assets, default layout?

This is more for experimentation - I am aware that I can do this with --full but I wanted the functionality of namespacing in the app to avoid conflicts
The idea is to have a main app - which handles authentication, common items, admin screens etc
Then creating engines to add further functionality like
crm
cms
blog
wiki
forum
etc
These engines I can pick and choose as I need for whatever kind of app I am building.
Is this possible?
Is it just the case of applying both --mountable and --full options?
Experimenting - would there be any issue if I use the full option add rspec and then simple add
rails plugin new plugin_name --skip-test-unit --full --dummy-path=spec/dummy
and in lib\plugin_name\engine.rb
module PluginName
class Engine < Rails::Engine
# this is added by rails when an engine is mountable
# to isolate the plugin and prevent name clashes
isolate_namespace PluginName
# one of the additions to make rspec work from command line for the plugin
config.generators do |g|
g.test_framework :rspec, :view_specs => false
end
end
end
I have already created both --full and --mountable engines and have rspec finally working for anyone reading there are some great articles (see below), but wondered of the wider impact of doing this for the solution I am trying to create
I am still playing with this and will post my findings..
Any help/discussion will be massively appreciated.
Please Note
Why I want to do it - build once use many times...
I would never want a non-tech/client to add "plugins/engines" - this is purely to entertain point 1.
Issues I am Having...
Running the server on the top level app. Only when accessing content from the engine, (I can see by the error messages) I have a routing problem (root_path undefined or devise routes missing) - the parent application layout is being rendered, I can see it in the extracted source of the error. Progress but no cigar just yet!
Useful References
Engines vs Mountable Apps
3.1 engines with rspec
testing rails 3 engines
Listing Routes in a Mountable engine
I managed to get this working with the following steps:
In my parent app I was mounting the engine in routes.rb
mount PluginName::Engine => '/plugin_name'
I just removed it.
Created an application controller as Ryan Bigg below had stated.
class PluginName::ApplicationController < ApplicationController
...
end
As I wanted to have things name spaced when generating controllers, models, tests so you have to essentially comment out the isolate_namespace PluginName lib\plugin_name\engine.rb when I wanted the gem to be run in the parent app.
It is not yet an ideal solution. off the top off my head, I could use something like:
isolate_namespace PluginName if %w[development testing].include?(Rails.env)
but will have to test if this is practical.
Kudos to Ryan for helping me find my way many thanks
Furthermore, the same can be done with the --mountable switch version and all you need to do is one further step in your engines config/routes.rb replace
PluginName::Engine.routes.draw do
with
Rails.application.routes.draw do
Yes, you can reference the parent application assets just by referencing them in your application like normal:
<%= stylesheet_link_tag "application %>
Although, not sure why you would want to do that because...
I'm going to answer your first question with the answer to your second question.
To use the application's layout you will need to modify the ApplicationController in the engine (which is namespaced) and have it inherit from ApplicationController in the engine.
That will then have the controllers for the engine using the layout provided by the engine. I'm doing this in my engine, forem.
One day, this will be covered in the Engines Guide that, at this time of writing, is currently being written.

Rails 3 controller from plugin

I am creating a Rails 3 plugin and I want to integrate controllers in it that will be automaticly consider by rails as a "normal" controller from the app/controllers folder. How can I do that or what is the best solution for me to have custom controllers from a plugin?
I have found documentations from guides.rubyonrails.org but they have changed the documentation and plugin development doesn't include controllers anymore.
Thanks
You will need to define a class within your plugin that inherits from Rails::Engine. In effect, the feature you want is an engine.
Define the class like this:
lib/your_thing/engine.rb
module YourThing
class Engine < Rails::Engine
end
end
You can then define your engine's controllers at app/controllers within that plugin and for them to work neatly you will also need to define routes for them, which you can do inside config/routes.rb inside the engine like this:
YourThing::Engine.routes.draw do
resources :things
end
Next, you'll need to mount your engine inside your application:
mount YourThing::Engine, :at => "/"
The application then will be able to use routes from your engine.
For more information, I'm in the progress of writing the official Rails Engine guide which you can reference here. Please let me know if you have any further questions and I'll try to answer them in the guide.

Ruby on Rails 3 - Routing

I am writing a rails application and getting stuck on a routing issue.
My application allows a user to manage multiple cause pages (for various reasons)
right now, I am trying to build the admin screens for users to update their site.
for example, cause pages could look like:
.com/causes/1
.com/causes/2
I and want the admin URL to be:
.com/causes/1/admin/updates
.com/causes/2/admin/updates
etc.
How would I setup my routes to do this
I originally thought something like:
namespace "admin" do
resources :updates
end
But how can I prefix that with the cause/:id so that I can relate which cause I am updating?
You will need to have an admin_controller of sorts and you can setup your routes like so:
resources :admin do
#insert any resource you need to have admin pages on here
resources :causes do
resources :updates do
end
end
end
something like that. Or you could try some rails admin gems to make your life easier like: http://www.activescaffold.com/