Is there a way to render a partial view with accompanying code in rails?
For instance: I want to be able to create a partial view which will show the top 5 foobars on my site. This partial needs accompanying code to retrieve some foobars from the database, rank them according to an algorithm, and then output the view with the top 5.
I want to be able to include this partial on any page I fancy, preferably just by using something like
<%= render :action => "top_five_foobars" %>
Is this doable? I'm used to asp.net mvc where you can create an action that runs some code and returns a partial, but it seems like in rails it returns simply the template...
If google get you here, you might be looking for cells
Cells are view components for Rails. They are mini-controllers with
their own MVC stack, can invoke logic and render views. They bring
back OOP to Rails' view layer and make writing reusable portlets for
your applications fun.
You need something like a shopping cart, which appears on almost
every page of your app.
You wouldn't use a partial and a helper, would you?
It might not be the cleanest way, but what I did is that I created a helper method in the Application Controller that retrieves the top 5 foobars. Then I call this method in the views. I also cached the part of the view that shows the results.
Related
I have the two Ruby on Rails models SafetyTest and TeamDue, and want to create a partial called _seasonal.html.erb that can work with either models.
The real problem is adding a link to create a new instance. For example, it would be this code for just SafetyTest:
<%= link_to new_safety_test_path %>
Now I want to be able to specify in my view, when I'm rendering _seasonal.html.erb, whether I want such a link for SafetyTest or TeamDue . I'm not sure what local to pass to this partial so that it creates the right new link without making a mess.
How should I go about doing this?
Take a look at Polymorphic URL helpers.
I'm writing a Rails 3.2 app with backbone, and since I only need rails to render one page, I have no need for a controller to back the index page.
Is there a way to render the layout (application.html.erb) without a controller? I imagine it would be a configuration in the routes.rb file?
My first thought was to move it to index.html in the /public directory, but I need to take advantage of erb for javascript includes and CSRF helpers, etc.
I get that you don't need the controller to do anything, but Rails is "opinionated" software; it expects a controller and a view, because that is the way it was designed, and trying to work around that is going to give you a lot of trouble.
Just
create an empty controller class in /app/controllers/main_controller.rb
create an empty view file /app/views/main/index.html.erb
set up a route like :root => 'main#index'
Easy peasy.
I am confused when I should use a custom widget or renderPartial in my view files. Sometimes I use widget and sometimes I use renderPartial.
Widget
You use widget when your application logic is defined in a separate CLASS file and the logic is somehow separated and standalone.
Widget's are chosen when the functionality is repeatedly used elsewhere, on lot of pages.
renderPartial
You use renderPartial for VIEW files that you want to embed into something bigger, or when you want to print something without using the application layouts.
renderPartial is chosen when all the variables it need to access are already prepared in the current action.
Widget
You can use widget when your site has some common part like header and footer or sometime some kind filter which require on every page of site.
renderPartial
Take example of search form of yii crude which is called by using renderPartial because that serach form is changing according to requirement of pages.
Sorry for english.
I have a problem and I dont know how to solve it.
I have a user that log in a web site, and I identify them by session[:user_id] and user has a status page that is defined in user_controller and in status view.
So, I would like to make one page for admin, to see all the statuses from all users on one page, using already written controller and view.
Is that possible?
I could rewrite some of the code, so that I could call with params, like ?user_id=70 and then set session[:user_id]=params[:user_id], but it would be painful if I will have to rewrite whole statuses, beside i would have to maintain same code on 2 different places.
Thank you.
Dorijan
If you need more functionality in your controller, you can add more actions to it. You can also do that in a resourcefull way.
On the other hand it usually is best practice to keep controllers thin.
See: ActionController
In order to make your views reusable, you should use partials.
You could make a _user_status partial.html.erb and render a single partial for a user render all of them for an admin.
Checkout: Layouts and Rendering in Rails
This is a noob question. My applets_controller has partials that will be called from views associated with other controllers.
(e.g. applets_controller _applet1.html.erb called from user_home_controller show.html.erb)
my link in show.html.erb is:
<%= render :partial => "applets/applet1" %>
I would prefer not to duplicate code to get variables from the applets_controller in all of the controllers of the views that will call the applet. How can i avoid doing this? I suppose i could transfer much of the code to the application_controller or to models. Is there a simpler solution?
Thanks.
I think you could put the relevant code & output into a Helper, and change your calls in the partial (_applet1.html.erb) to refer to that helper. That way, it would be available anywhere in your application. Depending on which helper you put it into, you may have to include a line to include AppletHelper or something similar.