Using custom rails admin action to render app view - ruby-on-rails

I am trying to create a custom action in rails admin. I have the action setup in the sense that the icon is now present and can click on it but I am trying to figure out how to get the action to do what I want it to. Essentially I need it to render a view that I have setup in app/views/...etc. I can't seem to find any examples on how to do this and if it is even possible.
The view can be accessed outside of the rails_admin portal by going to: www.myapp.com/clerk/<some_id>. I need to action in rails_admin to take me to this page.
Most examples i see either call and API on the object or some function on the object... Any help is appreciated.

Related

How should I be updating data with AJAX?

I am making a rails MVC style application.
When I am loading the pages, the controller gets the data from the model and forwards it to the view and the view renders itself.
However, I am wondering what the appropriate way is to interact between javascript and my database.
Say the user is playing a game and he has a bunch of clues on the screen. Clicking a clue will move it to his clue box and needs to update the UserClues model to add the clue.
The user also needs to be able to remove clues where the clue gets removed etc...
This needs to happen via AJAX.
I am wondering what the appropriate approach is.
Say he got his page from /Investigation which loaded the page using the InvestigationController.
Should the InvestigationController be the one calling the model to add/remove the clues, or should some sort of data controller do it.
For example:
/Investigation/AddClue
or another controller
/User/Clues/Add
The reason I am asking is I have a lot of data on my pages that is created/added/modified and I am wondering if my routes/controllers should be separate for data manipulation and pages, or if I should have a controller for each page and it also manages what the user can do on that page (like add / remove clues to and from a ClueBox)
Thanks
Ideally you will want to have a separate controller for each resource. Even though your current view is in the context of the Investigations, controller, dealing with clues should have its own resource. Otherwise things can get complex. If the clues are investigation specific, then you can scope them under the Investigation URL.
resources :investigations do
resources :clues
end
This will give you something like /investigations/1/clues/1, but this maybe not be necessary, and I would avoid nesting unless required.
The controller can then respond in the correct format. In this case Rails UJS provides you with the tools you need. You can render a create.js.erb or destroy.js.erb templates to handle user actions. The templates would have the required JavaScript and dynamic code to update the screen state.
If at all possible, try to use Rails' RESTful routes. In this case, that probably means line in your routes file that looks something like:
resources users do
resources clues
end
In your users/clues_controller.rb file, put in actions for the different AJAX requests, like create to add a clue to a user (via a POST request), destroy action to remove a clue from user (via a DELETE request, might need to simulate with POST + "_method=delete" parameter).

Best practice when developing an RoR application

I am currently creating an application. It's relatively simple, lets say for now it only contains users & posts
What I am trying to do is display basically a blog with a new post form at the top in case the signed in user is admin.
I have two options:
I can use an index for posts and keep it all within the posts controller. I could add a form to the index.html.erb which checks for admin attributes.
However, I will most likely use the index functionality later on in other parts of the app.
Second option would be to create a static page called blog and render the form view and all posts.
Both should be possible, but what is the "rails way"? Or is there no best practice?
Controllers should be RESTful, and should be appropriately named for the resources they manipulate.
The index action of your PostsController should have one purpose ... providing appropriate information relating to all of the posts to its view. The exact output of this could change within the view depending on whether you're logged in as an admin or not, but essentially the role of that action should be restricted to that function.
I would advise you to take a look at the CanCan gem and think about how you could use that to authenticate users, providing appropriate page content to admins and normal users alike.

are controllers necessary to send flash messages in rails?

this is kind of a newbie question, sorry but...if a model has no controllers, how could i perform a
flash[:notice]?
for example in
https://github.com/tute/merit
i noticed that it just has models and no controllers. i know that it would be wise to put the flash message inside a controller as opposed to the methods that get executed in the models, but.... how can i do this if there is no controller?
i could try to create one but then doesn't each action in the controller correspond to a page in the view?
certain methods are being executed in the models and i want to be able to add a flash[:notice] whenever they get executed but...how would i do this?
thank you
You would still set flash messages in a controller (or anywhere else the session can be accessed, technically). Merit is just an add-on for Rails, not a replacement. You would still use controllers in your application to handle requests from the user.

Rails use different layouts?

I know rails uses the default application.html.erb. I have a profile and on my profile I have videos, photos etc, how can I separate my homepage(Home, About, Signup, Login) from the actual application? The problem I am having is when a user tries to signup rails throws an error
Couldn't find Site with subdomain =
That is because I have not created a subdomain nor site since I am unable to sign up to create one.
How can i tell rails to use application.html for pages like home, about, signup, etc and use home.hmtl.erb for the profile pages and videos etc?
create a controller called home or something similar. Then in the controller call:
layout :layout_name
you may need to create an index.html for it to not throw an error as well
You can add layout and its name when rendering action for each action where new lay out is required or u can create set layout method by default is set application and for special action load specific layout.

How to Customize Registration using Devise on Rails?

I'm currently working on a Rails project and have decided to use Devise for user registration. The site is using MongoDB (mongoid gem), and I am planning to create a simple sign-up/sign-in system.
So, there is a link in the home page that allows the user to click on it or open in another tab. If he decides to click it, there should be a popup modal that contains the fields and sign-up button, etc. If he opens a new tab, he should see a dedicated page for the sign up process.
So, here is what I have so far. I have installed devise and am able to sign up properly. However, I also want to create the modal effect, in which I used jQuery UI dialog attached to the sign-up link. Then I loaded the sign-up page (/user/sign-up) using dialog.load("path") and stripped the layout when it is an ajax request.
I know this is not the best method to use, so I've been reluctant. Is there a better way of doing this? Preferably a standard method. Any help would be appreciated, or just point me to the right direction. Tutorials will be very nice and helpful. Thanks a lot in advance!
I am not sure if I understand the question, but try this. Run
rails generate devise:views
To create the devise views.
In the views/devise/registration folder you will find the registration page. You should now have access to both the form code and the path user_session mapping to /users/sign_in.
You now have the form and the path, so you should be able to play with the AJAXifying it.

Resources