How to call a ruby on rails controller when view is loaded? - ruby-on-rails

I am pretty new to Rails, basically what I want to do is pretty simple, when the view is loaded I want it to call a controller that executes a ruby script and render the image generated by this script on my view, is this possible? Or should I call the script before the view is loaded and just retrieve the generated image?

First of all what i would suggest you to go and learn here a link for your help
ruby on rails tutorial
Answer to your problem
In rails controllers are loaded first, each view is associated to some controller action, forexample if you have users controller(app/controllers/users_controller.rb) and in show you want to display the users image as a profile picture, what you can do is
def show
#user = User.find(params[:id])
end
and in its corresponding view(app/views/show.html.erb)
<img src = <%= #user.image_path %> />

I would retrieve the image on load of the page, hide it with css and load it anytime you want with javascript. I don't see a big reason for not calling it on the load of the page.

In RoR a controller action is rendering the view, so you can set it at the initial load itself.
For example, you have the Users controller and you need to show all users images in the index view.
controllers/users_controller.rb
def index
#users = User.all
end
views/users/index.hrml.erb
<% #users.each do |user| %>
<img src=<%= user.image_path %>> </img>
<% end %>

Related

Display navbar on all pages except home page

I want to show two different navbars. One will be displayed, on all pages except home page, when you're logged in. While the other navbar will be displayed just on my landing page.
I am thinking that I will probably need to write an if statement.
If (current user is not logged in) or maybe (current user is viewing home page) do
<nav>Second navbar</nav>
else
<nav>First navbar</nav>
end
I am very new to rails, so I could be wrong. (And yes, I know that's not how to correctly write an if statement in Ruby)
Home page is located at:
home/index.html.erb
I normally do following setup:
create partial shared/_nav_menu.html.erb
inside partial I put logic like:
<% if current_user %>
// nav bar for logged in user
<% else %>
// nav bar for non logged in users
<% end %>
Then inside application.html.erb file I render the partial like this:
<%= render :partial => 'shared/_nav_menu' if show_menu? %>
Inside my application_controller I put logic like this:
def show_menu?
true
end
helper method: show_menu?
If I don't want to show the menu for static_pages then inside static_pages_controller I overwrite show_menu? method to return false.
class StaticPagesController < ApplicationController
def show_menu?
false
end
helper_method: show_menu?
end
You don't have to use exactly this setup but I like this setup because my nav menu logic is kept seperate in partial. All logic required to nav menu lives in this file.
This approach don't bloat my application.html.erb file with lots of if..else.
In the case of two menus, but leaving yourself open to more, I would use a similar-but-slightly-different approach than Reboot's answer.
In the layout:
<%= render :partial => #nav_bar_partial %>
Then in my application controller, define the default nav:
def standard_nav
#nav_bar_partial = "path/to/standard/nav/partial"
end
From there, you can override that nav partial any time you need to (with any partial you want) from your controller
#nav_bar_partial = "path/to/new/nav/partial" if condition_that_requires_a_different_nav
That way, you have a little more flexibility. If for some reason you want to add a third nav bar for some other condition, you can just override the partial elsewhere without changing any of the above code.
You can prepare two layouts, one for the landing page and another for rest of the pages. Take a look at the official documentation: http://guides.rubyonrails.org/layouts_and_rendering.html.

Render show view in a partial - Ruby on Rails

So I have a sidebar in my rails application. Basically, I have a grid full of images. When someone clicks on an image in the grid, I basically want the show view to be displayed in the sidebar. I'm clueless on how to do this.
I've tried copying and pasting the show view's html and erb into my _sidebar.html.erb partial. But I get variable errors. My partial is located in my layout folder. But I can move it if needed.
Here is an image of the application for a better idea of what I'm talking about.
Here's how to do it:
1) Include the sidebar partial in your layout:
#app/views/layouts/application.html.erb
<%= render "shared/sidebar" %>
This calls the _sidebar.html.erb partial in /app/views/shared:
2) Populate the "default" sidebar partial:
#app/views/shared/_sidebar.html.erb
...
The important thing to note here is that partials should never have #instance variables inside.
Partials have locals, which you can pass with the following command:
<%= render "shared/sidebar", locals: { variable: "value" } %>
Partials are meant to run on any part of your web application, and since #instance variables are present for a single "instance" of an object (IE #post won't be available in /users), you can ensure partials are always populated by passing local variables to them.
This is why you received errors when copying your post#show code to your sidebar partial -- the instance variables present for the show action will not be present in other parts of the app.
3) Use JS to pull image objects
When someone clicks on an image in the grid, I basically want the show view to be displayed in the sidebar.
You need to be more specific - do you really want the "show" action to appear, or do you want parts of its functionality?
Either way, you'll be best using JS & ajax to pull the data:
#app/controllers/images_controller.rb
class ImagesController < ApplicationController
layout Proc.new { |controller| !controller.request.xhr? }
end
#app/assets/javascripts/application.js
$(document).on("click", "img", function(e) {
e.preventDefault();
$.get($(this).attr("href"), function(data){
$(".sidebar .image").html(data);
});
});
This will take the rendered images#show action (without layout), and append it to your sidebar partial. This is what you asked for - I can change it if required.
Lets say that your model is called "Image".
In your "images/show.html.erb":
# This is a handy shortcut that will call the 'images/_image' partial.
# Those two lines are exactly the same.
<%= render #image %>
<%= render "images/image", image: #image %>
In your "_sidebar.html.erb":
<div id="sidebar-img"></div>
In your "images/_image.html.erb":
# Code displaying your image, don't use any global variable.
# 'image' is the name of the local variable
In your grid:
# remote: true allows you to do an AJAX call
<%= link_to image, remote: true do %>
# Something like that, I don't know your code
<%= image_tag image.url %>
<% end %>
Create "images/show.js.erb", when clicking on a 'remote :true' link it will call this file.
# set #image to Image.find(params[:id]) in your controller
$("#sidebar-img").html("<%= render #image %>");

