How to create controllers in different namespace for an existing model? - ruby-on-rails

I have a Product model in my rails app. Now I want to create a controller and views for this product model through scaffolding in a different namespace(api).
Till now I have tried using
rails g scaffold_controller product name:string price:integer
and after this I added the this to my routes file
namespace :api do
resources :products
end
Now when I go to the link api/products . I get this error
uninitialized constant Api::Product
on the index action
def index
#api_products = Api::Product.all
end
After this I removed the Api:: from my controller index, new and create action. After doing this my index url (/api/products) was working fine but now when I try to create a new product(/api/products/new) I get the following error
undefined method `products_path'
This is the code for my model file (location is models/)
class Product < ActiveRecord::Base
end
Can anyone please help in implementing this correctly?

You should move product.rb to app/models/api and change the class name to Api::Product
#app/models/api/product.rb
class Api::Product < ActiveRecord::Base
self.table_name = "products"
end

Related

Rails controller design help needed

I have this scenario.
My Models
class User
end
class Student
has_many :daily_records
end
class DailyRecords
belongs_to :student
end
What i am trying to archive
i want to create a form on the student's show page to enter data for his daily records.
i want the form to have the id for the user entering the data.
How can i design the controller for this scenario.
What i have done so far.
i added the user model using Devise
i have scaffold the two model (student, daily_record) and added the associations as seen above.
I know i need to work on the show action of the student controller to make this work. I need help. just new to rails
thanks
class StudentsController < ApplicationController
before_action :set_student
def show
<--help code--->
end
........

Calling models with _ us controllers

I have a table in the model called pg_search_documents, how do I work with it in the controllers?
I'm trying like this:
def show
#search = PgSearchDocument.find(params[:content])
end
But the so-called "PgSearchDocument" seems to be wrong.
You need to make sure you have a model declared in your app. If you have not done so, create the following file:
app/models/pg_search_document.rb
class PgSearchDocument < ActiveRecord::Base
end
In Rails 5 you would use:
class PgSearchDocument < ApplicationRecord
end
Please note the following naming conventions in Rails:
Database table name is plural snake case: pg_search_documents
Model filename is singular snake case: pg_search_document.rb
Model class name is singular camel case: PgSearchDocument

Nil class error for association

I have a model Tutorial and has_many association Tutorialcategory where tutorials can be connected to many tutorial categories. Tutorials model and controller are working fine but categories are not. I am able to add categories but can not edit them, I am getting 'undefined method `model_name' for NilClass:Class' error while accessing edit page for tutorialcategory.
I have defined route as follows
resources :tutorialcategories do
end
following is my model definition
class Tutorial < ActiveRecord::Base
has_many :tutorialcategories
If you can add categories but not edit them then you must load a #tutorial_category variable in your edit action, something like this:
def edit
#tutorial_category = TutorialCategory.find params[:id]
end

relation "admin_keywords" does not exist`

I want to have scaffold actions for Keywords table as admin. This code begins to work after I restart the server and Remove Admin:: from /app/models/admin/keyword.rb, then refresh website, get error and adding Admin:: to model again. From that moment everything works fine. But after server Starts, I got this: (Rails 4)
PG::UndefinedTable: ERROR: relation "admin_keywords" does not exist
/app/controllers/admin/keywords_controller.rb source:
class Admin::KeywordsController < ApplicationController
def index
#keywords = Admin::Keyword.all
end
end
/app/models/admin/keyword.rb source:
class Admin::Keyword < ActiveRecord::Base
end
going to url:
http://localhost:3000/admin/keywords
routes.rb:
namespace :admin do
resources :keywords
end
How to fix this error?
If you add namespace to your models, database table should contain this namespace too. For example model Admin::Keyword is related with admin_keywords table.
You can override model's table defining self.table_name='your_table_name' method in model.
class Admin::Keyword < ActiveRecord::Base
self.table_name = 'your_table_name'
end

Create a controller and view

Hello I have the following models.
The User Model:
class User < ActiveRecord::Base
attr_accessible :email, :password, :password_confirmation
has_one :user_informations
has_secure_password
end
The UserInformation Model:
class UserInformation < ActiveRecord::Base
belongs_to :user
attr_accessible :address, :address2, :business, :descripcion, :identification_number, :mobile_cell, :name, :phone_number
end
Now I need to create the view and the controller to create and update the user information, and I have many questions:
1) how can I generate the controller:
rails g controller UserInformations
o
rails g controller UserInformation
2) how my new, create and update action know the user ID.
3) how can I set the routes for this user information
Thanks. Maybe these are a basic question, but I'm new in rails and I don't know how to do all of this.
Thanks again for your help.
1) You have to use pluralize for controller, so rails g controller UserInformations will work.
2 + 3) You can set up Restful routes:
resources :users do
member do
get 'user_information'
end
end
With above routes you will have path users/:id/user_information, so you can know your user ID through params[:id], ex, in your create or update action you can use:
user = User.find(params[:id])
to find which user is shown informaton.
You'll need a user_id column in your user_information table and model.
1) rails g UserController
2) you can include the user_id as param, so for the new action it will be
def new
#user = User.find(param[:user_id])
#user_information = #user.user_information.new
end
the create and update actions would get the user id from the form params but you'll need to think about who is going to be using these actions and if you want to allow all users to update the information of other users. If not, you should have the user id as a hidden param and use a gem like cancan (https://github.com/ryanb/cancan) to restrict access
alternatively you can set them up as nested resources (http://railscasts.com/episodes/139-nested-resources)
3) for a simple resources you can add this to your routes.rb file
resource :user_information
or for nested you can do
resource :users do
member do
resource :user_information
end
end
First of all
in User Model, there should be
has_one :user_information
as there association name should be singular with has_one.
you can create controller by giving the command
rails g controller UserInformation
and it depends on you what name you want to give to you controller.
In new action you will have to find user by its id. You can store user id in session
after login. Or if you are saving user first time then you will have to pass user id.
In new action do
user = User.find(session[:id])
user.user_information.create(params[:user_information])
I think you need to study all this first. Go through some examples. Then try. It is too quick to ask here.

Resources