I am having a problem to add custom form into Devise edit page. Currently, I have two models User and UserAddress. I splitted edit page on multiple pages, here are my routes:
devise_for :users, path: "user", path_names: {sing_in: "sign_in", sing_out: "sign_out", sing_up: "sign_up", edit: "edit"}, :controllers => { :registrations => :registrations }
devise_scope :user do
match 'user/edit' => 'registrations#edit', via: :get, :as => :user_basic
match 'user/edit/addresses' => 'registrations#addresses', via: :get, :as => :user_addresses
...
resources :user_addresses, only: [:create, :destroy]
end
resources :users, path: "user", only: [:show]
I created User Addresses controller to handle create and destroy actions:
class UserAddressesController < ApplicationController
def create
#User = User.find(params[:user_id])
if params[:addresses]
params[:addresses].each do |address|
#User.address.create(address: address)
end
#addresses = #User.addresses
redirect_back(fallback_location: request.referer, notice: "Success")
end
end
def destoy
#address = Address.find(params[:id])
#User = #address.user
#address.destroy
#addresses = Address.where(user_id: #user.id)
respond_to :js
end
end
And now I want to add the ability to add new addresses from Devise edit page (I will use ajax lately to update the records under the form), like this (haml):
= form_for user_addresses_path, url: user_addresses_path(current_user), method: 'post', :html => { class: "form", :multipart => true } do |f|
= f.text_field :name
= f.text_field :address
= f.submit
When I click submit, I am getting the following error:
No route matches [POST] "/user/edit/addresses.1"
I think that I need to move resources :user_addresses, only: [:create, :destroy] out of the devise_scope, however I don't want to change the url (www.url.com/user/edit/addresses). What am I doing wrong? Is it even possible to add custom forms into Devise? Thank you very much for your help and time.
UPDATE:
UserAddress model:
class UserAddress < ApplicationRecord
belongs_to :user, :foreign_key => 'user_id'
# Geocoder
geocoded_by :address
after_validation :geocode, :if => lambda{ |obj| obj.address_changed? }
end
Here your route user/edit/address is being generate from the match and is written via get method so you need to change it with
match 'user/edit/addresses' => 'registrations#addresses', via: :post, :as => :user_addresses
and in the form url path helper you are passing current user as a parameter while it does not exist in your route so form should get replace with
= form_for user_addresses_path, url: user_addresses_path, method: 'post', :html => { class: "form", :multipart => true } do |f|
Related
I use the gem Best In place for editing discussion topics and comments... It works for Discussion but for Comments it gives record not found error:
my routes:
namespace :school do
resource :account, :except => [:new, :create, :destroy], :controller => 'account'
resources :classes, :path => "" do
resources :discussions do
resources :comments
end
end
end
show.html.haml (Discussion)
= best_in_place #discussion, :name, :type => :textarea, :path => school_class_discussion_path
= best_in_place c, :body, :type => :textarea , :path =>school_class_discussion_comment_path(params[:class_id], #discussion)
discussion_controler.rb
def update
#discussion.update_attributes(params[:discussion])
respond_with #discussion
end
comments_controller.rb
def update
#comment = Comment.find(params[:id])
#comment.update_attributes(params[:comment])
respond_with #comment , :location => school_class_discussion_comment_path(params[:class_id], #discussion))
end
I can click on comments and edit but does not update :( log error:
[Raven] User excluded error:ActiveRecord::RecordNotFound comment with id = actually gives discussion id
Could you maybe show us your show contorller action for discussion?
I guess that something like this might solve your problem:
= best_in_place #discussion, :name, :type => :textarea, :path => school_class_discussion_path(params[:class_id], #discussion)
= best_in_place c, :body, :type => :textarea , :path =>school_class_discussion_comment_path(params[:class_id], #discussion, c)
In my app, I have sheets controller, model name is Sheet but my routes are as below
routes.rb
namespace :magazine do
resources :pages, :controller => "sheets" do
resources :articles do
resources :comments
so that url will be magazine/page/1/article...
In my Article controller how to call load_and_authorize_resource for sheets so that I can access article of the related sheet.
I tried
load_and_authorize_resource :sheet, :class => 'Sheet', :parent => false
load_and_authorize_resource :through => :sheet
Cannot access #sheet.articles......
Either you have:
load_and_authorize_resource :page, :class => 'Sheet', :parent => false
And you access your data with #pages
Or you replace with:
load_and_authorize_resource :sheet, :class => 'Sheet', :parent => false
And you access your data with #sheets
In ArticlesController, to get both sheet and articles:
load_and_authorize_resource :sheet, :class => 'Sheet'
load_and_authorize_resource :article, :through => :sheet
I want my artist links to look like this:
http://admin.foobar.com/artists/123
http://www.foobar.com/123
My Routes setup looks like this:
class AdminSubDomain
def matches?(request)
whitelists = IpPermission.whitelists
if whitelists.map { |whitelist| whitelist.ip }.include? request.remote_ip
request.subdomain == 'admin'
else
raise ActionController::RoutingError.new('Not Found')
end
end
end
Foobar::Application.routes.draw do
constraints AdminSubDomain.new do
..
resources :artists, :only => [:index, :show], :controller => 'admin/artists'
end
get ':id' => 'artists#show', :as => 'artist' do
..
end
end
Rake routes returns:
artist GET /artists/:id(.:format) admin/artists#show
artist GET /:id(.:format) artists#show
At the moment, <%= link_to 'Show', artist_path(artist, :subdomain => :admin) %> points to: http://admin.foobar.dev:3000/123.
It should look like: http://admin.foobar.dev:3000/artists/123
What am I doing wrong?
You have used the same name (artist) for both routes, so when you call artist_path, you get the last one you've defined, which is: get ':id' = 'artists#show', :as => 'artist' do ....
Use a different name for the admin route to distinguish it:
constraints AdminSubDomain.new do
..
resources :artists, :only => [:index, :show], :controller => 'admin/artists', :as => 'admin_artists'
end
Then you can call it with: <%= link_to 'Show', admin_artist_path(artist, :subdomain => :admin) %>.
Currently
Project101::Application.routes.draw do
match '/:id' => 'companies#show'
resources :companies do
resources :customers
resources :users
resources :categories
resources :addresses
end
devise_for :users
resources :users, :controller => "users"
root :to => "companies#index"
end
Everything belongs to a company. Trying to create routes like www.example.com/:id/customers where :id is always the company id.
At the moment www.example.com/:id works but all url's are generated as /companies/:id/cusotmers.
Saw Rails 3 Routing Resources with Variable Namespace.
Is this the right way of doing this?
EDIT
Kept :as => :company to help generate the URL's, Links, etc a little easier for me. Sure others could do cleaner or better method. Also had to manually create the edit, destroy, new with different urls so I could use them in links if user was admin.
Project101::Application.routes.draw do
match '/' => 'companies#index'
match '/companies' => 'companies#index'
match '/:company_id' => 'companies#show', :as => :show_company
match '/companies/:id/edit' => 'companies#edit', :as => :edit_company
match '/companies/:id/new' => 'companies#new', :as => :new_company
match '/companies/:id/destroy' => 'companies#destroy', :as => :delete_company
scope '/:company_id', :as => :company do
resources :customers
resources :users
resources :categories
resources :services
resources :addresses
end
devise_for :users
resources :users, :controller => "users"
root :to => "companies#index"
end
Then just used basic nested_resources for links, controllers and forms.
class ApplicationController < ActionController::Base
protect_from_forgery
helper_method :current_company
def current_company
if params[:company_id] != nil
#current_company ||= Company.find(params[:company_id])
else
#current_company = nil
end
return #current_company
end
end
Basic links
<%= link_to "Customers", company_customers_path(current_company) %>
links for specific customer
<%= link_to #customer.name, edit_company_customer_path(current_company, #customer) %>
Controllers look like
class CustomersController < ApplicationController
before_filter :authenticate_user!
load_and_authorize_resource
def new
#company = current_company
#customer = #company.customers.new
end
def create
#customer = Customer.new(params[:customer])
if #customer.save
flash[:notice] = "Successfully created customer."
redirect_to company_customer_path(current_company, #customer)
else
render :action => 'new'
end
end
end
And finally my forms look like
<%= form_for [#company, #customer] do |f| %>
<%= f.error_messages %>
....
<% end %>
Yes, if you always want the routes to begin with the company id you can wrap them in a scope like this:
scope ":company_id" do
resources :customers
resources :users
resources :categories
resources :addresses
end
I have a Vehicle model:
Routes:
map.resources :vehicles, :has_many => :suppliers
Everything works great, but Vehicle has a boolean attribute is_truck. I want to make an Alias so I can get the same resources filtering only trucks, I tried with:
Routes:
map.trucks '/trucks', :controller => :vehicles, :action => :index, :is_truck => true
map.trucks '/trucks/by_supplier/:supplier', :controller => :vehicles, :action => :index, :is_truck => true
The first one works well, but when I search within a Form the second doesn't work and searches all suppliers.
Controller:
class VehiclesController
def index
if params[:supplier]
#vehicles = Vehicle.all :conditions => { :is_truck => params[:is_truck] }
else
#vehicles = Vehicle.all
end
end
...
end
Search Form:
<% form_for :truck, :url => {:controller => :trucks, :action => :index}, :html => {:method => :get} do |f| %>
<% f.text_field :search %>
<% f.submit 'Search Trucks' %>
<% end %>
Is it possible to map.resources as an alias ?
I found a cleaner way to do it, but Search is still broken under a specific supplier:
# Show all vehicles
map.connect '/vehicles/supplier/:supplier', :controller => :vehicles, :action => :index
map.resources :vehicles
# Only show trucks
map.connect '/trucks/supplier/:supplier', :controller => :vehicles, :action => :index, :is_truck => true
map.resources :vehicles, :as => 'trucks', :requirements => { :is_truck => true }
Resource: http://api.rubyonrails.org/classes/ActionController/Resources.html
Just amend your routes in the following way:
map.resources :vehicles, :has_many => :suppliers,
:collection => { :trucks => :get }
And check rake routes for the routes this generates. It will allow you to list vehicles which are trucks:
trucks_vehicles GET /vehicles/trucks(.:format)
{:controller=>"vehicles", :action=>"trucks"}
So you now just need to add a new action called "trucks" which works similar to "index". Forms should keep track on it's own (via form fields) if you create a truck or another vehicle. Don't try to trick around with rails routing, which usually would mean your app design is flawed which will get you into trouble later.
You may take a look at STI (single table inheritance: one table stores multiple classes of vehicles). Another way would be to create a trucks controller which inherits from the vehicles controllers and overwrites just some methods like so:
class TrucksController < VehiclesController
def new
#is_truck = true
super
end
...
end
or
class TrucksController < VehiclesController
before_filter :this_is_a_truck
...
private
def this_is_a_truck
#is_truck = true
super
end
end
Update: Here's another one (given you have a is_truck column):
class TrucksController < VehiclesController
around_filter :with_truck_scope
...
private
# Scope every active record access with an is_truck condition
# you may want to put this directly into the model to get rid of the .send
# method and directly access "Vehicle.with_truck_scope &block" here
def with_truck_scope(&block)
Vehicle.send :with_scope, :find => { :conditions => "is_truck = 1" },
:create => { :is_truck => 1 }, &block
end
end
But I recommend really first to try out going with the :collection and :member parameters of Rails' routing.
are you processing it in your controller?
smth like:
class VehiclesController
def index
if params[:supplier]
#vehicles = Vehicle.all :conditions => { :supplier_id => params[:supplier] }
else
#vehicles = Vehicle.all
end
end
end
Not sure if it's possible to make an alias but at least you can try to swap your routes:
map.trucks '/trucks/by_supplier/:supplier', :controller => :vehicles, :action => :index, :is_truck => true
map.trucks '/trucks', :controller => :vehicles, :action => :index, :is_truck => true
Since routes works like 'first added - has higher priority' your solution could fail since map.trucks '/trucks' catch '/trucks/by_supplier/:supplier' as well. Also I'd recommed to refactor it a bit:
map.with_options :controller => :vehicles, :action => :index, :is_truck => true do |v|
v.trucks '/trucks/by_supplier/:supplier'
v.trucks '/trucks'
end
I recommend you to use another resource, just adding:
map.resources :vehicles, :as => :trucks, :has_many => :suppliers
Then handle it in your controller with something like:
def index
conds = {}
conds = { ... } if request.uri =~ /trucks/ # you can be more specific about the regexp if you need to
#vehicles = Vehicle.all :conditions => conds
end
What do you think about it?