rails admin, create a button that runs custom code - ruby-on-rails

in my rails app I need to generate a bunch of buttons/links linked to several models to enable users to run some maintenance tasks (coded in the Backend).
I found the concept of actions in rails admin here: https://github.com/sferik/rails_admin/wiki/Actions, but I'm not quite clear how to use it, any ideas?
I thought I could add the buttons to the edit action for a model, but no ideas how to.

Sooo, rails uses an MVC paradigm. It sounds like you have the View and Model components under control, but you are missing your Controller logic. The Controller, is a place that handles any View logic using Model methods... ideally.
So, if you already have an existing controller that you are trying to add "Controller Actions" to... you need to define your custom method actions you want to create in your config/routes.rb file.
This will look like the following:
Create a route for your new controller action.
Add new controller action definition to your Controller.
Call controller action in your view's button link_to tag helper
Handle your custom logic in your new controller action using model methods
This is the cyclical MVC nature of Rails. M (Model), V (View), C (Controller).

Related

I need a button or a link which runs method from my model

I can create method in a controller and add routes for it. After that I'll be able to use it like that <%= link_to "+", upvote_item_path(i) %>, but now my methods are in the model.
How can I use them?
Should I create *.js.erb and write a function which runs by the button with onclick or there is another way?
I can create method in a controller and add routes for it
That is exactly how you invoke methods on your models. Your app needs to provide a route, controller and action which invokes methods on your models on behalf of the user.
Buttons and links have absolutely no direct access to your model layer. Your model layer and the client can never communicate directly. The client submits requests, which are routed to an action on a controller, and that action may manipulate your models in some way, and then render a result. Trying to allow clients to invoke methods on your model layer without going through an action is absolutely incorrect, and not at all supported by Rails.

Can't understand what this text is saying

I am following a tutorial and got to this point: http://rubysource.com/building-your-first-rails-application-views-and-controllers/
rails generate controller urls new
The reason we only passed in the new action (instead of new, create,
and show) is because Rails automatically generates a dummy view for
each action included in the generator call. In this case, we only want
a dummy view for the new action, so we exclude the others.
So why we only need to create the controller for new? Can someone plase explain it in a little more details?
The command is used to create the UrlsController with only one method: new.
This command will also automatically create a view file for you in:
app/views/urls/new.html.erb
Had you supplied more arguments like:
rails generate controller urls new create show
You would have gotten:
app/views/urls/new.html.erb
app/views/urls/create.html.erb
app/views/urls/show.html.erb
Since the tutorial only needs the new view it was unnecessary to create the additional views, hence those additional arguments were not added to the generate command.
Later in the tutorial you manually add the create and show methods, but you never add views for those methods (since those methods will not be needing specific views files in this application).
So: what you did was create the controller UrlsController with one method new, and the corresponding view for that method. The rest of the methods you will code in manually later in the tutorial so there was no need to auto-generate anything else (create or show).
This will only create the new action in the controller and should skip the other ones.
EDIT:
It will generate a controller called UrlsController in app/controllers and in that controller there will only be one method called action which corresponds to a route or url called urls/new

Model without View: do I need a Controller in Rails

I would like to have a Referral model, where user A can refer an Event X to user B. The way I plan to implement it is without Referral Views. I just have a Refer button on a events/show page for create action, and have referred users in users/show page for show. I don't see the need to create a view for referral.
From design pattern point of view, is it OK to create "refer" action in Events_Controller to invoke create method in Referral model, instead of sending it to a designated controller (i.e., Refferals_Controller), which will make call to Referral model?
More generally, should I create controller to allow other controllers to interact with my model, even if I don't need any views for this particular model?
Thank you.
Controller is where the http requests come (after passing through Routing). No incoming requests == no controller. Good when controller complies with REST scheme (index, show, create ...). But you can add non-REST actions as well (Rails Routing supports this: 3 Non-Resourceful Routes)
All interaction with model should probably go in the model class.(Edit: I mean code)
You shouldn't create a controller to just forward calls to model from other controllers. You can have several models per controller. Or several controllers per model.
Sometimes (if you know what you're doing) you can even call model methods from the view directly ("some rules can be bent, some can be broken" (c) Morpheus ).

Rails - Same Model/Controller, but different View

I am constructing a form wizard that guides the user through a form that has already been created and deployed. The model and controller should stay the same as the only thing of change is the view (guiding the user through each form field). What is the best (and easiest, if possible) way of accomplishing this task?
Even through the wizard, once the user saves their form, it gets saved to the same database via the same model and controller. From doing a bit research it seems that this is possible by obviously creating a new view, create a simple controller that extends the original controller, and routing the new controller to the new view.
Any suggestions are really appreciated. Thanks!
After doing some research, I figured out a couple of ways to accomplish my task:
A simple way is to create a wizard action within the form's controller. Have the wizard action render the wizard's view.
A better way is to create a wizard controller and have it subclass the form controller. Override the new action, and render the wizard view. This is the more preferred method because basic CRUD actions in Rails acquire REST for free. Thus, following this method will yield a RESTful wizard.
You should definitely look here:
http://railscasts.com/episodes/217-multistep-forms
Otherwise, to answer shortly, you can tell any action of your controller to render ay view you want.That's what is done in the basic scaffold controller:
render :edit

Populating the model for a shared view, embedding a shared view within another view

I'm trying to embed a small view snippet that steps through a model fragment that works fine when I embed it in a single controller and pass it to a view like so;
Controller:
return View(_entities.formTemplate.ToList());
View:
http://www.pastie.org/666366
The thing is that I want to be able to embed this particular select box in more than just this single action / view, from the googling I've been doing this appears that it should go into a shared view, but I'm not clear then on how I could populate the model within that view from the controller? (or maybe I'm completely missing the purpose for shared views?)
In the other MVC framework I'm accustomed to working with there is the concept of a filter where you can call code before or after any action and mod the model as it passes the controller and goes to the view, is such a thing possible in .net mvc?
Any assistance appreciated.
You'll want to use the HtmlHelper method DropDownList() in order to create a input:
<%= Html.DropDownList("id", new SelectList(formBuilder, "ID", "Name")) %>
You probably want to use a ViewUserControl here.
You have a couple of options if you go that route. If it's model data that is easily available, recreate it at the call site of your RenderPartial like so:
<%=Html.RenderPartial("ViewName", new ModelData())%>
If it's data that is dependent on the current model data, then you'll need to pass that data somehow to your partial view.
ASP.Net MVC also has the concept of before/after controller actions. You decorate your controller method with an Attribute that derives from ActionFilterAttribute. In there, you have access to OnActionExecuting and OnActionExecuted.

Resources