Only getting the index for the current user - ruby-on-rails

I have a pretty basic app. I've managed to install devise. The idea is to have users who have created challenges (1 to many relationship).
I want the logged in user to be able to see all the challenges they have created.
I order to do this am I correct in thinking that I can pass the current user id as a parameter to just get the challenges of the current user as follows (assuming the view is set up correctly)
<%= link_to challenges_path(user_id: current_user.id), class: 'expandable' %>
challenges controller
def index
#challenges = Challenge.all
render :layout => false
end

If this is the default behaviour you want for the index of the challenges, then you can simply change your controller action directly, and no need to modify your link_to to add user_id
challenges controller
def index
#challenges = Challenge.where(user: current_user)
end
Now if you want to change the behavior only if the user_id GET param is set, you can keep your link_to like this and modify your controller this way
def index
#challenges = params[:user_id] ? Challenge.where(user: current_user) : Challenge.all
end

Related

Routing to different instances of Index using a Helper Method

I have a controller called BookingsController with a bookings#index action. Inside the index action, there are 2 instance variables, #pending_bookings and #approved_bookings, which query Booking objects by their status.
def index
#pending_bookings = Booking.where(host_id:#user.id,
status:'pending')
#approved_bookings = Booking.where(host_id:#user.id,
status:'approved')
end
I want to route the user to a different instance of index depending on the link they click. Basically bookings_path(#pending_bookings) should route the user to the index page displaying all pending_bookings, adversely, bookings_path(#approved_bookings) should route the user to the index page displaying all approved_bookings.
In my view, I have 2 links that should direct the user to each path respectively.
<%= link_to 'Pending Reservations', bookings_path(#pending_bookings)%>
<%= link_to 'Approved Reservations', bookings_path(#approved_bookings)%> `
The index.html.erb file:
<%= booking_index_helper_path %>
contains an embedded helper method that should recognize the path the user clicks and render the proper Booking objects.
Here's the (flawed) logic for recognizing the path the user chooses and rendering the necessary objects:
pages_helper.rb:
def booking_index_helper_path
if bookings_path(#pending_bookings)
render #pending_bookings
elsif bookings_path(#approved_bookings)
render #approved_bookings
else bookings_path(#total_bookings)
#total_bookings
end
end
I put a binding.pry in the helper method to confirm it is being hit (it is). For some reason when I click the link to direct me to the proper objects, however, the first condition is always satisfied. What is a better way to write this conditional to recognize the path the user chooses?
It seems like you're going about this in a more complicated way than you need to. Why not just have an index like:
def index
#Rails autoescapes this string so no fear of sql injection using user supplied strings
#bookings = Booking.where(host_id:#user.id, status: "#{params[:status]}")
end
Then use a link like:
<%= link_to 'Pending Reservations', bookings_path(status: 'pending')%>
<%= link_to 'Approved Reservations', bookings_path(status: 'approved')%> `
Now your view can just handle #bookings and not concern itself with the types of #bookings as that is done by the logic in your controller. This is the bare minimum but you should get in the habit of adding error messages etc. to your controllers so consider doing:
def index
if params[:status].present?
#Rails autoescapes this string so no fear of sql injection using user supplied strings
#bookings = Booking.where(host_id:#user.id, status: "#{params[:status]}")
flash[:success] = "#{params[:status].titleize} Bookings loaded."
redirect_to whatever_path
else
flash[:error] = "Something went wrong"
redirect_to some_path
end
end

How to look check whether model exists and create if it doesn't with a single form?

I'm trying to implement a form with the following behavior:
1) Input some semantic data about a user (i.e. username).
2) Do a User.find_by(:username = username).
3) If such a user exists, direct to show page for that user.
4) If such a user does not exist, create a new user with the provided username, then redirect to the show page for that user.
This should be simple but I cannot figure out how to format the form_for helper and my show and create actions in my user_controller to implement this behavior.
I currently have:
form_with :url => 'users/:id', :method => :get do
...
end
because I'm ultimately trying to invoke the "show" method of the controller. However, my form does not take in a user's id as a parameter, and when the user does not yet exist there is no :id parameter to access at the time of the form's submission.
How can I set up my form to redirect to show in each case, while still adhering to the logic explained above?
You can do something like this in your User's Controller create action
def create
usr_name = params[:username]
#user = User.where(username: usr_name).first_or_initialize
if #user.persisted?
redirect_to user_path(#user) # or whatever your user show path is
elsif #user.save
redirect_to user_path(#user)
else
render :new
end
end
You would just need to make sure that you are validating the uniqueness of usernames.
Also, first_or_initialize(and its counterpart first_or_create) can take in a block. So, you can assign other attributes to the new User like this...
User.where(username: usr_name).first_or_initialize do |usr|
usr.some_attribute = some_value
end
you can use find_or_initialize_by(unique_key) in your create method.unique_key can be any key which you are using to identify your user such as email,phone etc.

Action show to find value from hash instead of id

I am still learning rails and have done a lot of readings, but I am not very clear about how params, 'show' actions work yet.
For example we have UsersController, 'index' action is showing all the users with the code #user = User.all, and 'show' action is looking into each users, by using the code #user = User.find(params[:id])
I understand that they are all from the database, where User is a model.
However in my scenario, what if the data I am showing in views, doesn't go through database, instead in the 'index' action it is something like this -
#user = [{name => "alex"}, {name => "peter"}, {name => "john"}]
and in my 'show' action, how can I write the code so that it finds the users by name?
In your Rails app, the data that you show in your views, do not necessarily have to come from/through the database. You can always show any data you want in your views.
For example, in your index action, if you have this:
#users = [{name => "alex"}, {name => "peter"}, {name => "john"}]
Then, in your index view, you can show only those users by looping through the #users instance variable.
Same for show page as well.
If you want to show the users by name in your show page, you have to set the users by name in an instance variable e.g. #users_by_name:
#users_by_name = User.find_by(name: user_name)
# or you can hard code the values if you want like index action
and then this #users_by_name instance variable will be available in your show view so that you can loop through that and show the user names.
Originally, the show page is designed for showing a particular user related information, but you can show whatever information you want going against the conventions.
To be able to have a route like this: localhost:3000/users/alex that will show the user alex's information, you can add a route in your routes.rb file:
get 'users/:name', to: "users#show"
And, in your controller's show action, something like this:
def show
#user = User.find(params[:name])
end
Then, show the #user information in your view page.
P.S. This is not a good idea to find user by name as there might be more than one user with same name in the database and it will create conflict/ or give wrong data in such situations.
In show action , we search the user specific record not all.
So , we have to provide some unique identifiers as parameters to find the specific record.
For eg. Your view should be similar to the params we are passing as below:
<% #user.each do |user| %><br>
<%= link_to user.name, user_show_path+"?name="+user.name %><br>
<% end %><br>
In show action , write the code
def show
#user = User.find_by(:name => params[:name])
end
Also in routes.rb , write the below code:
get 'users/:name', to: "users#show"
For the above solution, make sure that name field will be unique.
My original question is that if it is possible for 'show' action not to go through database
Sure.
Your show action can be the following if you wanted it to:
#app/controllers/users_controller.rb
class UsersController < ApplicationController
def show
#user = "me"
end
end
You really don't have to do anything specific in your application, Rails is just a framework and has certain conventions if you want it to work efficiently.
What you're asking is if you can populate your #user object from a third party set of data...
... Yes you can ...
The way to do it would be in the model, not the controller:
#app/models/user.rb
class User < ActiveRecord::Base
# populates from Hash
end
You'd then be able to populate the data in the controller from the model again:
#app/controllers/users_controller.rb
class UsersController < ApplicationController
def show
#user = User.__________ #-> pull from your hash
end
end
finds the users by name
That's simple - just pass the name through the url: url.com/users/marine_lorphelin
This will set the :id parameter to marine_lorphelin, with which you'll be able to look up the name through your model:
#app/controllers/users_controller.rb
class UsersController < ApplicationController
def show
#user = User._______
end
end
If you were using a database with your user model, you'd be able to use the following:
def show
#user = User.find_by name: params[:id]
end
Since you're not, you'll have to attach your XML hash to your model somehow. This, I don't know without specifics such as where you're getting your data from, how you're accessing it, and which routes you're going to send to invoke it.

Redirecting to different actions without using a query string

I am trying to figure out the best way to do the following (there are a few ways I can think of, but I want to know what the best way to handle it is):
A user is putting together a shipment, and then clicks the "Send" link, which sends him to the /shipments/:id/confirm page. The confirm action checks to see if the user has a completed ShippingAddress; if not, it sends him to the ShippingAddress#new. (If he does, it render the confirm page.
I want the user to be able to complete the ShippingAddress#new page, submit it, and then be redirect back to the /shipments/:id/confirm. How can I do that? How can I pass the :id to the ShippingAddress#new page without doing something like redirect_to new_shipping_address_path(shipment_id: #shipment.id) in the Shipment#confirm action? Or is that the best way to do that?
class ShipmentsController < ApplicationController
def confirm
#shipment = Shipment.where(id: params[:id]).first
unless current_user.has_a_shipping_address?
# Trying to avoid having a query string, but right now would do the below:
# in reality, there's a bit more logic in my controller, handling the cases
# where i should redirect to the CardProfiles instead, or where I don't pass the
# shipment_id, and instead use the default shipment.
redirect_to new_shipping_address_path(shipment_id: #shipment.id)
end
end
end
class ShippingAddressesController < ApplicationController
def new
#shipment = Shipment.where(id: params[:shipment_id]).first
end
def create
#shipment = Shipment.where(id: params[:shipment_id]).first
redirect_to confirm_shipment_path(#shipment)
end
end
[In reality, there is also a CardProfiles#new page that needs to be filled out after the shipping address is].
Try calling render instead of redirect_to, and set the id into an instance variable. Adjust the view logic to pull that instance variable if it exists.
#shipment_id = #shipment.id
render new_shipping_address_path
In the view
<%= form_for #shipment_address do |f| %>
<% if #shipment_id %>
<%= hidden_field_tag :shipment_id, #shipment_id %>
<% end %>
I don't know your view logic entirely, but giving an example.

How do I use instance variables, defined in the controller, in the view with ActiveAdmin?

I have this:
ActiveAdmin.register User do
controller do
def show
#user = User.find(params[:id])
show!
end
end
show do
attributes_table do
row "User" do
link_to #user.display_name, user_path(#user.slug)
end
end
end
end
But when I load the page, I get an error saying:
undefined method `display_name' for nil:NilClass
which means that #user is nil. I am positive that #user is set appropriately (meaning the finder is getting appropriate data that exists in the db). I'm thinking it has something to with how ActiveAdmin works that I'm unfamiliar with. Any thoughts?
Also, I know I could do show do |user|, but there are more complicated things I am using this for and need access to the user object in the controller.
Just in case someone else stumbles upon this:
controller.instance_variable_get(:#user)
should work as well.
There is controller in active admin, despite this you can not pass instance variable to arbre part. But you can use params hash for this:
ActiveAdmin.register User do
controller do
def show
params[:user] = User.find(params[:id])
show!
end
end
show do
attributes_table do
row "User" do
link_to params[:user].display_name, user_path(params[:user].slug)
end
end
end
end
P.S.: If you don't want to change params, then all instance variables are stored in #arbre_context.assigns. You may also do like:
link_to #arbre_context.assigns[:user].display_name, user_path(#arbre_context.assigns[:user].slug)
Instance variables are defined as helper methods. If you have that defined in your controller, you can access it. Alternatively, you can simply call resource, which will have reference to the active record object.
ActiveAdmin.register User do
controller do
def show
#user = User.find(params[:id])
show!
end
end
show do
attributes_table do
row "User" do
# note that your have access to `user` as a method.
link_to user.display_name, user_path(user.slug)
end
end
end
end
It seems does not work that way in activeadmin. The only instance variable available inside "form" block is #config.
The best way to solve this issue is to use partials as described in "Customizing the Form"
http://activeadmin.info/docs/5-forms.html
Not entirely sure how selects the correct instance variable on the model, but, you could give pretty much any name to the instance variable, i test some cases and it seems that just looks for the one that haves the same model type when you don't specify it, to answer your other question, you have many ways to do it
the simples one, just the same name as your instance variable,
in your case,
row :attr do
link_to user.display_name, admin_user_path(user)
end
the you have
row :attr do |any_name|
link_to any_name.display_name, admin_user_path(any_name)
end
and the last method i know, you have two escenarios, one for your active_admin files(.rb)
#eg: admin/user.rb
#arbre_context.assigns[:user]
or in custom .arb views, like a form for a custom collection_action(same but direct access)
assigns[:user]
eg:
#views/admin/users/new_invitation.html.arb(arbre) or html.erb
active_admin_form_for assigns[:user], :url => send_invitation_admin_users_path do |user|
....
end
form_for assigns[:user], :url => send_invitation_admin_users_path do |user|
....
end
semantic_form_for assigns[:user], :url => send_invitation_admin_users_path do |user|
.....
Like i say, i'm not sure how active_admin deals with instance variables, but a least you have multiple options, regards
If your goal is to set #user for the show action template, it's not necessary to do so, because active admin is already doing this for you.
If you use member_action, the #user object is there for you, it's called resource.
You can define a singleton method on your resource, it would be available in the view. This could make sense in some cases.
This is another way to pass information from the controller to the view.
member_action :show, method: :get do
resource.instance_eval do
define_singleton_method('language') do
'English'
end
end
end
show do
attributes_table do
row :name
row :email
row :id
row :language
end
end

Resources