Where to configure routes and paths in Ruby on Rails? - ruby

Probably a noob question but I'm new to Rails and building my first app as a project to complete my apprenticeship.
I have four pages listing contact, account, billing plan and call information. Each have that standard edit, show and create functions.
I have a table that pulls information from FreeAgent using the API, It just lists basic information (price, due date etc.) I want to create another page so when the user clicks on the invoice they go to another page containing a PDF and more information.
So instead of the path being /accounts/'user id' I want it to be /accounts/'user id'/invoice
Do I need to create another controller or just define a new method in the accounts controller?
Cheers
Arran

I suggest you read the RoR Guide on Routing first, especially the section on nested resources.
Your routes in routes.rb probably look something like this:
resources :accounts
resources :invoices
The aforementioned guide will teach you that you should define the routes like this instead:
resources :accounts do
resources :invoices
end
which will give you these routes
verb route action
------------------------------------------------
GET /accounts/:account_id/invoices index
GET /accounts/:account_id/invoices/new new
POST /accounts/:account_id/invoices create
GET /accounts/:account_id/invoices/:id show
and so on ...

Related

Laravel Nova - output forms on front end?

I am building a system that uses Laravel Nova for managing resources.
There are a couple of instances where I want non-admin users to be able to create resources. The ideal solution would be to define the resource in Nova and embed Nova's own create form on the front end of the site.
Has anyone done anything similar, or have any suggestions how to go about this?
I believe you can use the same endpoints that Nova is using.
For example to get all the fields of a resource:
GET updating fields:
http://{url}/nova-api/{resource}/{id}/?editing=true&editMode=update
GET creation fields:
http://{resource}/nova-api/{resource}/creation-fields?editing=true&editMode=create=
After the user has filled out the fields, you can use this endpoint to save the new values:
PUT:
http://{url}/nova-api/{resource}/{id}

URL Routing: Laravel

I have been working on laravel and have been doing some routing. I was just wondering on what is the difference between writing the route as:
route::get('roles/{id}/edit',rolesController#edit);
versus
route::get('roles/edit/{id}',rolesController#edit);
One difference is clearly visible and that is the placement of the id variable. Can't figure out any other reason. Please provide an explanation on this.
Other than the actual look of the URL, there's no real difference as far as the framework is concerned.
I suppose it's the matter of preference when using any of this. Maybe, for example, if you are giving options of editing the user profile and posts, this might come handy as both are different routes, technically
No difference. It depends on you how you would like to build your routes. But try to user best practices which laravel creator recommend (https://laravel.com/docs/5.7/controllers#resource-controllers).
And also i want take your attention on how you called your controller. You should use CamelCase for naming your files (https://github.com/alexeymezenin/laravel-best-practices/blob/master/README.md).
There's no difference, but you might want to look in reosource routes and controller. Basically, laravel framework automatically creates routes and methods for controllers that you might need in your project. For example:
If you create a contoller like this:
php artisan make:controller RolesController --resource
and create a resource route like this:
Route::resource('/roles', 'RolesController ');
framework automatically crates this routes for you:
Verb Path Action Route Name
GET /roles index roles.index
GET /roles/create create roles.create
POST /roles store roles.store
GET /roles/{roles} show roles.show
GET /roles/{roles}/edit edit roles.edit
PUT|PATCH /roles/{roles} update roles.update
DELETE /roles/{roles} destroy roles.destroy
So you don't have to make your own routes and ask yourself if they are correct or not. Look into laravel official documentation for more info about this.

How to load an codeigniter application by using different url

Now I am developing an application by using CodeIgniter framework. In this application, there is a section which name account setup. By using this section we can create multiple accounts. As an example, accounts name are abc, bca or anything. And Suppose my site URL is: www.xyz.com (base_url) Then we need to access the site by the domain name and also domain name with account name like bellow:
www.xyz.com
www.xyz.com/abc/
www.xyz.com/bca/
www.xyz.com/anything/
For More clear, URL pattern will be:
www.xyz.com/dashboard/index
www.xyz.com/abc/dashboard/index
www.xyz.com/bca/dashboard/index
How can we do it? I need your suggestion.
Yes it's Possible through codeigniter Routs.
Go to application\config Open a file which Name is routes.php
you can call the controller functions see in code example
example
$route['new_project'] = "controller/function";
when you hit your website url like this www.xyz.com/new_project it will go to your mention controller and function.
see the documentation of routs CodeIgniter URI Routing
example 2:
$route['account/(.*)'] = "controller/function";
Now we you redirect your url like this
redirect('account/'.$name_var.'');
Now your url look like this.
www.xyz.com/account/name_of_logged_in_user
Also you can use this like this.
$route['(.*)/dashboard/index'] = "controller/function";
And then you can call it like this.
redirect(''.$name_var.'/dashboard/index');
And from this code your output url show something like this.
www.xyz.com/name_of_logged_in_user/dashboard/index
Hope it will help you if any question add comment
There are many way to do it.
1) Domain alias if you want to run same script with different domain.
2) if you want to run same script with sub domain then you can do it from Cpanel
3) If you want to run same script with different folders in same domain then you need to use htaccess

How do I change the index page in a CodeIgniter anchor?

So, I have two different applications in my CodeIgniter installation. One is admin, the other is frontend. I basically just copied the index file, renamed it "admin.php", and changed the application directory to "application/admin". I then changed the application directory in index.php to "application/frontend".
What I would like to do is create a link on the frontend application that takes you to the admin application. The variable config['index_page'] in the frontend application is set to "index.php" and in the admin application it's set to "admin.php".
Is there a way to set the url helper to use "admin.php" instead of "index.php"?
You don't need to do that way.
you should make or use an authentication library and you set different roles for different
users.
you just after login can put the redirection to your admin controller.
and for other users and viewers you can redirect them to any other controllers.
for viewers you can just use something like this:
Code:
if(!$this->m_auth->is_logged_in())
{
$this->viewers();
}
else
{
$this->users();
}
In your users function you just check different roles and redirect according.
I think you are missing some codeigniter concept, and you are trying to do it the normal way, i suggest you to read this article , you will how you can use MY_Controller as same concept of front controller and how you will be able to give every use specific roles
another way is to use a ready made authentication library as #medhi said
I would recommend Tank Authentication or Ion Auth
I

Magento custom model override

I'm new to Magento, so forgive my lack of knowledge. I am following a tutorial to integrate the contacts in Magento and import it to SugarCRM. The tutorial changes the core functions of Magento, but I would like to write a module and leave the core intact. I can easily find information about overriding controllers, models, helper, etc. But I have a question.
Imagining, for example, each time a user registers on my website, his contact information is exported to SugarCRM. In the save contact phase, in my module I know that I can write a specific code and tell that it would need to be run after or before some specific core-functions of Magento are done to save the contacts.What I really don't understand is, how will Magento know that I have a module that will perform me certain actions when I'm creating a new account?
The way magento handle this is by using events/observers. For instance, if you needed to send order information to another system after an order has been placed, you could create a custom module which observe sales_order_place_after.
Read more # http://codemagento.com/2011/04/observers-and-dispatching-events/
To get a list of events see https://magento.stackexchange.com/questions/153/where-can-i-find-a-complete-list-of-magento-events
For how to create an module with observer see https://stackoverflow.com/a/12696681/1191288

Resources