My opinion is you can move Pagecontent.find(c) to model and access
this from controller. Then just use it in view so to avoid making db
call from helper/view even this is simple
I don't believe it will, but perhaps I don't understand the structure of
your app. Can you explain how you're using this?
Anyway, even if it *does* mean extra coding, it's the right thing to do.
Let's say I have 5 pages. Each page can have 1 or more areas which can
be edited by the user. Probably 1-3 areas, where the main sections is
the body text.
Each area is assigned an id. By using my setup as above I can assign the
id and content for each area in the view, and that's it. No extra coding
or assigning a variable for each page and each area.
This is like static CMS where the control of new pages are limited but
not the actual text/content.
I don't believe it will, but perhaps I don't understand the structure of
your app. Can you explain how you're using this?
Anyway, even if it *does* mean extra coding, it's the right thing to do.
Let's say I have 5 pages. Each page can have 1 or more areas which can
be edited by the user. Probably 1-3 areas, where the main sections is
the body text.
Each area is assigned an id. By using my setup as above I can assign the
id and content for each area in the view, and that's it. No extra coding
or assigning a variable for each page and each area.
So the Pagecontent (should be PageContent!) is the model for the
editable area?
Are the 5 pages rendered by separate controller actions, or by the same
action on different instances of Page?
This is like static CMS where the control of new pages are limited but
not the actual text/content.
Where do the ids that you're passing to the pagecontent helper get set?
In the view with.
<%= content(the_id_of_content) %>
In the application helper:
def content(c)
@content = Pagecontent.find(c).content
end
Then the best thing to do, it seems to me, is to restructure things as I
indicated before. Moving the query into the controller will not require
any more code, and it will decouple the view from the DB (which is the
right thing). The proper Railsy way is like this:
# controller
def page1
@content = Pagecontent.find(whatever)
end
# page1.html.erb
<%= @content %>
Less code than a helper, more flexible, and more truly MVC. The helper
method has nothing at all to recommend it here.
Then the best thing to do, it seems to me, is to restructure things as I
indicated before. Moving the query into the controller will not require
any more code, and it will decouple the view from the DB (which is the
right thing). The proper Railsy way is like this:
# controller
def page1
@content = Pagecontent.find(whatever)
end
# page1.html.erb
<%= @content %>
Less code than a helper, more flexible, and more truly MVC. The helper
method has nothing at all to recommend it here.
Then the best thing to do, it seems to me, is to restructure things as I
indicated before. Moving the query into the controller will not require
any more code, and it will decouple the view from the DB (which is the
right thing). The proper Railsy way is like this:
# controller
def page1
@content = Pagecontent.find(whatever)
end
# page1.html.erb
<%= @content %>
Less code than a helper, more flexible, and more truly MVC. The helper
method has nothing at all to recommend it here.
I don't see how that would mean less code.
With the helper, you have both the helper method definition and the call
in the view. With the proper Railsy way, you just have the call in the
controller (equivalent to the helper definition) and the variable is
already set for the view without further work.
But don't take my word for it. Branch your project and try it!