routes.rb
PASFramework::Application.routes.draw do |map|
resources :users do
collection do
get :index
get :edit
post :update
get :show
end
end
root :to => 'users#index'
end
==================================================================
navigation.rb
# Configures your navigation
SimpleNavigation::Configuration.run do |navigation|
navigation.items do |primary|
primary.item :users, 'Welcome User', root_path do |users|
users.item :edit, 'Edit Profile', edit_users_path
end
end
end
==================================================================
User_controller
class UsersController < ApplicationController
def index
user = current_facebook_user.fetch
#u = User.find_by_facebook_id(user.id)
if #u.nil?
#u = User.find_or_create_by_facebook_id(user.id)
#u.update_attributes( :first_name => current_facebook_user.first_name,
:last_name => current_facebook_user.last_name )
gflash :notice => "Welcome, #{current_facebook_user.name} "
else
gflash :notice => "Welcome back, #{current_facebook_user.first_name} #{current_facebook_user.last_name}"
end
return
rescue Exception
logger.info "Problem"
logger.error($!)
top_redirect_to auth_url
end
def show
end
def edit
user = current_facebook_user.fetch
#user = User.find_all_by_facebook_id(user.id)
end
def update
user = current_facebook_user.fetch
#user = User.find_all_by_facebook_id(user.id)
if #user.update_attributes(:first_name => params[:first_name],
:last_name => params[:last_name])
redirect_to user_path, :notice => t('users.update_success')
else
render :action => 'edit'
end
end
end
==========================================================================
The menu is created really well and thanks for this awesome plugin. But when I click edit user I get an error:
ActionView::Template::Error (No route matches {:action=>"show", :controller=>"users"}):
Can someone help me with that please? Can someone explain me why in routes.rb resources user only is not working instead of the collection thing?
Thanks in advance.
It's because of how you are defining the show route.
resources :users do
collection do
get :index
get :edit
post :update
get :show #this is your problem
end
end
Trying renaming this to a different action. The default show route is getting overridden.
resources :users do
collection do
get :index
get :edit
post :update
get :second_show # of course rename this to a better name
end
end
Related
I have a small game in which after the game is over the user is linked to a view that displays a leader board of all users and their scores. To do this I have treated the link as an update action so that the users score can be updated after the game is over, however, upon clicking the link I get an error saying "param is missing or the value is empty: user". I am also wondering if this is being caused because there is no form to be filled simply a variable being updated.
Controllers:
class UsersController < ApplicationController
def new
#user = User.new
end
def create
#user = User.new(user_params)
if #user.save
session[:user_id] = #user.id
redirect_to '/play'
else
render '/'
end
end
def update
#user = User.find(current_user)
if #user.update(user_params)
redirect_to '/leaderboard'
else
render '/play'
end
end
private
def user_params
params.require(:user).permit(:nick_name, :score)
end
end
class ScoresController < ApplicationController
before_action :require_user, only: [:index]
def index
#user = User.find(current_user)
#score = #user.score
#score = 0
end
def leaderboard
#users = User.all
end
end
View-link:
<div class="game-over"><%= link_to 'Game Over', "/update", :style => 'text-decoration:none; color:white;' %></div>
Routes:
Rails.application.routes.draw do
root 'users#new', as: :users
post '/' => 'users#create'
get '/logout' => 'sessions#destroy'
get '/play' => 'scores#index', as: :user
get '/update' => 'users#update'
get '/leaderboard' => 'scores#leaderboard'
user has to be present in the request params because you required it in user_params. You can change link_to to use query parameters as follows:
link_to "Refresh", {controller: 'users', action: 'update', nick_name: "#{user.nick_name}", score: "#{get_score}"}, style: '...'
Or change update route to contain those parameters in the url.
# routes.rb
get '/update/:nick_name/:score' => 'users#update'
Tip: You probably should change it to PUT and use form instead, since update action alters state in the server.
Looks like whatever you are using to pass through your params to make the request is not properly nested under a user key.
Please help i have tried my best. I really need your help.
So im trying to make a mark order as complete. Now it all works up to the button to mark order as complete. I ran a migration to add.
class AddCompleteToOrder < ActiveRecord::Migration
def change
add_column :orders, :complete, :boolean, default: false
end
end
Then i added a method the order.rb of
def complete!
update(complete: true)
end
Then routes.rb
resources :orders do
post "complete", to: "orders#complete", on: :member
end
Then this is the button
= button_to "Mark as complete", { action: "complete", id: #order.id }
But i dont have a #order.id
but a order does have a #order.name so i changed it to
= button_to "Mark as complete", { action: "complete", name: #order.name }
But then i get the error:
ActionController::UrlGenerationError in Dashboard#dadmin
Showing /Users/jacksharville/Desktop/dancer/app/views/dashboard/dadmin.html.haml where line #87 raised:
No route matches {:action=>"complete", :controller=>"dashboard", :name=>"Order"}
Extracted source (around line #87):
85
86
87
= link_to "Back to Dashboard", :back, :class => 'btn-danger btn'
= button_to "Mark as complete", { action: "complete", name: #order.name }
So clearly im doing the routes.rb wrong but i cant fix it. Please help. Any help greatly appreciated.
routes.rb (full file)
Rails.application.routes.draw do
mount RailsAdmin::Engine => '/admin', as: 'rails_admin'
get 'home/index'
root 'home#index'
#pages
get '/why' => 'pages#why'
get '/trak' => 'pages#trak'
get '/contact' => 'pages#contact'
get '/mydms' => 'pages#mydms'
get '/air' => 'pages#air'
get '/ocean' => 'pages#ocean'
get '/road' => 'pages#road'
get '/courier' => 'pages#courier'
get 'fulfilment' => 'pages#fulfilment'
get 'express' => 'pages#express'
resources :dashboard
get 'dadmin' => 'dashboard#dadmin'
get 'myorders' => 'dashboard#myorders'
get 'label' => 'dashboard#label'
resources "contacts", only: [:new, :create]
devise_for :users
as :user do
get 'users/edit' => 'devise/registrations#edit', :as => 'edit_user_registration'
put 'users' => 'devise/registrations#update', :as => 'user_registration'
end
resources "orders"
get "/confirm" => "confirmations#show"
get 'dconfirmation' => 'orders#confirmation'
resources :orders do
post "complete", to: "orders#complete", on: :member
end
end
orders_controller.rb
class OrdersController < ApplicationController
before_filter :authenticate_user!
def new
#order = Order.new
end
def create
#order = current_user.orders.new(order_params)
#order.email = current_user.email
#order.name = current_user.name
#order.address_line_1 = current_user.address_line_1
#order.address_line_2 = current_user.address_line_2
#order.postcode = current_user.postcode
#order.city = current_user.city
#order.country = current_user.country
if #order.save
redirect_to dconfirmation_path
end
end
def order_params
params.require(:order).
permit(
:email,
:delivery_name,
:company_name,
:delivery_address1,
:delivery_address2,
:delivery_address3,
:delivery_city,
:delivery_postcode,
:delivery_country,
:phone,
:package_contents,
:description_content,
:restricted_items,
:terms_conditions,
:insurance,
:contents_value,
:cf_reference,
:reference_number
)
end
def show
#user = User.find(params[:id])
end
def confirmation
end
def complete!
order = Order.find(params[:id])
order.complete!
# handle response
end
end
dashboard_controller.rb
class DashboardController < ApplicationController
before_filter :authenticate_user!
def index
end
def admindashboard
(current_user.nil?) ? redirect_to(root_path) : (redirect_to(root_path) unless current_user.admin?)
end
def adminuser
(current_user.nil?) ? redirect_to(root_path) : (redirect_to(root_path) unless current_user.admin?)
end
def dadmin
(current_user.nil?) ? redirect_to(root_path) : (redirect_to(root_path) unless current_user.admin?)
# #order = Order.all
#order = Order.order("name").page(params[:page]).per(1)
end
def myorders
#order = current_user.orders.order("name").page(params[:page]).per(1)
end
def show
#user = User.find(params[:id])
end
def label
#order = current_user.orders.order("name").page(params[:page]).per(1)
end
def complete!
order = Order.find(params[:id])
order.complete!
# handle response
end
end
You can specify this additional option in the button_to tag :controller => "order", but the proper way to use button_to tag is to not create a separate hash for options. Instead use it as a button_to("Mark as complete", controller: "order", action: "complete", name: #order.name)
try this
button_to "Mark as complete", { controller: "orders", action: "complete", name: #order.name }, {method: :post}
This link can help: Routing Error - No route matches when using button_to with custom action
rake routes give me:
complete_order POST /order/:id/complete(.:format) orders#complete
You need to move the complete action from DashboardsController to OrdersController, which is where it is defined in the routes file.
Also, you can probably use:
= button_to "Mark as complete", complete_order_path(#order)
The controller action is looking for params[:order_id] so it can find the right order. I am not sure how orders are defined in your view, but you will need to pass an order object to the path.
If by any chance you defined a to_param method in Order class, which defines order's name rather than id as params, you will need to update the complete action to look for an order by name.
But my guess is that it is the default id. So passing the order object to the path should work.
error
uninitialized constant YourSpace::UsersController::User
controller
class YourSpace::UsersController < ApplicationController
def new
#title = YourSpace
end
def edit
#title = YourSpace
#user = User.find(params[:id])
end
def update
name = params[:user][:name]
if name.blank?
flash[:notice] = "Name can not be blank dawg!"
redirect_to :back
else
User.find(params[:id]).update_attributes(params[:user])
# redirect_to :action 'show'
redirect_to :action => :show
# render :action => 'show'
end
end
def index
#title = YourSpace
#users = User.limit(100).order('created_at DESC')
end
def show
#title = YourSpace
#user = User.find(params[:id])
end
end
routes
Rails.application.routes.draw do
root 'site#home'
get '/about', to: 'site#about', as: :about
namespace :your_space do
resources :users
end
namespace :word_cloud do
resources :words, :only => [:index, :create]
end
namespace :word_clock do
resources :page, :only => [:index]
end
namespace :wish do
resources :page, :only => [:index]
end
end
when firing up the rails server, I get error in the YourSpace UsersController where this line of code #users = User.limit(100).order('created_at DESC') is apparently all jacked up. Please know I'm trying to duplicate 180 websites in 365 days which is mostly built using ruby and rails. I'm following closely along the repo to build muscle memory. very much new to learning how to think, and programming.
First thought was wrong.. to solve the problem change User to ::User
My first thought is that you need to put this YourSpace::UsersController in app/controllers/your_space/users_controller.rb. Rails forces directory structure.
I'm getting the following error:
Routing Error
No route matches {:controller=>"tasks", :action=>"complete", :list_id=>1, :id=>nil}
Try running rake routes for more information on available routes.
This is what I have in my routes.rb file:
resources :lists do
resources :tasks
end
match 'lists/:list_id/tasks/:id/complete' => 'tasks#complete', :as => :complete_task
root :to => 'lists#index'
In my tasks_controller:
attr_accessor :completed
before_filter :find_list
def create
#task = #list.tasks.new(params[:task])
if #task.save
flash[:notice] = "Task created"
redirect_to list_url(#list)
else
flash[:error] = "Could not add task at this time."
redirect_to list_url(#list)
end
end
def complete
#task = #list.tasks.find(params[:id])
#task.completed = true
#task.save
redirect_to list_url(#list)
end
private
def find_list
#list = List.find(params[:list_id])
end
And in show.html.erb (where the error occurs):
<%= button_to "Complete", complete_task_path(#list.id,task.id) %>
Can someone please tell me what I'm doing wrong?
What's causing the problem is that task.id in your show view returns nil, while in your routes:
match 'lists/:list_id/tasks/:id/complete' => 'tasks#complete', :as => :complete_task
Requires a task id in order to match the url pattern.
You can read more about it in this blog post.
I just created my first engine. It adds a couple of new routes like so:
Rails.application.routes.draw do
scope :module => 'contact' do
get "contact", :to => 'contacts#new'
get "contact/send_email", :to => 'contacts#send_email', :as => 'send_email'
end
end
Then, in /websites/Engines/contact/app/controllers/contacts_controller.rb, I have:
module Contact
class ContactsController < ApplicationController
# Unloadable marks your class for reloading between requests
unloadable
def new
#contact_form = Contact::Form.new
end
def send_email
#contact_form = Contact::Form.new(params[:contact_form])
if #contact_form.valid?
Notifications.contact(#contact_form).deliver
redirect_to :back, :notice => 'Thank you! Your email has been sent.'
else
render :new
end
end
end
end
I loaded it up in the client app's console to prove to myself some basics were working and quickly got this load error (which I then confirmed by reproducing the issue in a browser):
ruby-1.8.7-p302 > Contact::Form.new
=> #<Contact::Form:0x2195b70>
ruby-1.8.7-p302 > app.contact_path
=> "/contact"
ruby-1.8.7-p302 > r = Rails.application.routes; r.recognize_path(app.contact_path)
LoadError: Expected /websites/Engines/contact/app/controllers/contacts_controller.rb to define ContactsController
And there you have it; /contact gets to the engine's contacts_controller.rb but the fact the controller is inside the module Contact makes it unrecognizable.
What am I doing wrong?
Your app/controllers/contacts_controller.rb is actually defining the Contact::ContactsController, not the ContactsController that Rails expects.
The problem is with your routes, they should be defined like this:
Rails.application.routes.draw do
scope :module => 'contact' do
get "contact", :to => 'contact/contacts#new'
get "contact/send_email", :to => 'contact/contacts#send_email', :as => 'send_email'
end
end
Thanks to #ryan-bigg and #nathanvda their answers in conjunction fixed this issue for me. In short, I ended up using the following routes:
Rails.application.routes.draw do
scope :module => 'contact' do
get "contact", :to => 'contacts#new'
post "contact/send_email", :to => 'contacts#send_email', :as => 'send_email'
end
end
with the following controller:
module Contact
class ContactsController < ApplicationController
def new
#contact_form = Contact::Form.new
end
def send_email
#contact_form = Contact::Form.new(params[:contact_form])
if #contact_form.valid?
Contact::Mailer.contact_us(#contact_form).deliver
redirect_to :back, :notice => 'Thank you! Your email has been sent.'
else
render :new
end
end
end
end
but what seemed to be the final piece was #nathanvda's suggestions to move the contacts_controller from:
/app/controllers/contacts_controller.rb
to
/app/controllers/contact/contacts_controller.rb
Thank you both for your help!