How to display a resource from a different controller?

I'm creating a mostly static home page for a website, except for one little container that needs to pull web links from a model database. The home page view is sitting in home_controller which I created earlier in development. I've now created the link resource and links_controller.
I don't know much about rails and what I've been doing up to now is displaying resources in the index view for their designated controller. However, i need to display this resource in the index view for the home_controller.
Can someone guide me in the right direction towards doing this?
Do a query to fetch the required link objects in the index action of home_controller and display it in the view, as you want:
# HomeController
def index
# other code
# #links = Link.all # or any other query you may want to do
end
# in the view index.html.erb
<% #links.each do |link| %>
<%= link.url %> <br/>
<% end %>
Customize it as per your needs.

Link to random record in header partial (rails)

I'm wondering how to create a "shuffle" button in the header partial of my rails app that links to a random record. I have a "pins" table and each pin is a video.
I've searched through stackoverflow and couldn't figure out how to do it..
I think it had to do with the header partial doesn't work with the Pins controller/model.
When someone clicks the button it should link to something like this:
website.com/pins/13
Any help would be great thanks!
EDIT:
This is the code I have previously tried:
offset = rand(Model.count)
rand_record = Model.first(:offset => offset)
But I am new to rails and I wasn't sure where to put it. I tried putting it in the model and the controller and both didn't work.
Ok, so I'm assuming that you want a link to a random Model to be shown each time a user loads a particular page. Let's say that the page that shows this link is the ModelController#index action.
Since the randomization only happens when the page is initially loaded, you can do it in the controller action:
class ModelController < ActionController::Base
#other actions
def index
#any other index code
#random_model = Model.order('random()').first
end
end
Now, in your view, you can link to that model in the usual manner:
<%= link_to("Shuffle", #random_model, :class => "btn btn-small btn-warning") %>
Every time that the controller action executes, it will pick a Model at random, and include a link to that Model when it renders the page.
Edited to address:
"Is there anyway to make it work without putting the code in the index and show actions?"
Yes. You can actually load the model right in the view code. Normally, assigning it to an instance variable in the controller is the 'more correct' method, but as you point out, it leads to duplication of code. If this is something you want to include in multiple views, I would recommend making it a partial. Something like so:
views/shared/_shuffle.erb:
<%= link_to("Shuffle", Model.order('random()').first, :class => "btn btn-small btn-warning") %>
And then rendering that partial in any page you want to include a randomized link:
<%= render 'shared/shuffle' %>
Note that if you render this partial more than once in a page, the random model will be different for each link.

rails navigation and partial refresh

Thanks for your time!
I get some reports data on my hand and I want to present these data on the web. The view of the HTML will be divided into two parts, the left part and the right part. There's a tree view in the left part consisting of the report names. In the right part presents the contents of the report.
What I want to achieve is when I click the report name on the left part, it will call an Action in the Controller, and passed the report name as parameter. The Action will fetch the data from the database and represent the data in the right part. And now I am stuck on how to realize this kind of view.
I've Googled a lot on the Internet and found Frameset, Partials or Ajax may capable of this. Because I've never developed web applications before and also new to Rails. So can anyone give me some advise or suggestion?
Things I've already known :
I've used Frameset to accomplish a view like this. But I found it needs a lot of .html files and all these .html files are static. And many people don't suggest it at all.
Then I've Googled Partials. But it seems Partials don't call the Action. It directly loads the _partials.html.erb to the main view. And besides, how can I control the layout? Using CSS?
I've never used Ajax.
If you want a fluid, seamless transition between one report and another, you should use both AJAX and Partials.
The way that it works is something like:
Make a left column in the html that has some links
Make the right column inside a partial
Assign the links to jQuery listeners to call the AJAX.
I'll put a bit of code here to show how it works:
Controller:
def index
reports = Report.all
if params[:report_id]
reports = Report.find(params[:report_id]
end
respond_to do |format|
format.html
format.js { render :template => "update_reports" }
end
end
update_reports.js.erb (in the same folder as the report views):
$('#report_viewer').html('<%= escape_javascript render :partial => "report_detail" %>');
In your view:
<div style=float:left>
<ul>
<li><%= link_to "Some report", "#", :class => "ajax" %></li>
</ul>
</div>
<div style=float:right id="report_viewer">
<%= render :partial => "report_detail" %>
</div>
<script type='text/javascript'>
$(document).ready(function() {
$(".ajax").click(function(e) {
$(this).ajax("your route to the action");
}
});
</script>
I think it's basically this, now let me explain a few things:
I don't remember if you have to do this, but in my case I created a new custom route to force the call to the action to be a json call instead of a html one. You can do this by adding :format => "js" to your route
You must name all your partials like "_yourname.html.erb". Rails won't recognize partials without the leading underscore.
In the controller, everything that comes after "format.js" is optional, you don't need to specify the template name, and if you don't Rails will look for the file index.js.erb.
The update_reports.js.erb file is basically a callback javascript that executes to update the current page. It finds the div where the partial is, and updates it rendering a new partial with the new report.
In the view, the link to change the report don't need to be a link at all if you're using the jQuery.click listener, but if it is a link, it must have the href as "#", or else the browser will just try to redirect to that location.
There are several ways to hook your link to the ajax function, I just chose the one I like it better, but you also could have a named function and call it in the html tag "onClick='yourFunction()'".
You need jQuery to call ajax like this. If you're sing Rails 3.0 or lower, you should replace the default Prototype with jQuery, because it's much better (IMHO), but I think prototype also have some ajax features.
It may seem complicated, but once you get the idea of it it'll become simple as writing any other action.
In the js callback file you could also add an animation to smooth the transition, like a fading. Look for the jQuery fade function for more info on this.
This is quite an open question so don't take this answer verbatim, but merely as a guide.
app/views/layouts/reports.html.erb
<!DOCTYPE html>
<html itemscope itemtype="http://schema.org/">
<head>
# omitted
</head>
<body>
<div id="body-container">
<div id="left-column">
<ul id="reports-list">
<% for report in #reports %>
<li><%= link_to report.name, report %></li>
<% end %>
</ul>
</div>
<div id="right-column">
<%= yield %>
</div>
</div>
</body>
</html>
app/controllers/reports_controller.rb
class ReportsController < ApplicationController
before_filter { #reports = Report.all }
def index
end
def show
#report = Report.find(params[:id])
end
def edit
#report = Report.find(params[:id])
end
def new
#report = Report.new
end
def update
# omitted
end
def create
# omitted
end
def destroy
#report = Report.find(params[:id])
end
end
routes.rb
YourApp::Application.routes.draw do
resources :reports
root to: "reports#index"
end
This would achieve the effect your after using just rails, of course adding ajax could add a better user experience.

Resources