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
Related
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).
I'm trying to find out if its possible to create a controller or action result in MVC that will trigger for every action.
Basically I want a check to occur before people are directed to the url each time, if this check fails I will return a different view but I don't want to write this out for every single action in my controller.
Can this be done??
This can be done with a custom ActionFilterAttribute.
Overriding OnActionExecuting means I can put the same check in for all actions.
I am new to Umbracoo . I was trying to call Controller action method from umbracoo . I find the solution for how to render view but did not find any way to call action method of controller ..
for this purpose first you would have to inherit your controller from Umbraco.Web.Mvc.SurfaceController or Umbraco.Web.Mvc.RenderMvcController
have a look at:
http://our.umbraco.org/documentation/reference/mvc/surface-controllers
http://our.umbraco.org/documentation/reference/mvc/custom-controllers
Make sure that your view/partialView is created from umbraco backend as template so that it would have it's record save in database through unique id.
it would be hidden in your solution explorer first show all hidden items and the include in your project and start working on it.
I am following this tutorial http://guides.rubyonrails.org/v3.2.13/getting_started.html to build my rails app in version 3.2.13 . If you go to the section 6.9 you will find controller and view for creating new posts . Here I do not get how #post variable is passed from new action to create action and where is create function called ? Also , I faced the same problem while working on edit and update actions . Please guide me through this .
It's not passed to create action, it's instantiated again with params you pass from the form displayed with new action.
create action is called with POST request to the path specified in config/routes.rb, leading to specific controller and action.
#post is not passed from new to create the params hash is passed into the create method #post is then set using the new method of the model not the controller. create calls new and then save and returns the object. new returns the object without saving and then save returns the validity of the object. That is why the create method in the controller calls new and then has a conditional statement for save. It is basically saying initialize this object then if it is a valid object do one thing if it is not do another. The create action is not called because of this check.
#this will return true if valid or false if invalid
Post.new(params[:post]).save
#this will always return the Post object which conditionally is true in Ruby
Post.create(params[:post])
#To use the create in a conditional statement it would be
Post.create(params[:post]).valid? || Post.create(param[:post]).save
The last line is unnecessarily redundant and thus why the example uses new followed by save.
create method for a Model is more succinct but probably best to use when you know the object is valid.
Hope this gives you a better understanding but if you are still confused please let me know and I will try to explain further.
I am trying to write a custom src/templates/scaffolding/Controller.groovy and was wondering if there was any way to get access to the controller name? Right now it seems like you can only get the "model" class. The reason I need it is I am customizing the render to prefix the templates directory based on the controller.
For instance I have a controller named AuthorAdminController and I need to customize the list to use the /admin/user/** directory.
Let me know if you have any questions. I am getting ready to look into how to customize DefaultGrailsTemplateGenerator but I am not sure if that is the correct route to go.
Example:
class UserAdminController {
static scaffold = User
}
Currently in my Controller.groovy I get className='user' so I have no access to the controller.
I don't think you can, as the way scaffolding works your template will always be generating a class named DomainClassNameController (i.e. UserController in your example), which gets loaded into a new classloader and then the metaclass of the real controller (UserAdminController) gets new actions added to it which delegate to an instance of the generated UserController.
Now every controller has access to the controllerName property during execution of actions, so this may provide you with a workaround. I haven't tried it, but you could try putting a log.info("controller: \${controllerName}") into the template and see which name it gives you (the backslash to make it resolve at runtime rather than generation time).