Relationship between model and controller in rails - ruby-on-rails

i am a beginner, i got doubt regarding model and controller name specification i have created model name as "login" weather the controller name should plural of model name like "logins" ?

You might want to read up on naming conventions.
The Model is usually singular and the Controller is usually plural.

Related

How to create Models in Rails and pass data from models to controller

I am new to rails. I have created controller and able to see changes in http://127.0.0.1:3000/.
How to create Models in Rails and pass data from models to controller.
If you need to access a controller variable from a model it generally means your design is wrong because a controller serves as bridge between view and model (at least in Rails), controller gets info from models, models shouldn't know anything about controllers, but if you want to do it anyway.
For your first question you can simply create models in rails as
rails g model MODELNAME
g as short of generate

Calling Web API with Singular or Plural Controller URIs

Most examples of Web API that I've seen show URIs in this format:
/api/values/ (returns all records)
/api/value/{id} (returns a specific record)
I can think of times I would not like this URI convention, but for my current project it's perfect. Though, it's not working for me and I don't know what I could have done to effect this. For me, I can have one or the other - singular or plural.
If my controller class is named ValuesController, then my Web API is
/api/values/
/api/values/{id}
If my controller class is named ValueController, then my Web API is
/api/value/
/api/value/{id}
I used ADO.NET Entity Data Model to create several entities in model designer. All have a singular naming convention. For example, Activity, State, Country, User, etc. Though, EF appears to create plural database tables no matter.
I created APIControllers for each of these entities. I've tried 2 naming conventions with my controllers - both plural and singular, but I am not seeing the expected (and desired) results with the URIs. The plural and singular Web API are always the same no matter what I try.
FYI, I just checked the default API that is created automatically (ValuesController). To return all records I have to specify /api/values/ and to return a single record I have to specify /api/values/{id} - both are plural and I'm sure it wasn't that way by default.
What effects this? Where is the logic that configures/recognizes a distinguished form of the URI for plural and singular?
The default route that is configured in Web API's RouteConfig.cs file specifies a parameter named controller, similar to this:
routes.MapHttpRoute("Default", "api/{controller}/{id}",
new { id = RouteParameter.Optional };
When you're making a call to either /api/values or /api/values/{id}, the route value for your controller will be values in both cases. Consequently, the controller that Web API resolves by convention will be the ValuesController.
(Note that there's no automatic pluralization performed whatsoever. You can implement a custom IHttpControllerSelector, if you really wanted to implement that behavior. I strongly recommend not to do that, though.)

Good practice -- Redirection in models? -- Rails 3.1

I have a Search model and controller. The business logic is that if the user's keyword exact matches a product's model number, redirect them to the product page.
In this situation, should I just do the redirection from inside the model (where most of the logic resides already)?
Or should I return a flag or something to the controller so I can handle the redirect?
THe model object cannot ans shall never do a redirect. The application logic is the duty of the controller, so the controller should ask the model object (as result of a request) if the product matches a model number, and then the controller does the redirect. The model object should not know anything about controller or views. This is part of the "Model-View-Controller concept" this is implemented by Rails.
Rails implements the model as the ActiveRecord pattern, so it is ok that the model object is responsible for the database, and that includes the search on the database. See the many options you have in the Rails Guides for ActiveRecord Queries to see what falls in the responsibility of the model objects.
Always remember MVC pattern: MVC in Rails
Model must not take care of redirection or some other stuff that related to controller. Let redirection be in controller.

Retrieve the controller name from a class name

I am using Ruby on Rails 3.0.7 and I would like to retrieve a controller name given a class name. That is, I have
Articles::Category
and I would like to retrieve
articles/categories
I would like to retrieve the controller name inside a view file not related to the Articles::Categories controller.
How can I do that (possibly using some Ruby on Rails core method)?
Articles::Category.name.underscore.pluralize
=> "articles/categories"
As mentioned by many others, there is no special method for that. But as long as you followed Rails conventions, this approach will take you far.
To get the controller name, say inside your view, you can do :
<%= controller.controller_name %>
To get the name of a class, say User, if you have a user object named user :
user.class.to_s
Other than that, i don't think there's a correlation between a controller and a model, that can give you the controller name from a class name, because they are different things. You can maybe create a hash like {:controller => 'class_name'} and map those yourself.
Articles::Categories.name.underscore #=> articles/categories
And it underscores camelcased words to so that:
RailsAdmin::ApplicationHelper.name.underscore #=> rails_admin/application_helper
Assuming that you really meant Articles::CategoriesController then Articles::CategoriesController.controller_path will give you what you want.
Update The question is, given the name of a Rails Model, how do you get the name of the associated controller?
The answer to that question is, you can't. There is not a one-to-one mapping from the model name to the controller name. The User model could be controlled by the users_controller.rb and/or admin/users_controller.rb or not have an associated controller at all. You can certainly guess at some likely possibilities based on Rails conventions but you can't know.
Instead of underscore.pluralize just use tableize:
ActiveRecord::Base.name.tableize
=> "active_record/bases"

Do any naming conventions exist for ASP .NET MVC 3?

As I am learning MVC 3 it has become apparent that naming is critical to getting the Dbase-Model-Controller-Views to talk together.
Is there an already existing list of naming conventions that should be used in a MVC application?
Something like:
Table names (plural)
Model names (singular of Table Name)
View folder must be same name as controller class (ie Contract -> derived from ContractController)
The only major convention is that controller class names must end with "Controller". Any other conventions simply represent best practices and are not required for your application to work.
The table and model naming conventions you mentioned are there because they make the code "read" better (Select * From products where category = 1; Products.Insert(new Product()))
MVC automatically looks for a view that matches the name of the action method and it starts looking in a folder that has the same name as the controller. However you can easily trump that by specifying the view name in your view result (return View("MyCustomName"))
Not many name conventions exist that will break your application, other than the few such having your controllers end with "Controller" and name inference of the "View()" call from the action name.
MVC is based on the ideology of convention over configuration, so as I found out slowly, there are many conventions that are useful to follow, I have yet to come across a list though. They are subtle and very convenient... usually.

Resources