I''m trying to work with pagination for the first time in RoR. In my post_controller.rb I am editing the show.rhtml. It reads: model :post .... def show @post_pages, @posts = paginate :post, :order_by => ''id'' end In my database, under the "posts" table, I have columns named ''title'', ''description'', ''created_on'', ''updated_on'' and ''file''. In my show.rhtml I try to access the variables simply using: <p>The title is: <%= @posts_page.title %></p> and it gets stuck there. I have tried using the @post variable and the @page variable, but I guess that doesn''t make any sense. Any direction in my pagination journey would be sincerely appreciated! --- [This E-mail scanned for viruses by Declude Virus]
I am trying to make a photoblog where I have a title, description, date, and then MySQL while store the location of the image. My goal is something similar to http://overshadowed.com. I''ve tried it doing a manual way where users can just browse through pictures and have links going to the next page by doing things like (@params["id"] - 1) and (@params["id"] + 1). Someone suggested trying the pagination feature and I am attempting to understand it. I would like to use "Paginate" to make each page for each picture. I might be unbelievably wrong in doing this, but from my understanding of Paginate it makes sense. One basic thing I fail to understand is the effect/importance of the @param["id"] value on the Paginate. Can I use this with paginate just like a normal action would use it? My other problem is how to actually use the paginate function to retrieve my information. I know my variables are wrong because they don''t work, but I don''t know what variable to use in my view files to access the information of the page. Is it @page, @post_pages, @posts? You''ve said some of these don''t work, but what is the right way to access my information. Maybe my full show.rhtml will show you some idea of what I am trying to accomplish. Keep in mind I am just trying to get it to work, not display images and everything else - yet. Just reading information from the database. show.rhtml: <p>The title is: <%= @posts.title %></p> <p>The description is: <%= @posts.description %></p> <p>The post was made on: <%= @posts.created_on.strftime "%e %B %Y | %I:%M %p" %></p> <p>The file is:<%= @posts.file %> Seriously Jarkko, thank you for your help so far. Jarkko Laine <jarkko@...> writes: > It should be paginate :posts but I''m not sure if it makes any > difference. I changed that but since my variables are still wrong it doesn''t make any difference, yet. > If you mean <at> post_pages, it''s a paginator object. It''s not a Post > object so it doesn''t respond to methods that belong to Post class (like > title). > > Even <at> posts is not a Post object. It''s an array consisting of Post > objects. This is *exactly* my problem. I fail to understand paginate and its role in reading the database. Does paginate create something like a Post object so that I can actually access my database information? Is there a Post object from the pagination? What are the @post_pages, paginator object, and the @posts arrays used for? It sounds like I might use the @posts array "consisting of Post objects". If you understand what I mean and are able with time permitting if you could write the proper "show" action and show.rthml and say what your saying, I would appreciate it so much. Thanks again. --- [This E-mail scanned for viruses by Declude Virus]
I''ll look through all that and now go over the documentation a few more times. I can''t thank you enough for your help. I''ll start working more on that tonight. Thanks again, its starting to make some more sense. > Like said, <at> posts is an array of Post objects, not a single Post > object, so you need to use <at> posts.first.title and so on (or > <at> posts[0].title if that sounds more familiar). I do have one more initial question. When you say this it makes me think that I should use: @posts[(@params["id"]).title or something like that to access the information. Does that work/make sense? I have to go now and won''t be back until much later so I''ll say thanks in advance now. All your notes have been extremely appreciated, best of luck! Jarkko Laine <jarkko@...> writes: > > Michael, > > On 25.4.2005, at 07:20, Michael Klein wrote: > > > I am trying to make a photoblog where I have a title, description, > > date, and then MySQL while store the location of the image. My goal is > > something similar to http://overshadowed.com. I''ve tried it doing a > > manual way where users can just browse through pictures and have links > > going to the next page by doing things like ( <at> params["id"] - 1) and > > ( <at> params["id"] + 1). Someone suggested trying the pagination feature > > and I am attempting to understand it. > > Pagination is normally used on pages like Google search results where > there''s a huge amount of results and they therefore need to be split on > multiple pages. As far as I understand you are talking about pages > showing only a single photo. While even this can be achieved using > paginator, it''s not really something paginator has been made for. > > > > > I would like to use "Paginate" to make each page for each picture. I > > might be unbelievably wrong in doing this, but from my understanding > > of Paginate it makes sense. > > Well, yeah, I don''t see any reason why it wouldn''t work when I think it > more closely. > > > > > One basic thing I fail to understand is the effect/importance of the > > <at> param["id"] value on the Paginate. Can I use this with paginate just > > like a normal action would use it? > > Yes. Pagination is just something your "normal" view is using to help > split the result set. > > > > > My other problem is how to actually use the paginate function to > > retrieve my information. I know my variables are wrong because they > > don''t work, but I don''t know what variable to use in my view files to > > access the information of the page. Is it <at> page, <at> post_pages, <at> posts? > > You''ve said some of these don''t work, but what is the right way to > > access my information. > > Here''s an example from one of my projects: > <at> model_pages, <at> models = paginate :models, :conditions => ["s.id = ?", > <at> subpage.id], :per_page => 15, :order_by => ''models.art'' > > Here <at> model_pages is a Paginator object (see > http://rails.rubyonrails.com/classes/ActionController/Pagination/ > Paginator.html). <at> model_pages.current for example will then always > represent the current page. > > <at> models will be an array of <at> model objects. It''s size is normally 15 > (and something between 1 and 15 on the last page) because I''ve said in > the above code that I want 15 models per page. In your case this number > should be 1. > > If I now want to access single Model objects, I can do it like this: > > <% for model in <at> models do %> > <%= model.art %> > <%= model.edscription %> > <% end %> > > > > > > Maybe my full show.rhtml will show you some idea of what I am trying > > to accomplish. Keep in mind I am just trying to get it to work, not > > display images and everything else - yet. Just reading information > > from the database. > > > > show.rhtml: > > > > <p>The title is: <%= <at> posts.title %></p> > > <p>The description is: <%= <at> posts.description %></p> > > <p>The post was made on: <%= <at> posts.created_on.strftime "%e %B %Y | > > %I:%M %p" %></p> > > <p>The file is:<%= <at> posts.file %> > > Like said, <at> posts is an array of Post objects, not a single Post > object, so you need to use <at> posts.first.title and so on (or > <at> posts[0].title if that sounds more familiar). > > Hope this helps you a bit further. If you still have some problems, > just ask for help. I would suggest reading through the api docs on > paginator > (http://rails.rubyonrails.com/classes/ActionController/Pagination.html) > carefully, too. > > //jarkko > > -- > Jarkko Laine > http://jlaine.net > http://odesign.fi > > Attachment (smime.p7s): application/pkcs7-signature, 2363 bytes > > _______________________________________________ > Rails mailing list > Rails@... > http://lists.rubyonrails.org/mailman/listinfo/rails > --- [This E-mail scanned for viruses by Declude Virus]
On 25.4.2005, at 06:18, Michael Klein wrote:> I''m trying to work with pagination for the first time in RoR. > > In my post_controller.rb I am editing the show.rhtml. It reads: > > model :post > .... > def show > @post_pages, @posts = paginate :post, :order_by => ''id'' > endIt should be paginate :posts but I''m not sure if it makes any difference.> > In my database, under the "posts" table, I have columns named ''title'', > ''description'', ''created_on'', ''updated_on'' and ''file''. > > In my show.rhtml I try to access the variables simply using: > > <p>The title is: <%= @posts_page.title %></p> > > and it gets stuck there.There is probably no instance variable named @posts_page (you are setting up @post_pages and @posts above). If you mean @post_pages, it''s a paginator object. It''s not a Post object so it doesn''t respond to methods that belong to Post class (like title). Even @posts is not a Post object. It''s an array consisting of Post objects. It''s a bit unclear to me what you''re trying to accomplish with your code. It would be easier to help if you could post a bit more thoughts/code on what you want the action to do. //jarkko> > I have tried using the @post variable and the @page variable, but I > guess that doesn''t make any sense. Any direction in my pagination > journey would be sincerely appreciated! > --- > [This E-mail scanned for viruses by Declude Virus] > > _______________________________________________ > Rails mailing list > Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org > http://lists.rubyonrails.org/mailman/listinfo/rails >-- Jarkko Laine http://jlaine.net http://odesign.fi _______________________________________________ Rails mailing list Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org http://lists.rubyonrails.org/mailman/listinfo/rails
Michael, On 25.4.2005, at 07:20, Michael Klein wrote:> I am trying to make a photoblog where I have a title, description, > date, and then MySQL while store the location of the image. My goal is > something similar to http://overshadowed.com. I''ve tried it doing a > manual way where users can just browse through pictures and have links > going to the next page by doing things like (@params["id"] - 1) and > (@params["id"] + 1). Someone suggested trying the pagination feature > and I am attempting to understand it.Pagination is normally used on pages like Google search results where there''s a huge amount of results and they therefore need to be split on multiple pages. As far as I understand you are talking about pages showing only a single photo. While even this can be achieved using paginator, it''s not really something paginator has been made for.> > I would like to use "Paginate" to make each page for each picture. I > might be unbelievably wrong in doing this, but from my understanding > of Paginate it makes sense.Well, yeah, I don''t see any reason why it wouldn''t work when I think it more closely.> > One basic thing I fail to understand is the effect/importance of the > @param["id"] value on the Paginate. Can I use this with paginate just > like a normal action would use it?Yes. Pagination is just something your "normal" view is using to help split the result set.> > My other problem is how to actually use the paginate function to > retrieve my information. I know my variables are wrong because they > don''t work, but I don''t know what variable to use in my view files to > access the information of the page. Is it @page, @post_pages, @posts? > You''ve said some of these don''t work, but what is the right way to > access my information.Here''s an example from one of my projects: @model_pages, @models = paginate :models, :conditions => ["s.id = ?", @subpage.id], :per_page => 15, :order_by => ''models.art'' Here @model_pages is a Paginator object (see http://rails.rubyonrails.com/classes/ActionController/Pagination/ Paginator.html). @model_pages.current for example will then always represent the current page. @models will be an array of @model objects. It''s size is normally 15 (and something between 1 and 15 on the last page) because I''ve said in the above code that I want 15 models per page. In your case this number should be 1. If I now want to access single Model objects, I can do it like this: <% for model in @models do %> <%= model.art %> <%= model.edscription %> <% end %>> > Maybe my full show.rhtml will show you some idea of what I am trying > to accomplish. Keep in mind I am just trying to get it to work, not > display images and everything else - yet. Just reading information > from the database. > > show.rhtml: > > <p>The title is: <%= @posts.title %></p> > <p>The description is: <%= @posts.description %></p> > <p>The post was made on: <%= @posts.created_on.strftime "%e %B %Y | > %I:%M %p" %></p> > <p>The file is:<%= @posts.file %>Like said, @posts is an array of Post objects, not a single Post object, so you need to use @posts.first.title and so on (or @posts[0].title if that sounds more familiar). Hope this helps you a bit further. If you still have some problems, just ask for help. I would suggest reading through the api docs on paginator (http://rails.rubyonrails.com/classes/ActionController/Pagination.html) carefully, too. //jarkko -- Jarkko Laine http://jlaine.net http://odesign.fi _______________________________________________ Rails mailing list Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org http://lists.rubyonrails.org/mailman/listinfo/rails
On 25.4.2005, at 14:33, Michael Klein wrote:> I''ll look through all that and now go over the documentation a few > more times. I can''t thank you enough for your help. I''ll start working > more on that tonight. Thanks again, its starting to make some more > sense. > > > Like said, <at> posts is an array of Post objects, not a single > Post > object, so you need to use <at> posts.first.title and so on > (or > <at> posts[0].title if that sounds more familiar). > > I do have one more initial question. When you say this it makes me > think that I should use: @posts[(@params["id"]).title or something > like that to access the information. Does that work/make sense?No, it doesn''t. In your case @posts will only have one single item, @posts[0]. What that item is depends on your page number. Please don''t mix @posts here with an array returned from a normal Post.find call. In this case @posts will be different for every page (marked by @params["page"]). So in /posts/show&page=1 you will have the first item returned by your query, in ...&page=2 you will get the second and so on. //jarkko> > I have to go now and won''t be back until much later so I''ll say thanks > in advance now. All your notes have been extremely appreciated, best > of luck! > > Jarkko Laine <jarkko@...> writes: > > > > > Michael, > > > > On 25.4.2005, at 07:20, Michael Klein wrote: > > > > > I am trying to make a photoblog where I have a title, description, > > > date, and then MySQL while store the location of the image. My > goal is > > something similar to http://overshadowed.com. I''ve tried > it doing a > > manual way where users can just browse through > pictures and have links > > going to the next page by doing things > like ( <at> params["id"] - 1) and > > ( <at> params["id"] + 1). > Someone suggested trying the pagination feature > > and I am > attempting to understand it. > > > > Pagination is normally used on pages like Google search results > where > there''s a huge amount of results and they therefore need to > be split on > multiple pages. As far as I understand you are talking > about pages > showing only a single photo. While even this can be > achieved using > paginator, it''s not really something paginator has > been made for. > > > > > > > > I would like to use "Paginate" to make each page for each picture. > I > > might be unbelievably wrong in doing this, but from my > understanding > > of Paginate it makes sense. > > > > Well, yeah, I don''t see any reason why it wouldn''t work when I think > it > more closely. > > > > > > > > One basic thing I fail to understand is the effect/importance of > the > > <at> param["id"] value on the Paginate. Can I use this with > paginate just > > like a normal action would use it? > > > > Yes. Pagination is just something your "normal" view is using to > help > split the result set. > > > > > > > > My other problem is how to actually use the paginate function to > > > retrieve my information. I know my variables are wrong because > they > > don''t work, but I don''t know what variable to use in my view > files to > > access the information of the page. Is it <at> page, > <at> post_pages, <at> posts? > > You''ve said some of these don''t > work, but what is the right way to > > access my information. > > > > Here''s an example from one of my projects: > > <at> model_pages, <at> models = paginate :models, :conditions => > ["s.id = ?", > <at> subpage.id], :per_page => 15, :order_by => > ''models.art'' > > > > Here <at> model_pages is a Paginator object (see > > http://rails.rubyonrails.com/classes/ActionController/Pagination/ > > Paginator.html). <at> model_pages.current for example will then > always > represent the current page. > > > > <at> models will be an array of <at> model objects. It''s size is > normally 15 > (and something between 1 and 15 on the last page) > because I''ve said in > the above code that I want 15 models per page. > In your case this number > should be 1. > > > > If I now want to access single Model objects, I can do it like this: > > > > <% for model in <at> models do %> > > <%= model.art %> > > <%= model.edscription %> > > <% end %> > > > > > > > > > Maybe my full show.rhtml will show you some idea of what I am > trying > > to accomplish. Keep in mind I am just trying to get it to > work, not > > display images and everything else - yet. Just reading > information > > from the database. > > > > > > show.rhtml: > > > > > > <p>The title is: <%= <at> posts.title %></p> > > > <p>The description is: <%= <at> posts.description %></p> > > > <p>The post was made on: <%= <at> posts.created_on.strftime "%e > %B %Y | > > %I:%M %p" %></p> > > > <p>The file is:<%= <at> posts.file %> > > > > Like said, <at> posts is an array of Post objects, not a single > Post > object, so you need to use <at> posts.first.title and so on > (or > <at> posts[0].title if that sounds more familiar). > > > > Hope this helps you a bit further. If you still have some problems, > > just ask for help. I would suggest reading through the api docs on > > paginator > > (http://rails.rubyonrails.com/classes/ActionController/ > Pagination.html) > carefully, too. > > > > //jarkko > > > > -- > > Jarkko Laine > > http://jlaine.net > > http://odesign.fi > > > > Attachment (smime.p7s): application/pkcs7-signature, 2363 bytes > > > > _______________________________________________ > > Rails mailing list > > Rails@... > > http://lists.rubyonrails.org/mailman/listinfo/rails > > > --- > [This E-mail scanned for viruses by Declude Virus] > > _______________________________________________ > Rails mailing list > Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org > http://lists.rubyonrails.org/mailman/listinfo/rails >-- Jarkko Laine http://jlaine.net http://odesign.fi _______________________________________________ Rails mailing list Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org http://lists.rubyonrails.org/mailman/listinfo/rails