Within my show action/view I'm displaying a lot of data that I want to split-up into separate pages (in this case three pages total). I can do this easily by adding a new action and view for each additional page, but is that the "correct" way to do it in Rails?
Great question!
I can do this easily by adding a new action and view for each additional page, but is that the "correct" way to do it in Rails?
I suspect you are unsure about violating REST?
I don't know what data you are displaying, but In the end the clearest and simplest solution for you (code wise) and your users (design wise) should win, if that means adding a new action, so be it. Avoid adding a new controller just for the sake of a new show action.
Are you looking for a pagination solution? If so, I would suggest either kaminari or will_paginate. Also, they each have railscast if you need any help getting set up.
Related
I have a page object and various template objects in my application. pages have names, descriptions urls etc and each have a relationship with a template. templates have different numbers of content boxes and relationships with other controllers (like blogs, galleries etc.).
When I am rendering a page I can work out what template is attached to the page, and what the relevant content is based on that. but I am not sure what the best way is to render the nested Items.
Are you meant to somehow render the templates view from within the other view? Or would you have to just rewrite the view altogether? In this case would I have to create an extra template view for each different template, bundle it with the page views, and then only include it if it is the right one?
Would this be the same for galleries and blogs? do they all need to be bundled with the page? Or can it be called from its proper location?
I'm not sure what the best practice is here and haven't had any luck googling it. I'm suspecting that the key words im using aren't correct. Or this is common knowledge that isn't worth documenting.
You can use shared partials to render views. Check out this guide.
In the views, you can render the partials based upon whatever condition you want.
For example:
- if params[:page] == "my_page"
= render "shared/my_page"
Naturally, you will still need to set up the needed data in the controller.
Shared logic for this can be placed in the Application Controller.
When to choose a partial or helper or a loop in view.
Specifically, when we need a repeated structure only in a single page.
I would like to know which one is a best practice from DRY point of view and Performance point of view.
Thanks.
If only in a single page, as described, I would consider using a loop or partial.
- If the output is one or two lines I might just use a loop.
- If more than that a partial
If used in multiple pages I would use a helper.
Overall I prefer helpers as they feel like a more object oriented, ruby approach and they are easy and great to have tests for!
Before I start work I would like to get some ideas. What I have is an MVC3 page that I currently use to display rows of data. There are many rows so I would like to filter them. Ideally at the top of my page I would like to have a select drop down box and a refresh button with rows of data appearing below when the refresh button is clicked.
I can imagine doing this with Ajax and then having the data from my controller populate new HTML text between a DIV.
Does this sound like the best approach? I am not looking for a person to write code for me. Just want to be sure my solution sounds like a good way to go.
thank you
i recommend this approach:
http://geekswithblogs.net/michelotti/archive/2008/06/28/mvc-json---jsonresult-and-jquery.aspx
You can 'enchance' it with AJAX of course, but do not forget about users with disabled javascript. Make it work without client scripting, then enchance it, when its working.
I also think that you can simply create controller action, that is accepting parameters like pageNumber and amountOfItems. Then in your controls at page, you can just change values (number of page etc..) and use them in call for your controller action at form submit.
I'm having a really hard time understanding routing.
Please help me with this problem.
Each of my controllers have these three actions right now
Users have Index, Create and Edit
Locations have Index, Create and Edit
Companies have Index, Create and Edit
The thing is, it all gets done through ajax.
I have jquery ui tabs with two tabs for each, Create and Edit
So the Index method is always the one that gets called for action links.
and inside this main view is that you can call(by clicking on the tab icon) the other methods that return an ajax view that gets output into the jQuery tab (I hope that's clear)
I have a sidebar with links to the controllers. and to specific methods of these controllers. The wanted behavior is that it should actually go into the Index Method and then with some logic autoload the wanted tab.
It all works just fine right now. But my urls are horrible.
To get to the create method for Users I have to go this url
http://localhost/Users/Index/1
http://localhost/Users/Index/2
I want the behavior of these links to be remapped to these links
http://localhost/Users/Create
http://localhost/Users/Edit
So even though it seems as if you are calling the Create method of the controller you are actually always calling the Index method.... (I know how to transform Create into "1" and Edit into two, so don't worry about that part
Hope that's clear.
Thanks in advance
Edit:
Just realized that this might not be possible cause then when I actually need to call the methods (with ajax) it wont know what to do.... am I correct?
Without seeing your controller action, you should be able to add a route something like this:
routes.MapRoute("myroute","users/{option}",new {controller="Users",action="Index"});
Is it best to create a separate view for authorized and unauthorized even if there will not be a lot of additional information in the authorized view? Or should there be one view and with model data adjusted accordingly?
EDIT: In MVC, I believe it better to have 2 views and then use partial views for the duplicate information. agree?
There is no "the best" solution. It's all depends on the situation. As for me I used not to create "almost identic" Views without important reason.
UPDATED:
I think fist you should try "adjusting" the ViewModel in the Controller and then passing it to the View. This makes your Views "more general"
I use a single view for both authenticated/unauthenticated states. I have helpers for the parts of the view that are for authenticated users only.
For example: if i have a "New Contact" link that i need to render onto the view but it should only be visible to authenticated users, then i'll use my helper (something like this):
<% =Html.RenderNewLink() %>
..that helper will first check if the user IsAuthenticated before it renders anything.
I'll have these types of helpers scattered throughout my views in the places where, for authenticated users, there would need to be more markup. And so, for the un-authenticated users, those places are blank/empty.
I hope this makes sense.. prob not the best way to explain it.