I am new to rails and I feel this question should have an easy answer but I can't figure it out.
in rails I have two tables USERS and INFOS they have many to many relationship and there is a third table INFOS_USERS but it doesn't have the model.
my problem is I want to add to this association table from Info view, but I have no method to call in the controller!
should I create a new model and controller?
is it possible to add a custom methods to controller?
Thanks
edit: I can write to table from controller but problem is I don't have any controller for the association table so I don't have any method to call from view.
Yes, you can add custom methods (actions) to a controller
Your routes might look like:
resources :users do
members do
get 'infos' # => /users/:id/infos
post 'add_info' # => /users/:id/infos/add_info
end
end
In this case it might make sense to use nested routes, which means you would have a different controller for the 'infos' - checkout the rails guides http://guides.rubyonrails.org/routing.html#nested-resources
for adding #user to #info:
#info.users << #user
if you need create new User from association:
#info.users.build
and you don't need any extra Controllers or Models
Related
I've got a User resource generated by devise. The users have a has_many association with a model called Parking.
I need to create an action for listing all the parkings that belongs to a certain User. Without devise, I'd create a parkings action on users_controller, but I'm not sure how's the correct way of doing it devise-way.
You need to create a controller parkings_controller.Then In index method you can write your code to list all parkings that belongs to certain User.
like this ......
In your index method in parkings_controller.rb
def index
#parkings = current_user.parkings
end
Hope this will work for you.
I'm still trying to get my head around actions and routes. I more or less understand how to user forms with the build-in controller actions like create, show, etc. What I want to do for a demo app is imitate a school's class schedule, where I have Courses and Students with a has_and_belongs_to_many relationship.
I'm using Mongoid, and I can add students to a course and vice versa using the console, but I can't figure out how to do it with a form. Would adding students to a course even be a controller action, or can I write and call a setter in the model somehow? If a controller action is better, what would the route look like?
If anyone knows of an example that does something similar, I'd love to examine it.
Thanks
It can be a controller action. If adding students to a course is a simple logic, you could add /courses/:course_id/Students/add. This means creating a courses folder, and a students_controller within it, with an add action.
Example (in your routes.rb)
resources :courses, :except => [:destroy] do
resources :students
end
More info: https://gist.github.com/jhjguxin/3074080
Is this what you are looking for?
I have a Section model which has many SectionTests. Each SectionTest has a 'position' attribute.
I am creating a screen that allows the user to alter the position attribute for all the SectionTests in a Section. So I'm updating a single attribute of a collection.
What is the best way of doing this in the routes and the controller?
Here is my current routes file :
resources :sections do
resources :section_tests
end
My first thoughts are to do it in the 'edit' action of the 'section_tests' controller, but 'edit' is usually used to edit only the attributes of a single model.
Where would you put this code?
If I understand correctly, you are trying to update a section that is linked to a section_test? You could use an Active Record Callback like after_save in your SectionTest model.
Supposing I have 3 tables: Users, Roles and UserRoles. I can fetch the user_roles in the request: users/1/user_roles but what if I want to fetch the roles of a user such as: users/1/roles.
My relation is as follows:
has_many :roles, :through => :user_roles
Is it possible?
The relation in your model has no direct relevance to the url you go to. You need to replicate your existing behaviour for routes rather than user_routes by either adding a new action or new controller and creating new views etc.
At the moment you probably have either:
an action called user_routes in the UsersController
an action called index in the UserRoutesController
So either:
Add an action called routes in the UsersController
Add an controller called RoutesController with an index action.
Regardless to which method you are using, in your action set your instance variable to user.roles instead of user.user_roles.
You can then write whatever view code you need.
How can I define a relationship between two controllers. I have one controller called rides and another called registrant. Is there anyway I can access the registrant database from within the rides controller? I was thinking
#registrant = Registrant.find(:first)
from within rides, but that didn't work.
Any suggestions?
Thanks
You can access your registrant model from your rides controller just like accessing it from any other controller. What do you mean by Registrant.find(:first) not working?
Now, if there's a relationship (or association as it's normally called) between your rides model and registrant model (like a has_many association), you can use nested resources to nest one controller in another.
Check out the Action Controller Overview and Rails Routing from the Outside In guides and think about picking up a good book on Rails like Agile Web Development with Rails.
If you have defined models: ride and registrant (or more general user) then you can setup a before_filter on the rides controller:
before_filter :get_user
def get_user
#user = User.find(:first, :conditions => %Q(userid = "#{params[:user_id]}"))
end
This would fetch the the user with user_id passed in as a parameter before the controller generates the view.
Yes, that should work. To get the terminology right, you are accessing the Registrant model from the RidesController. They should both be in the same database, but in separate tables.
Please post the error message you are getting.