How do I view all of a user's orders in Rails?
.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty{ height:90px;width:728px;box-sizing:border-box;
}
I have made a simple Rails app where users are able to add orders they have made to an Order model. I used Devise and I have been able to work out how to only allow a user to delete and edit their on orders. Now I would like for a user to be able to view all of the orders they have created. A user has many orders and orders belong to users.
I'd like to be able to go to localhost:3000/users/1/orders
and see all of their orders.
Here is my current orders controller:
class OrdersController < ApplicationController
before_action :find_order, only: [:edit, :destroy, :update, :show]
def index
@orders = Order.all.order("created_at DESC")
end
def new
@order = current_user.orders.build
end
def update
if @order.update(order_params)
redirect_to root_path
else
render 'edit'
end
end
def show
end
def create
@order = current_user.orders.build(order_params)
if @order.save
redirect_to root_path
else
render 'new'
end
end
def edit
end
def destroy
@order.destroy
redirect_to root_path
end
private
def order_params
params.require(:order).permit(:start_point, :restaurant_location, :customer_location, :fee)
end
def find_order
@order = Order.find(params[:id])
end
end
Thanks in advance!
ruby-on-rails ruby
add a comment |
I have made a simple Rails app where users are able to add orders they have made to an Order model. I used Devise and I have been able to work out how to only allow a user to delete and edit their on orders. Now I would like for a user to be able to view all of the orders they have created. A user has many orders and orders belong to users.
I'd like to be able to go to localhost:3000/users/1/orders
and see all of their orders.
Here is my current orders controller:
class OrdersController < ApplicationController
before_action :find_order, only: [:edit, :destroy, :update, :show]
def index
@orders = Order.all.order("created_at DESC")
end
def new
@order = current_user.orders.build
end
def update
if @order.update(order_params)
redirect_to root_path
else
render 'edit'
end
end
def show
end
def create
@order = current_user.orders.build(order_params)
if @order.save
redirect_to root_path
else
render 'new'
end
end
def edit
end
def destroy
@order.destroy
redirect_to root_path
end
private
def order_params
params.require(:order).permit(:start_point, :restaurant_location, :customer_location, :fee)
end
def find_order
@order = Order.find(params[:id])
end
end
Thanks in advance!
ruby-on-rails ruby
This was also posted on Reddit: reddit.com/r/rails/comments/9xsdge/…
– Dorian
Nov 17 '18 at 4:41
add a comment |
I have made a simple Rails app where users are able to add orders they have made to an Order model. I used Devise and I have been able to work out how to only allow a user to delete and edit their on orders. Now I would like for a user to be able to view all of the orders they have created. A user has many orders and orders belong to users.
I'd like to be able to go to localhost:3000/users/1/orders
and see all of their orders.
Here is my current orders controller:
class OrdersController < ApplicationController
before_action :find_order, only: [:edit, :destroy, :update, :show]
def index
@orders = Order.all.order("created_at DESC")
end
def new
@order = current_user.orders.build
end
def update
if @order.update(order_params)
redirect_to root_path
else
render 'edit'
end
end
def show
end
def create
@order = current_user.orders.build(order_params)
if @order.save
redirect_to root_path
else
render 'new'
end
end
def edit
end
def destroy
@order.destroy
redirect_to root_path
end
private
def order_params
params.require(:order).permit(:start_point, :restaurant_location, :customer_location, :fee)
end
def find_order
@order = Order.find(params[:id])
end
end
Thanks in advance!
ruby-on-rails ruby
I have made a simple Rails app where users are able to add orders they have made to an Order model. I used Devise and I have been able to work out how to only allow a user to delete and edit their on orders. Now I would like for a user to be able to view all of the orders they have created. A user has many orders and orders belong to users.
I'd like to be able to go to localhost:3000/users/1/orders
and see all of their orders.
Here is my current orders controller:
class OrdersController < ApplicationController
before_action :find_order, only: [:edit, :destroy, :update, :show]
def index
@orders = Order.all.order("created_at DESC")
end
def new
@order = current_user.orders.build
end
def update
if @order.update(order_params)
redirect_to root_path
else
render 'edit'
end
end
def show
end
def create
@order = current_user.orders.build(order_params)
if @order.save
redirect_to root_path
else
render 'new'
end
end
def edit
end
def destroy
@order.destroy
redirect_to root_path
end
private
def order_params
params.require(:order).permit(:start_point, :restaurant_location, :customer_location, :fee)
end
def find_order
@order = Order.find(params[:id])
end
end
Thanks in advance!
ruby-on-rails ruby
ruby-on-rails ruby
asked Nov 17 '18 at 1:07
user10410465
This was also posted on Reddit: reddit.com/r/rails/comments/9xsdge/…
– Dorian
Nov 17 '18 at 4:41
add a comment |
This was also posted on Reddit: reddit.com/r/rails/comments/9xsdge/…
– Dorian
Nov 17 '18 at 4:41
This was also posted on Reddit: reddit.com/r/rails/comments/9xsdge/…
– Dorian
Nov 17 '18 at 4:41
This was also posted on Reddit: reddit.com/r/rails/comments/9xsdge/…
– Dorian
Nov 17 '18 at 4:41
add a comment |
1 Answer
1
active
oldest
votes
I would set it up like so:
resources :users, only: do
resources :orders, module: :users, only: :index
end
This routes /users/:user_id/orders
to Users::OrdersController#index
.
Using the module
option is a nifty trick that lets you disambiguate between nested and non nested resources. Meaning that it will not effect your existing orders index.
Creating the controller itself is very straight forward:
# app/controllers/users/orders_controller.rb
module Users
class OrdersController
# GET /users/:user_id/orders
def index
@user = User.includes(:orders).find(params[:user_id])
@orders = @user.orders
end
end
end
And a just create a view:
# app/views/users/orders/index.html.erb
<table>
<thead>
<tr>
<th>id</th>
<th>created_at</th>
</tr>
</thead>
<tbody>
<% @orders.each do |order|%>
<tr>
<td><%= order.id %></td>
<td><%= order.created_at %></td>
</tr>
<% end %>
</tbody>
</table>
Remember that partials are your friend if you want to share view code with the "normal" index.
I don't understand... I'm new to Rails (and programming) so bear with me. Your example has an orders controller within a users module here:# app/controllers/users/orders_controller.rb
Does this mean i generate a new orders controller? Or do I just add the users module to my existing orders controller?
– user10410465
Nov 22 '18 at 0:18
No - its a separate controller and you don't need to generate it since you already have the code.
– max
Nov 22 '18 at 3:27
Okay, this worked! Although I needed to make sure the controller class was inheriting fromApplicationController
. Thanks!
– user10410465
Nov 22 '18 at 15:37
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53347275%2fhow-do-i-view-all-of-a-users-orders-in-rails%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
I would set it up like so:
resources :users, only: do
resources :orders, module: :users, only: :index
end
This routes /users/:user_id/orders
to Users::OrdersController#index
.
Using the module
option is a nifty trick that lets you disambiguate between nested and non nested resources. Meaning that it will not effect your existing orders index.
Creating the controller itself is very straight forward:
# app/controllers/users/orders_controller.rb
module Users
class OrdersController
# GET /users/:user_id/orders
def index
@user = User.includes(:orders).find(params[:user_id])
@orders = @user.orders
end
end
end
And a just create a view:
# app/views/users/orders/index.html.erb
<table>
<thead>
<tr>
<th>id</th>
<th>created_at</th>
</tr>
</thead>
<tbody>
<% @orders.each do |order|%>
<tr>
<td><%= order.id %></td>
<td><%= order.created_at %></td>
</tr>
<% end %>
</tbody>
</table>
Remember that partials are your friend if you want to share view code with the "normal" index.
I don't understand... I'm new to Rails (and programming) so bear with me. Your example has an orders controller within a users module here:# app/controllers/users/orders_controller.rb
Does this mean i generate a new orders controller? Or do I just add the users module to my existing orders controller?
– user10410465
Nov 22 '18 at 0:18
No - its a separate controller and you don't need to generate it since you already have the code.
– max
Nov 22 '18 at 3:27
Okay, this worked! Although I needed to make sure the controller class was inheriting fromApplicationController
. Thanks!
– user10410465
Nov 22 '18 at 15:37
add a comment |
I would set it up like so:
resources :users, only: do
resources :orders, module: :users, only: :index
end
This routes /users/:user_id/orders
to Users::OrdersController#index
.
Using the module
option is a nifty trick that lets you disambiguate between nested and non nested resources. Meaning that it will not effect your existing orders index.
Creating the controller itself is very straight forward:
# app/controllers/users/orders_controller.rb
module Users
class OrdersController
# GET /users/:user_id/orders
def index
@user = User.includes(:orders).find(params[:user_id])
@orders = @user.orders
end
end
end
And a just create a view:
# app/views/users/orders/index.html.erb
<table>
<thead>
<tr>
<th>id</th>
<th>created_at</th>
</tr>
</thead>
<tbody>
<% @orders.each do |order|%>
<tr>
<td><%= order.id %></td>
<td><%= order.created_at %></td>
</tr>
<% end %>
</tbody>
</table>
Remember that partials are your friend if you want to share view code with the "normal" index.
I don't understand... I'm new to Rails (and programming) so bear with me. Your example has an orders controller within a users module here:# app/controllers/users/orders_controller.rb
Does this mean i generate a new orders controller? Or do I just add the users module to my existing orders controller?
– user10410465
Nov 22 '18 at 0:18
No - its a separate controller and you don't need to generate it since you already have the code.
– max
Nov 22 '18 at 3:27
Okay, this worked! Although I needed to make sure the controller class was inheriting fromApplicationController
. Thanks!
– user10410465
Nov 22 '18 at 15:37
add a comment |
I would set it up like so:
resources :users, only: do
resources :orders, module: :users, only: :index
end
This routes /users/:user_id/orders
to Users::OrdersController#index
.
Using the module
option is a nifty trick that lets you disambiguate between nested and non nested resources. Meaning that it will not effect your existing orders index.
Creating the controller itself is very straight forward:
# app/controllers/users/orders_controller.rb
module Users
class OrdersController
# GET /users/:user_id/orders
def index
@user = User.includes(:orders).find(params[:user_id])
@orders = @user.orders
end
end
end
And a just create a view:
# app/views/users/orders/index.html.erb
<table>
<thead>
<tr>
<th>id</th>
<th>created_at</th>
</tr>
</thead>
<tbody>
<% @orders.each do |order|%>
<tr>
<td><%= order.id %></td>
<td><%= order.created_at %></td>
</tr>
<% end %>
</tbody>
</table>
Remember that partials are your friend if you want to share view code with the "normal" index.
I would set it up like so:
resources :users, only: do
resources :orders, module: :users, only: :index
end
This routes /users/:user_id/orders
to Users::OrdersController#index
.
Using the module
option is a nifty trick that lets you disambiguate between nested and non nested resources. Meaning that it will not effect your existing orders index.
Creating the controller itself is very straight forward:
# app/controllers/users/orders_controller.rb
module Users
class OrdersController
# GET /users/:user_id/orders
def index
@user = User.includes(:orders).find(params[:user_id])
@orders = @user.orders
end
end
end
And a just create a view:
# app/views/users/orders/index.html.erb
<table>
<thead>
<tr>
<th>id</th>
<th>created_at</th>
</tr>
</thead>
<tbody>
<% @orders.each do |order|%>
<tr>
<td><%= order.id %></td>
<td><%= order.created_at %></td>
</tr>
<% end %>
</tbody>
</table>
Remember that partials are your friend if you want to share view code with the "normal" index.
edited Nov 17 '18 at 3:01
answered Nov 17 '18 at 2:53
maxmax
47k1060106
47k1060106
I don't understand... I'm new to Rails (and programming) so bear with me. Your example has an orders controller within a users module here:# app/controllers/users/orders_controller.rb
Does this mean i generate a new orders controller? Or do I just add the users module to my existing orders controller?
– user10410465
Nov 22 '18 at 0:18
No - its a separate controller and you don't need to generate it since you already have the code.
– max
Nov 22 '18 at 3:27
Okay, this worked! Although I needed to make sure the controller class was inheriting fromApplicationController
. Thanks!
– user10410465
Nov 22 '18 at 15:37
add a comment |
I don't understand... I'm new to Rails (and programming) so bear with me. Your example has an orders controller within a users module here:# app/controllers/users/orders_controller.rb
Does this mean i generate a new orders controller? Or do I just add the users module to my existing orders controller?
– user10410465
Nov 22 '18 at 0:18
No - its a separate controller and you don't need to generate it since you already have the code.
– max
Nov 22 '18 at 3:27
Okay, this worked! Although I needed to make sure the controller class was inheriting fromApplicationController
. Thanks!
– user10410465
Nov 22 '18 at 15:37
I don't understand... I'm new to Rails (and programming) so bear with me. Your example has an orders controller within a users module here:
# app/controllers/users/orders_controller.rb
Does this mean i generate a new orders controller? Or do I just add the users module to my existing orders controller?– user10410465
Nov 22 '18 at 0:18
I don't understand... I'm new to Rails (and programming) so bear with me. Your example has an orders controller within a users module here:
# app/controllers/users/orders_controller.rb
Does this mean i generate a new orders controller? Or do I just add the users module to my existing orders controller?– user10410465
Nov 22 '18 at 0:18
No - its a separate controller and you don't need to generate it since you already have the code.
– max
Nov 22 '18 at 3:27
No - its a separate controller and you don't need to generate it since you already have the code.
– max
Nov 22 '18 at 3:27
Okay, this worked! Although I needed to make sure the controller class was inheriting from
ApplicationController
. Thanks!– user10410465
Nov 22 '18 at 15:37
Okay, this worked! Although I needed to make sure the controller class was inheriting from
ApplicationController
. Thanks!– user10410465
Nov 22 '18 at 15:37
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53347275%2fhow-do-i-view-all-of-a-users-orders-in-rails%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
This was also posted on Reddit: reddit.com/r/rails/comments/9xsdge/…
– Dorian
Nov 17 '18 at 4:41