Hi, guys,
I want to create a controller that assocates multiple
models(ActiveRecord),
how to building ?
For example, my controller called posts, it has the models post ,
catalog
and tag.
thanks all.
Hi, guys,
I want to create a controller that assocates multiple
models(ActiveRecord),
how to building ?
For example, my controller called posts, it has the models post ,
catalog
and tag.
thanks all.
One controller, multiple models, how to create ?
class PostsController < ApplicationController
def new
p = Post.new
c = Catalog.new
t = Tag.new
end
end
class Post < ActiveRecord::Base
end
class Catalog < ActiveRecord::Base
end
class Tag < ActiveRecored::Base
end
As far as I know, models aren’t associated with a controller. So you
could just ‘rails generate model ModelName field:type’ three times for
the Post, Catalog, and Tag models. Then in some controller, you can
write:
class PostsController < ApplicationController
def new
@title = “Some page”
@val = 10
@post = Post.new
@post.email = "[email protected]"
@catalog = Catalog.new
@catalog.name = "Pottery Barn"
@tag = Tag.new
@tag.name = "dishes"
end
end
And then in view, like new.html.erb, you can write:
which is inserted in the layout: application.html.erb,
<%= @title %> <%= csrf_meta_tag %><%= yield %>
This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.
Sponsor our Newsletter | Privacy Policy | Terms of Service | Remote Ruby Jobs