D
D
Damir Nurgaliev2015-05-21 10:46:39
Ruby on Rails
Damir Nurgaliev, 2015-05-21 10:46:39

How to implement routing and communication between models?

Question from a completely newbie.
Actually, there is an implemented User model with an input and output.
Task: to give each such User the opportunity to create an event (Event model) and, in fact, display on Ch. page at User.
The connection itself seems to be clear: User has many :events, Events belongs_to :user
But what about routing?
That is, it is necessary that the user's page has a button "Create new event", and when the transition is redirected to <>/user/id1/create_event

Answer the question

In order to leave comments, you need to log in

3 answer(s)
J
Jeiwan, 2015-05-21
@damirik

/user/id1/create_event is not the Rails way. In rails, everything is already invented for you:

resources :users do
  resources :events
end

In general, an ordinary nested resource. To create an event, use the create action of the EventsController controller. The path will be /users/:id/events, POST method.
In the EventsController controller, in event actions, search through the user, i.e. (example for show):
@user = User.find(params[:id]])
@event = @user.events.find(params[:event_id])

The path for show will be /users/:id/events/:event_id.
Everything is standard and normal. I advise you to learn www.rusrails.ru/rails-routing

K
kkrieger, 2015-05-21
@kkrieger

Something like this

resources :user do
    member do 
      get :create_event
    end
end

S
Shetani, 2015-05-21
@Shetani

resources :user do
   resources :event do
     match :create_event, via: [:get, :post], on: :member
   end
end

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question