Im fairly new to Laravel, running version 6 atm. I have made a working feature that any registered user can edit their profile.
My issue is that I dont like the url being shown to the user like:
localhost/profile/16/edit
16 = userid
Route file:
Route::put('/profile/edit/{profile}','ProfileController#update')->name('profile.update');
Is there a way to change my route so its only displayed like: profile/edit, if any user has entered? They can enter any other user id in the url but only the auth user info will be show and be possible to edit but it bugs me that the url has an id in it. Any fancy routing method to change that?
Thanks in advance!
Fixed my own issue.
Edited my routes to this:
Route::get('/profile/edit', ['as' => 'profile.edit', 'uses' => 'ProfileController#edit']);
Route::put('/profile/edit', ['as' => 'profile.update', 'uses' => 'ProfileController#update']);
Route::resource('profile', 'ProfileController');
Related
I have a site where you can enter the name of a business after the domain and if it exists it is displayed. E.g. www.mysite.com/BUSINESS_NAME. I do this through a route:
match ':id' => 'businesses#show', :via => [:get]
It essentially means that a new custom URL is created for each new business that signs up. At the moment each time a new business is created (create form submitted) I am redirected to something like
http://localhost:3000/businesses/42
This is expected as I use:
redirect_to #business
How am I able to redirect to the newly created business and make the URL look like
www.mysite.com/BUSINESS_NAME instead of http://localhost:3000/businesses/42
I thought about making logic in the show action that would work for either URL request but it doesn't look very nice to the end user seeing an ID - I am sure there is something in Rails to mask this sort of information and still pass the ID parameter.
Thanks in advance for any help.
Yes, you can make the url as user friendly.So user can see the simple url that dosen't use the id.
There is a gem in rails that easily solve your problem.
Friendly_id
I have a rails 3 application in which users can find objects near them by tapping their address or by clicking on links I generates (looks like "Find objects in Dallas").
For the second way, I generates URLs like this :
www.my_website.com/search?place=Dallas
How can I transform these URLs to looks like this :
www.my_website.com/search/Dallas
It all has to do with the routes you set up.
routes.rb
get 'search/:place' => 'search_controller#search', :as => :seo_search
Use it as such
seo_search_path('Dallas')
From your controller you will get a params[:place] available.
I'm trying to create custom URLs for my site's users:
www.mysite.com/user1
If I set the routing rule to be this:
match ':id' => "user#view", :as => :user
Then it works great! I can access www.mysite.com/user1 and it will pull up the page for user1.
However, that breaks my about page, which is at www.mysite.com/about . The error I get here is:
"Couldn't find User with id=about"
Any idea how I can fix this problem? Thanks! As an aside - I'm using friendlyid to generate the human readable slugs.
Ringo
Is the about page a static file located at public/about.html? If so, then Rails will match this first and fall back to the router if it can't find the file.
If it's a route instead, then your match :id route should be beneath the one for about, so that about is matched first.
My app allows people to create portfolios. I would like for them to be able to connect their domain to their portfolio.
So somedomain.com would show /portfolio/12, someotherdomain.com would show /portfolio/13 and so on. But I don't want the redirect. I want the user to see somedomain.com in the browser url.
How do I do that?
Ok, I've found this solution:
match "/" => "portfolio#show",
:constraints => { :domain => "somedomain.com" },
:defaults => { :id => '1' }
As I don't have many custom domains, this is fine for now but the question is - how to make this dynamic, to read domain and id data from db?
Ok, let's assume you own yourdomain.com and use it as your home page for your application. And any other domain name like somedomain.net is mapped to a portfolio page.
First of all, in your routes.rb you need to catch yourdomain.com and map it to wherever your home page is, so that it stands out from the rest of the crowd.
root :to => "static#home", :constraints => { :domain => "yourdomain.com" }
Then you need to catch any other root on any domain and forward it to your PortfoliosController
root :to => "portfolios#show"
Keep in mind that this line will only be checked if the previous line fails to match.
Then in your PortfoliosController find the requested portfolio by its domain rather than id.
def show
#portfolio = Portfolio.find_by_domain(request.host)
…
end
Of course you may want to rescue from an ActiveRecord::RecordNotFound exception in case the domain is not in your database, but let's leave that for another discussion.
Hope this helps.
First, you should add a field to the portfolio model to hold the user's domain. Make sure this field is unique. Adding an index to the field in your database would also be wise.
Second, set your root to route to the portfolios#show action, as you already did, but without the constraints.
Then, in the PortfoliosController#show method, do the following check:
if params[:id]
#portfolio = Portfolio.find(params[:id])
else
#portfolio = Portfolio.find_by_domain(request.host)
end
After this, the only thing left to do is to make sure your own domain does not trigger the portfolio#show action. This can be done with the constraint you used before, but now with your own domain. Be sure to put this line in routes.rb above the line for the portfolio#show action, since the priority is based upon order of creation.
The request object seems not to be available to the routes.rb file w/o some patching.
There are some plugins that make it available, but most of them seem to be outdated. This one here request_routing seems to be with the latest commit dates so it would be most up to date. Though I doubt it will work with Rails 3.0 out of the box, it is a start and might not be that hard to port.
Your users can set DNS CNAME redirects so that requests for theirdomain.com land on your_app.com/portfolio/12.
Morning Everyone!..
General Routing Quesiton Here... I'm currently working to achieve a route similar to this for users in my application.
http://www.example.com/username
This then maps to the usersControllers#show, hence I have the following in my routes file.
map.connect '/:permalink', :controllers => "users", :action => "show"
I've then got the show action to find the user by the permalink in the param. So its works but....
The problem I'm running into is that all other UNDEFINED routes get sent to userController#show. i.e 404's & other un-named routes. So I dont think i'm going with the right convention for this. My solution is to just add other named routes above this, which solves the problem, but to me seems brittle. Am I thinking about this wrong?
Whats a better solution? I'm going to mine google for answers but I just thought i'd throw this up for discussion. Ideas?
You're doing it right. Rails routes go from high priority at the top to low priority at the bottom. Your users show action should go at the bottom. Just make sure that if the permalink does not correspond to a user a proper 404 is generated.
What if you get a user whose username is the same as other URLs on your site?
This seems like a trouble waiting to happen.
Just change it to http://www.example.com/user/username
This way you create a "user" namespace for all username based URLs.