I'm looking to put the users ID within my applications URL. I have devise all set up with a default configuration, what I'm trying to accomplish is to have a URL when the user is logged in as the following:
application.com/userid/page
and when they're not logged in
application.com/page
Is this possible with Devise?
Thanks in advance.
You just need to create a route that looks for an :id param, then make sure you pass an :id param when navigating to the route. Here's a simple example:
routes.rb
get '/page', to: 'pages#page', as: :guest_page
get '/:id/page', to: 'pages#page', as: :user_page
page.html.erb
<% if user_signed_in? %>
<%= link_to "page for logged in user", user_page_path(current_user) %>
<% else %>
<%= link_to "page for guest user", guest_page_path %>
<% end %>
Also, if have many routes, you will want to extract this logic outside the view into a helper method or decorator.
Actually is more routing than devise.
Scope your routes
scope path: ":account_id", as: "account" do
resources :projects
end
http://api.rubyonrails.org/classes/ActionDispatch/Routing/Mapper/Scoping.html
Related
I have a place model and a user model and a user_place model, user_place belongs to user and place both. Traditional has_many through association.
I have a page where you can view the users associated with a place. My routes look like:
resources :places do
resources :user_places
end
which generates these routes:
place_user_places GET /places/:place_id/user_places(.:format) user_places#index
POST /places/:place_id/user_places(.:format) user_places#create
new_place_user_place GET /places/:place_id/user_places/new(.:format) user_places#new
edit_place_user_place GET /places/:place_id/user_places/:id/edit(.:format) user_places#edit
place_user_place GET /places/:place_id/user_places/:id(.:format) user_places#show
PATCH /places/:place_id/user_places/:id(.:format) user_places#update
PUT /places/:place_id/user_places/:id(.:format) user_places#update
DELETE /places/:place_id/user_places/:id(.:format)
I don't love this but I'm ok with it for now.
But whenever I try to delete a user_place I have all sorts of issues.
<%= link_to "delete", place_user_place_url(place_id: #user_place.place_id, id: #user_place.id), method: 'delete' %>
No route matches {:action=>"show", :controller=>"user_places", :id=>nil, :place_id=>2}, possible unmatched constraints: [:id]
I had this working previously with slightly different routes and an actual form:
resources :places do
resources :user_places, as: 'user', only: %i[index create new]
delete 'remove_user', to: 'user_places#remove_user'
end
<% if user != current_user %>
<%= form_with model: #user_place, url: place_remove_user_path(#place.id), method: 'delete' do |form| %>
<%= form.hidden_field :user_id, value: user.id %>
<%= form.hidden_field :place_id, value: #place.id %>
<%= form.submit "delete" %>
<% end %>
<% end %>
But this feels hacky, I don't think I should need a specific form, and this was leading the form to be submitted with javascript which I don't want.
What might be a solution is to use shallow nesting in the routes (shallow: true).
resources :places do
resources :user_places, shallow: true
end
Make sure to run rails routes again. The delete method of a user_place will no longer be nested.
You can then simply delete the user_place passing a single variable (an instance of a user place, #user_place). There is no need to set the id (place_id or id) as Rails is smart enough to handle that. Just passing an instance variable is enough for the delete method to find the corresponding record.
<%= link_to "delete", user_place_url(#user_place), method: 'delete' %>
I have a sponsor which can have many warranty management urls. I have implemented the sponsor form but having trouble to create urls child form. Problem is I need to create child in new page and show the list back to the main form. How do I do it?
My form:
<%= form_for #sponsor, url: polymorphic_path([:a, #sponsor]) do |form| %>
<%= form.file_field :logo %>
<%= link_to "Add Warranty Service URL", new_a_sponsor_warranty_management_url_path(#sponsor), class: 'button green right' %>
<% end %>
Routes:
resources :sponsors do
resources :warranty_management_urls, only: [:new,:edit,:create,:update,:destroy]
end
Controller:
def new
#sponsor = Sponsor.new
end
Currently this error pops up:
No route matches {:action=>"new", :controller=>"a/warranty_management_urls", :sponsor_id=>nil}, possible unmatched constraints: [:sponsor_id]
It looks like your route helper is incorrect. The documentation for resource route helpers may be helpful for your situation: https://guides.rubyonrails.org/routing.html#path-and-url-helpers
Have you tried running rake routes to get the list of available routes and route helpers? I imagine you need something more like:
<%= link_to "Add Warranty Service URL",
new_warranty_management_url_path,
class: 'button green right' %>
You can pass a sponsor_id param as an argument to the path helper, but that won't work unless the sponsor has been saved, which isn't true for a new record.
I'm using Rails 4.0.10. I have a show and edit view for a model. The show url is just model/id, and the edit url is model/id/edit. What I can't figure out is how to create a link on the show page that redirects to the edit page.
Here's what I've tried so far:
<%= link_to "edit", model_edit_path(model) %>
The correct way is (swap model and edit around) :
<%= link_to 'Edit', edit_model_path(model) %>
The RESTful default routes are the following:
#index => models_path
#new => new_model_path
#edit => edit_model_path(:id)
#show => model_path(:id)
If you want to see all your available routes, use rake routes in your console
I'm currently using devise with the following link_to url to "submit" a Resource (resources_controller, resource.rb model)
This is in the menu:
<li><%= link_to "Submit Resource", :action => 'new', :controller => 'resources' %></li>
It works fine if I'm not on a devise login page (user signup, user login, etc)
otherwise it changes the url from /resources/new
to this:
http://localhost:3000/assets?action=new&controller=devise%2Fresources
The only reason i linked it using the first piece of code above is because I'm not sure if theres a better way to link to a particular REST action directly for a given controller (I'm not using :index)
Use <%= link_to "Submit Resource", new_resource_path %>. Before use it you should make sure if you have line resources :resources line in your routes.rb
I need create a link to users/id/microposts where id is the user id. I was wondering how I could do that with link_to?
The HTML code is I am using for the moment is :
<strong>Microposts</strong> <%= user.microposts.count %>
Which renders the following :
Microposts
In routes.rb, you should have something like this:
match '/users/:id/microposts' => 'microposts#index_by_user', :as => :microposts_by_user
and in view, you can do like this:
<%= link_to "Microposts", microposts_by_user_path( #user) %>
Assuming that you have your routes setup correctly, that would be a nested route for the microposts, In rails 3 it would be
resources :users do
resources :microposts
end
Then in your view, with link_to you can
<%= link_to "Microposts", user_microposts_path(user) %>
This might be current_user, #user ... whatever user object you want the microposts, it might be from
#users.each do |user|
<%= link_to "Microposts", user_microposts_path(user) %>
end
OR
<%= link_to "Microposts", user_microposts_path(current_user) %>
EDIT: Added in the user object I forgot to passin and some examples with it.