I followed the following tutorial: http://www.themodestrubyist.com/2010/03/05/rails-3-plugins---part-2---writing-an-engine/
And it all works great. I namespaced the controller using
#app/controller/authr/accounts_controller.rb
module Authr
class AccountsController < ApplicationController
unloadable
def new
#account = Account.new
end
def create
#account = Account.new(params[:account])
if #account.save
redirect_to '/'
else
render :action => :new
end
end
end
end
And in the tutorial he didn't namespace the model. I want to namespace my model though so it doesn't collide with host apps. So i tried the following:
#app/models/authr/account.rb
module Authr
class Account < ActiveRecord::Base
attr_accessor :password
validates_confirmation_of :password
end
end
This is my view, with a simple form_for that should go to accounts_path
#app/views/authr/accounts/new.html.erb
<%= form_for(#account) do |f|%>
<p>
<%= f.label :uname, "Username"%>
<%= f.text_field :uname%>
</p>
<p>
<%= f.label :password, 'Password'%>
<%= f.password_field :password%>
</p>
<p>
<%= f.submit "Submit"%>
</p>
<% end %>
But when i use my namespaced model i get the following error:
undefined method `authr_accounts_path' for #<#<class:0x1038f54e0>:0x1038f3780>
The object created by the new method (#account = Account.new) results in this :
<Authr::Account id: nil, uname: nil, hashed_password: nil, remember_token: nil, remember_expiry: nil, created_at: nil, updated_at: nil>
Routes file: (This works when i dont namespace the model.)
Rails.application.routes.draw do |map|
resources :accounts, :only => [:new, :create],
:controller => "authr/accounts"
end
So this is a routing thing. When i dont namespace the model all works fine but when i namespace it it doesnt work. Then i tried the following:
#routes.rb
Rails.application.routes.draw do |map|
scope "authr", :module => :authr, :as => "authr" do
resources :accounts
end
end
Now i get the form without the routing error. But when i try to submit the form the object isn't saved.
Started POST "/authr/accounts" for 127.0.0.1 at Mon Mar 28 18:51:12 +0200 2011
Processing by Authr::AccountsController#create as HTML
Parameters: {"commit"=>"Submit", "authenticity_token"=>"cPH8ZmNmgoT84UMnYBoM38di+/OZQmuGQTrSv3HhFR4=", "utf8"=>"✓", "authr_account"=>{"uname"=>"usrrrrrrrrrrrrnmmmmeee", "password"=>"[FILTERED]"}}
SQL (48.0ms) BEGIN
SQL (0.5ms) SHOW TABLES
SQL (13.2ms) describe `accounts`
AREL (0.3ms) INSERT INTO `accounts` (`updated_at`, `created_at`, `remember_expiry`, `uname`, `remember_token`, `hashed_password`) VALUES ('2011-03-28 16:51:12', '2011-03-28 16:51:12', NULL, NULL, NULL, NULL)
SQL (0.4ms) COMMIT
Redirected to http://localhost:3000/
I know that i'm doing #account = Account.new(params[:account]) and if i change it to Account.new(params[:authr_account] that i should work but i want to user params[:account] that should work right? Because the controller is namespaced as well...
Then i found something about isolated_name space so i tried this:
#lib/authr/engine.rb
require "authr"
require "rails"
module Authr
class Engine < Rails::Engine
isolate_namespace Authr
# engine_name :authr #deprecated?
end
end
and i changed my routes to:
Rails.application.routes.draw do |map|
resources :accounts, :only => [:new, :create],
:controller => "authr/accounts"
end
But this gives me the following error:
/Library/Ruby/Gems/1.8/gems/authr3-0.1.0/lib/authr/engine.rb:6: undefined method `isolate_namespace' for Authr::Engine:Class (NoMethodError)
I tried everything and i looked at other gems and they have namespaced models. I am convinced that i need to namespace my models just to be sure that they don't conflict with the host application. I want to use restfullroutes but i don't know how i can fix this problem.
I am using:
Daniel-Zs-MacBook-Pro:gem_test Daniel$ ruby -v
ruby 1.8.7 (2009-06-12 patchlevel 174) [universal-darwin10.0]
Daniel-Zs-MacBook-Pro:gem_test Daniel$ rails -v
Rails 3.0.3
Thanks for any advice / help
Possibly a typo?
scope "authr", :module => :authr, :as => "auth" do
change to
scope "authr", :module => :authr, :as => "authr" do #you are missing an r
If its just a typo in this post and you have it correct in the engine, then what do you get when you run "rake routes" from the parent application using that same scope in the engine?
Also, I think isolate_namespace is only in edge rails right now. 3.1 is slated to have alot of new engine goodies including this.
Related
I created the following devise scope:
devise_scope :users do get
get 'spotkey' => 'spotkeys#spot_page'
get 'dashboard' => 'spotkeys#dashboard'
post 'dashboard' => 'spotkeys#dashboard'
get 'signup' => 'users/registrations#new', :as => :new_user_session
post 'signin'=> 'users/sessions#create', :as => :user_session
delete 'signout' => 'users/sessions#destroy'
end
controllers/users.rb
class UsersController < ApplicationController
def index
end
def show
#user = User.find_by(id: params[:id])
end
def dashboard
#keys = Spotkeys.all
#keys = Spotkeys.new
#spotkeys = Spotkeys.all
#spotkeys = Spotkeys.new
end
end
views/spotkeys/dashboard.hrml.erb
<div class="key">
<%= #keys.location %><br/>
<%= #keys.picture_url %><br/>
<%= #keys.floor_number %><br/>
<%= #keys.description %><br/>
<%= #keys.floor %><br/>
<%= #keys.buzzer_code %><br/>
<%= #keys.parking_info %><br/>
<%= #keys.cross_street %><br/>
<%= #keys.public_transit %>
</div>
I'm getting the follow error:
missing :controller key on routes definition, please check your routes
Please let me know if you need to see any other files.
Problems I noticed:
Regarding to this comment and devise how-to wiki, devise_scope needs the resource name in singular (devise_for, in contrary, needs the resource name in plural).
You also have a weird get on the same line with devise_scope- it might cause the problem.
Routing mapper, from where the error is raised, also suggests that it does not find such controller. It probably tries to search a controller based on your plural form of devise_scope. OR it tries to find controller name after your extra-get keyword after :users do.
I'm trying to add a new form (for a facebook invite) inside the devise invitable new view: devise/invitations/new.html.erb. I'm struggling to get the routing right. I'm using Rails 4, if it makes a difference.
view (views/devise/invitations/new.html.erb)
<%= form_tag "invite_fb_friends" do %>
...
<% end %>
controller (controllers/users/invitations_controller)
def invite_fb_friends
raise params.to_yaml # debug
end
routes
# this is probably wrong
devise_scope :user do
post 'users/invitation/invite_fb_friends', :to => 'users/invitation#invite_fb_friends'
end
Routing Error
ActionController::RoutingError (uninitialized constant Users::InvitationController)
You're very close, but I think it's just pluralization in your route that's the problem.
Note "invitationS" in the controller name.
devise_scope :user do
post 'users/invitation/invite_fb_friends' => 'users/invitations#invite_fb_friends'
end
I'm building a simple app with standard User model that has_one Profile model. I would like the profiles table to have a column for an image attribute. Therefore, I followed RailsCast #253 titled CarrierWave File Uploads. All is going well until I try to resize an image that has been uploaded. This requires the installation of ImageMagick & RMagick which took an entire day of searching to get done. However, I think I finally got it right and successfully installed rmagick version 2.13.2 (as verified by running "gem list").
But not so fast...now when I try to render the form to create a new profile, I get the following error:
NoMethodError in Profiles#new
undefined method `model_name' for NilClass:Class
FYI, this form was working fine until I un-commented "include CarrierWave::RMagick" in my ImageUploader (which I'm suppose to do if I want to use RMagick methods for image re-sizing).
Any thoughts on how to fix this?
My version info (I used RailsInstaller for Windows to get up & running)
Rails 3.2.13
Ruby 1.9.3p392 [i386-minw32]
ImageMagick 6.8.5-Q16
rmagick 2.13.2
Gemfile
gem 'rmagick'
gem 'carrierwave'
Models
class User < ActiveRecord::Base
has_one :profile
class Profile < ActiveRecord::Base
attr_accessbile :image
belongs_to :user
mount_uploader :image, ImageUploader
ImageUploader
class ImageUploader < CarrierWave::Uploader::Base
include CarrierWave::RMagick
storage :file
def store_dir
"uploads/#{model.class.to_s.underscore}/#{mounted_as}/#{model.id}"
end
end
ProfilesController
def new
#profile = current_user.build_profile
end
views/profiles/new.html.erb
<%= form_for #profile, :html => {:multipart => true} do |f| %>
<%= f.file_field :image %>
<%= f.submit "Create my profile" %>
<% end %>
Just replace this line
<%= form_for #profile, :html => {:multipart => true} do |f| %>
by
<%= form_for #instructor_profile, :html => {:multipart => true} do |f| %>
Issue is really with form_for not with RMagick, you have defined #instructor_profile in controller but using #profile in view. Make sure that there will be single space between form_for and #instructor_profile
Does anyone know if it is possible (and, if so, what the syntax is) for using a nested resource with the best_in_place gem?
My routes.rb looks something like this
resources :users do
resources :goals
end
I would like to edit the :description field of the goal, but the code in my view for
<%= best_in_place [#user, #goal], :description %>
gives a NoMethodError saying
undefined method `description' for #<Array:0x20e0d28>
Using
<%= best_in_place #goal, :description %>
give me an undefined method error also because there is no goal_path
I can get the gem to work for #user (the non nested resource) field without problems.
I'm running Rails 3.1.1, Ruby 1.9.2, best_in_place 1.0.4
I figured it out.
I needed to set the path option in the call like so
<%= best_in_place #goal, :description, :path => user_goal_path %>
It works like a champ now!
Add path and the objects to the path:
<%= best_in_place #goal, :description, :path => user_goal_path(#user,#goal) %>
Somehow the simple path solution of bknoles didn't work for me.
Now above method is deprecated.
According to latest Documentation use ":url" instead of ":path" like below in the example
<%= best_in_place #goal, :description, :url => user_goal_path %>
Cheers!
Thank you, #bknoles. Your answer definitely helped me reach a similar solution of my own. Here's my implementation:
#widget.rb
class Widget < ActiveRecord::Base
validates_presence_of :name
has_many :gadgets
attr_accessible :name, :description
end
#gadget.rb
class Gadget < ActiveRecord::Base
belongs_to :widget
attr_accessible :name, :widget_id, :id
end
#gadgets_controller.rb
def update
#gadget=#widget.gadgets.find(params[:id])
if #gadget.update_attributes(params[:gadget])
respond_to do |format|
format.html
format.json { respond_with_bip(#gadget) }
end
else
respond_to do |format|
format.html { render :action => "edit" }
format.json { respond_with_bip(#gadget) }
end
end
end
#views/gadgets/_gadget.html.haml
%tr{ :name => "gadget_", :id => gadget.id }
%td= gadget.created_at.localtime.strftime("%B %d, %l:%M%p")
%td.big=best_in_place gadget, :name, :path => [#widget, gadget]
%td.delete{:style => 'text-align:center;'}
=check_box_tag "gadget_ids[]", gadget.id, false, :class => "checkbox"
You can checkout the entire project on github if you want to see more of the code.
https://github.com/hernamesbarbara/ajax-rails-full-crud
Best,
Austin
I have a problem with rails 3 routes.
I want add a new action called "gestion_etudiant" with a new view "gestion_etudiant.html.erb".
I have on my index page a link like this
<%= link_to "Administration", {:controller => "users", :action => "gestion_etudiant"} %>
I also try this:
<%= link_to "Administration", "/users/gestion_etudiant" %>
In my controller:
def gestion_etudiant
#users = User.find(:all)
end
but when I clic on the link, I always have this error:
ActiveRecord::RecordNotFound in UsersController#show
Couldn't find User with ID=gestion_etudiant
I have this in my routes file:
resources :users
And I've also try to add:
match "users/gestion_etudiant", :to => "users#gestion_etudiant"
and
resources :users, :only => [:gestion_etudiant]
But I can not access my page "gestion_etudiant.html.erb". Can anybody suggest why?
Try this:
# router:
resources :users do
get :gestion_etudiant, :on => :collection
end
# view:
link_to "Administration", gestion_etudiant_users_path
# Controller
def gestion_etudiant
#users = User.all # Don't use find with :all as it will be deprecated in rails 3.1
end
In your routes, try:
resources :users do
collection do
get 'gestion_etudiant'
end
end
You can check the routes you have in your application by running rake routes