how can I change url style in rails 4 app with mongoid - mongoid

I am using Mongoid.
now my rails app url like this http://localhost:3000/posts/523605c9d9d834ee00000002
How can I change the url style like http://localhost:3000/posts/1

What you can do is not use the default BSON::ObjectId for id on your posts collection.
I had a similar use case that needed a numeric id for my collection, and I created this gem which makes easier to have this functionality.
You can check it out, in here.
https://github.com/arthurnn/mongoid_integer_id

Related

How to use one Route for a controller to hit all actions in Laravel 8

I switched to Laravel 8 from CakePhp and I would like to know if it's possible to use only one route for all the actions in a controller. I found "Implicit Controllers" for Laravel 4.2 but it's not working on version 8.
Is not possible to use one route for all actions. However what you can do is use the resource function so that laravel will automatically generate the routes using the CRUD model. You can find the documentation in: https://laravel.com/docs/9.x/controllers#resource-controllers. So, you could declare the route as:
Route::resource('photos', PhotoController::class);
And that will generate:
Route::get('/posts', 'PostController#index');
Route::get('/posts', 'PostController#create');
Route::post('/posts', 'PostController#store');
Route::get('/posts/{postId}', 'PostController#show');
Route::get('/posts/{postId}/edit', 'PostController#edit');
Route::put('/posts/{postId}', 'PostController#update');
Route::delete('/posts/{postId}', 'PostController#destroy');
You can use just this Route:
Route::resource('crud','App\Http\Controllers\CrudsController');
for these function :
index/create/store/show/edit/update/destroy

Wrong 'View live' url in Wagtail admin message after page creation when using id as slug

I am using a page model 'EventPage', which shall have a slug based on the id of the page. To achieve this, I have modified the full_clean method like this (similar to this question):
class EventPage(Page):
...
def full_clean(self, *args, **kwargs):
super().full_clean(*args, **kwargs)
# set slug to id, if already existing
if self.id is not None:
self.slug = str(self.id)
This seems to work fine in principle. However, after the page is published,the Wagtail admin/pages view shows a message box at the top ('Page ... created and published') with a View live button that links to the wrong url (i.e. using the default slug created from the page title).
In the list of pages below that, the just created page's own View live and Add child page links show the correct page url using the page id. It is just the message box's View live url at the top that needs to be corrected. Here's a screenshot:
How can I get the correct link in the message box at the top as well?
In case it matters, I am currently using Wagtail 2.9, Django 2.2.9 and Python 3.6.
I guess my problem has something to do with the fact that the page id is not known until the page is saved for the first time, and the View live link in the message box somehow uses an initial page.url which is overridden later on.
Any ideas how to solve this?
You could probably achieve this by using Hooks in Wagtail. Hooks are typically used to customize the view-level behaviour of the Wagtail admin and front end. I would suggest overwriting the button functionality this way, or possibly removing the existing button and adding your own new one in. More info in the docs: https://docs.wagtail.io/en/latest/reference/hooks.html
For anyone ending up here, the solution was given in the accepted answer to a similar question here: Wagtail 'View live' button provides wrong url after page creation while using id as slug.
In short: use Django signals instead of hooks, i.e. here a the post_save signal for EventPage.
For documentation purposes, I'm repeating here the solution from the linked question, slightly adjusted. All credits and thanks go to gasman.
Create following signal in the calendar app, calendar/signals.py:
from django.db.models.signals import post_save
from django.dispatch import receiver
from .models import EventPage
#receiver(post_save)
def set_number_and_slug_after_event_page_created(sender, instance, created, **kwargs):
if issubclass(sender, EventPage) and created:
page = instance
page.number = page.slug = str(page.id)
page.save()
new_revision = page.save_revision()
if page.live:
new_revision.publish()
And, if not yet done, register signals in the corresponding calendar/apps.py config:
from django.apps import AppConfig
class CalendarConfig(AppConfig):
name = 'calendar'
def ready(self):
from . import signals
This worked like a charm for me.

Bootstrap Typeahead Async - multiple values

Im trying to implement Bootstrap Typeahead in my AngularJS project and I came across an issue with values.
Im loading the content via $http from my Django API server. For now, I can lookup for any item I want and display it's name, but what I need is to display "title" but return "id" via ng-model back to the controller.
Do you have any working example of doing this?
http://pastebin.com/xtype9J4
I'm assuming you are using https://angular-ui.github.io/bootstrap/#/typeahead, so I'd suggest having a look at the last example.
Looking at the DOM, your code could look something like this:
uib-typeahead="company as company.name for company in getCompanies($viewValue)"
This pretty much contains exactly what you need. Additionally, take a look at https://docs.angularjs.org/api/ng/directive/select and https://docs.angularjs.org/api/ng/directive/ngOptions for further examples, as AngularUI has a similar (if not identical) approach.

cake 2.3.x create menu

I need to help with add menu to my apps.
I use cake 2.3.8. I tried to use MenuHelper from this article http://bakery.cakephp.org/articles/alkemann/2009/02/04/menuhelper.
I add this class to lib>Cake>View as MenuHelper.php
Next I added from AppsController $helpers =>array('Menu');
But I don't know what I should to do next. I tried use
$menu->add('link_list','title','url');
in my view or layout but cake doesn't view $menu variable. What I do wrong?
You're using the CakePHP 1.x syntax in your snippet, hence it doesn't work. In CakePHP 2.x, helpers are exposed as a property in the view and you have to use $this->Menu->add('link_list','title','url');.

cakephp pagination: how to set a different 'page' parameter

Is there any way to configure a different param for the current page number?
for example, I have this paginated url:
http://localhost/petproject/posts/index/page:2
And I would like to have the url like this:
http://localhost/petproject/posts/index/my_custom_page_param:2
Thank you!
You could simply use the router to transform the URL into something else that matches your needs by adding a rewrite rule for that URL. Without researching it further I think this is the most easy solution.
Another one might be to check in Controller::beforeFilter() if your custom named arg is set and copy/set it to params['named']['page'].
Or extend the Paginator component and create your customized version of it.

Resources