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
Related
I need to define a method/action in my LessonsController that I can call from the lesson show action that marks the lesson as being completed by the current user. What does that controller method look like?
Here's the overview of my models:
User
has_many :completions
has_many :completed_steps, through: :completions, source: :lesson
Lesson
has_many :completions
has_many :completed_by, through: :completions, source: :user
Completions
belongs_to :user
belongs_to :lesson
My Completions Table looks like this:
t.integer "user_id"
t.integer "lesson_id"
t.boolean "completed_step"
t.string "completed_by"
I'm assuming in the LessonsController it looks like this
def complete_step
self.completions.create(completed_step: true, user_id: current_user.id)
end
Routes info:
Rails.application.routes.draw do
namespace :admin do
resources :users
resources :coupons
resources :lessons
resources :plans
resources :roles
resources :subscriptions
resources :completions
root to: "users#index"
end
devise_for :users, :controllers => { :registrations => "registrations"}
# Added by Koudoku.
mount Koudoku::Engine, at: 'koudoku'
scope module: 'koudoku' do
get 'pricing' => 'subscriptions#index', as: 'pricing'
end
resources :lessons do
member :complete
end
# The priority is based upon order of creation: first created -> highest priority.
# See how all your routes lay out with "rake routes".
# You can have the root of your site routed with "root"
root 'pages#home'
get '/dashboard' => 'pages#dashboard', :as => 'dashboard'
mount StripeEvent::Engine, at: '/stripe-events' # prov
end
Here's my button link to make this functional.
<%= button_to "Mark this Lesson as Complete", complete_lesson_path(#lesson), method: :put, class: "btn btn-warning btn-lg" %>
Will this work or am I WAY off? Thanks!
Keep this is the LessonsController, but change it in either of the following ways:
def complete_step
current_user.completions.create(completed_step: true, lesson_id: #lesson.id)
end
# ~~ OR ~~
def complete_step
#lesson.completions.create(completed_step: true, user_id: current_user.id)
end
Both of these assume that you've already set #lesson in the controller, probably in a before_action :set_lesson.
EDIT:
If you need a route suggestion, then assuming you have resources :lessons in your routes file, you can either use an existing route (likely update) or add a member route like this:
resources :lessons do
get 'complete', on: :member
end
If you add a route, then you will need to add an action that looks like
def complete
complete_step
redirect #lesson
end
or similar, however you want to handle the response itself. You will also need to ensure that #lesson is set, so you should tweak your before_action :set_lesson, only: [:show, :update, ...] to also include :complete
Please try with below code.in your completion controller
def create
#lession = Lession.find(params[:lession_id])
#completion = current_user.completions.create(completed_step: true, lesson_id: #lesson.id)
redirected_to #completion
end
You can also just pass user: current_user to the completions.create method instead of passing in the current_user.id
Something like #lesson.completions.create(completed_step: true, user: current_user)
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) %>.
I'm creating a message-board site using ruby on rails.
I generated two scaffolds: Topic and Forum. Topic belongs_to Forum.
when the user is creating a new topic, I should pass the forum_id (a GET var). something like:
http://example.com:3000/topics/new/1
and then, when the user submit the form he passes back the forum_id with the POST request (through hidden html field?).
what is the right way doing it?
thanks
routes:
resources :forums
get "admin/index"
resources :posts
resources :topics
resources :users
match '/signup', :to => 'users#new'
get '/login', :to => 'sessions#new', :as => :login
match '/auth/:provider/callback', :to => 'sessions#create'
match '/auth/failure', :to => 'sessions#failure'
match '/topics/new/:id', :to => 'topics#new'
A good way to do it is to nest topics resources inside forums resources like this:
resources :forums do
resources :topics
end
Then in your TopicsController
class TopicsController < ApplicationController
def new
#forum = Forum.find params[:forum_id]
#topic = Topic.new
end
def create
#forum = Forum.find params[:forum_id] # See the redundancy? Consider using before_filters
#topic = #forum.topics.build params[:topic]
if #topic.save
redirect_to #topic
else
render action: :new
end
end
end
And finally in your views/topics/_form.html.erb:
<%= form_for [#forum, #topic] do |f| %>
# Your fields
<% end %>
I have a school page that has tabs when clicked upon are suppose to bring up microposts. The issue is I don't think I am routing it correctly and I feel like an idiot trying to figure this out and not succeeding. If anyone has suggestions please feel free to help me out! Thank you so much!
Routes.rb
get "/schools/:id/mostrecent_schools" => "users#microposts", :as => "mostrecent_schools"
School Controller
def mostrecent
#school = School.find_by_slug(request.referer.gsub('http://localhost:3000/','')).id
#microposts = #user.microposts.paginate(:per_page => 10, :page => params[:page])
respond_to do |format|
format.html
format.js
end
end
Tab HTML
li class='StreamTab StreamTabRecent active'>
<%= link_to 'Most Recent', mostrecent_schools_path, :remote => true, :class => 'TabText' %>
</li>
<div id='ContentBody'>
<div id='ajax'></div>
<%= render 'users/microposts', :microposts => #microposts %>
</div>
mostrecent.js
$("#ajax").hide();
$("#ContentBody").html('<%= escape_javascript(render :partial => "users/microposts" )%>');
EDIT
*Routes.rb*
Projects::Application.routes.draw do
resources :pages
resources :application
resources :schools
resources :microposts
resources :comments
resources :users
resources :sessions
resources :password_resets
resources :relationships, only: [:create, :destroy]
resources :users do
member do
get :following, :followers
end
end
resources :microposts do
member do
post :vote_up, :unvote
end
end
resources :microposts do
member do
post :upview
end
end
resources :microposts do
resources :comments
end
get "schools/:page/mostrecent" => "schools#mostrecent", :as => "mostrecent_schools"
root to: "pages#index"
From what I can understand, Your routes.rb should look something like this
My final try
Change your routes.rb to this
get "schools/mostrecent/new/:page" => "schools#mostrecent", :as => "mostrecent_schools"
and in your controller edit this line. If this dosen't work then i give up
#school = School.find_by_slug(request.referer.gsub('http://localhost:3000/','')).params[:page]
Although this is not the restful way of doing stuff and as far as I know since users belong to schools and microposts belong to users you shouldn't define schools microposts and users as simple :resources.
Refer to [Rails Routing Guide ](Refer to http://guides.rubyonrails.org/routing.html)for more details.
trying to get rails routing to "click" and just not getting it
have a project and a task model:
class Task
include Mongoid::Document
field :title, :type => String
has_many :projects
belongs_to :user
end
class Project
include Mongoid::Document
field :title, :type => String
has_and_belongs_to_many :tasks
belongs_to :user
end
I want to "associate" a task with a project
so I have this in the project controller:
def connect
#project = Project.find(params[:id])
#project.tasks_ids.push(params[:task_id])
#project.save
redirect_to project
end
with this route:
resources :projects do
match 'connect/:id' => 'projects#connect', :as => :connect, :via => :put
resources :tasks
end
I cant seem to get this to work in the view:
= link_to 'Associate Task', project_connect_path(#task)
fails with:
No route matches {:controller=>"projects", :action=>"connect"}
resources :projects do
member do
put :connect
end
resources :tasks
end
Your path should look like this:
= link_to 'Associate Task', project_connect_path(#project, :task_id => #task.id), :method => :put
Try to do this:
resources :projects do
member do
put 'connect'
end
resources :tasks
end
or you can write your route above the resources :projects do ...
and here is the link, you can read more about routes there: Rails routes