Ruby on Rails View Rendering Db Info on Page

Ruby on Rails View Rendering DB Info On Page

Yes, fix is:

<div class="categories">
<div class="container blurbs">
<div class="cards row">
<% @categories.each do |c| %>
<div class="card col-xs-4" %>
<%= image_tag c.image, :class => "cat" %>
<h4 class="title"><%= c.title %></h4>
</div>
<% end %>
</div>
</div>
</div>

Look I removed = from this <%=.. In the below line :

<% @categories.each do |c| %>

#each method returns the collection after it completed its iterations. And due to this <%=, the return value of each which is @categories printed back. But if you use <%.. only, all above things will happen, but it wouldn't print back the object @categories.

Rails Rendering the data from DB

First of all, in index page I would write:

<% @companies.each do |company| %>
<tr>
<td><%= company.name %></td>
<td><%= company.place %></td>
<td><%= link_to 'VIEW', company %></td>
</tr>
<% end %>

this will show a list of companies. Next, a click on VIEW would take you for ex to: /companies/1 where 1 is the id of a company. That means you'll find company by id in show action of companies controller:

def show
@company = Company.find(params[:id])
end

see how I call variable - @company, not @companies cause I assume in show.html.erb you have used @company variable (or if it's created by scaffold it's for sure @company), also this is the page for one company only and it would be more appropriate to call it with singular (company not companies).

Doing all of the above you will use in show page for ex:

<%= @company.name %>

P.S. 1 You have the same naming issue for new and create actions, I would do it like this:

 def new
@company = Company.new
end

this will let me use this variable in a form like:

<%= form_for @company do |f| %>
....
<% end %>

and:

def create
@company = Company.new(params[:company])
if @company.save
flash[:success] = "Welcome to skillable"
redirect_to root_url
else
render 'new'
end
end

P.S. 2

to check if you have something in db - open rails console:

your/project/path/ rails c

x.x.x-pxxx :001 > Company.first
Company Load (18.2ms) SELECT "companies".* FROM "companies" LIMIT 1
=> nil

if result will be nil, means you have nothing in db, else - you have records inside.

Rails renders view when redirected

Ok, I figured it out. It was caused by browser cache. No rails error.

It was really an accident, I did some changes in a layout and loaded it in FF when signed in and then opened up a private window and observed the response, it was changed too. Then I changed the layout back and did another request, but noticed that the response did not change. When I reloaded it in my active session FF, it changed in the private window as well. So I disabled browser cache and it worked.

I still can't believe that this was it.

How do i render data from db into dashboard page generated by ActiveAdmin?

Yes, it is possible. You should be able to render a table based on an ActiveRecord query result. Instead of using the class name table_for Test do, you should use the ActiveRecord query table_for Test.all do or your query. For example:

panel "Pending Dealers" do
table_for Dealer.pending.last(5) do
column :id
column :name do |dealer|
link_to dealer.name, admin_dealer_path(dealer)
end
column :email
column :created_at
end
end

You can display any information using the Arbre Components o rendering an ERB file partial with render partial: 'important_information'

Render not rendering right page

run a rake:routes from your command line and you will see how they map.

    bills GET    /bills(.:format)          {:controller=>"bills", :action=>"index"}
POST /bills(.:format) {:controller=>"bills", :action=>"create"}
new_bill GET /bills/new(.:format) {:controller=>"bills", :action=>"new"}
edit_bill GET /bills/:id/edit(.:format) {:controller=>"bills", :action=>"edit"}
bill GET /bills/:id(.:format) {:controller=>"bills", :action=>"show"}
PUT /bills/:id(.:format) {:controller=>"bills", :action=>"update"}
DELETE /bills/:id(.:format) {:controller=>"bills", :action=>"destroy"}

The RESTful resources take a little getting used to but in your case \bills with a post method goes to the create action. You are specifying in the create action to render the contents of the new template when you call render :action => :new - you do not actually run the action.

Diagnosing the cause of slow view rendering

Ok, I figured it out (at long last). Without changing any of my actual assets I am now seeing this is development:

Started GET "/" for 127.0.0.1 at 2013-03-11 23:14:33 +1300
Processing by PagesController#home as HTML
Rendered pages/home.html.erb within layouts/application (1.3ms)

It turns out the delay was being caused by config.assets.debug = true inside of development.rb. Setting this to false resolves the problem.

Looks like the Rails core team debated turning this off by default, but decided against the idea. In the future I'd love to see them put something in the comment section of development.rb to tip off users of the potential for significant delays.

May I suggest the following:

# Expands the lines which load the assets 
# May cause significant delays in view rendering

Great, they heard me muttering and updated rails!

How to have Rails Views process HTML tags from database content?

You can use the raw helper method.

<%=raw @page.content %> 

Raw Database content showing up in Rails View

you don't need the = here: <%=@messages.each do |t|%>, the equals sign is telling erb to show every message on the view.

<% %>

Will execute Ruby code with no effect on the html page being rendered. The output will be thrown away.

<%= %>

Will execute Ruby code and insert the output of that code in place of the <%= %>

example...

<% puts "almost" %> nothing to see here
would render as

nothing to see here

however

<%= puts "almost" %> nothing to see here

would render as

almost nothing to see here

Look at <% %>(without equal) in ruby erb means?

How to pull and display data from database

You could include the whole table in the partial and render it inside your view (app/views/tasks/index.html.erb) for each one of your tables.

Example for app/views/tasks/_task.html.erb:

<table>
<tbody id="quadrant-<%= priority %>">
<% @tasks.each do |task| %>
<% if task.priority == priority %>
<tr>
<td><%= task.description %></td>
</tr>
<% end %>
<% end %>
</tbody>
</table>

Example for app/views/tasks/index.html.erb:

<h2>Priority 1</h2>
<%= render 'task', priority: 1 %>

<h2>Priority 2</h2>
<%= render 'task', priority: 2 %>

<h2>Priority 3</h2>
<%= render 'task', priority: 3 %>

<h2>Priority 4</h2>
<%= render 'task', priority: 4 %>

This will give you the flexibility to put each list wherever you want on your view, but this comes with a cost since you will iterate your task list 4 times (1 for each priority).

Although you could fix the this problem if you move the iteration out of the partial and include it in the view, inside a script that inserts each row on the correct table (quadrant).

Modified app/views/tasks/index.html.erb using JQuery:

<h2>Priority 1</h2>
<%= render 'task', priority: 1 %>

<h2>Priority 2</h2>
<%= render 'task', priority: 2 %>

<h2>Priority 3</h2>
<%= render 'task', priority: 3 %>

<h2>Priority 4</h2>
<%= render 'task', priority: 4 %>

<script type="text/javascript">
<% @tasks.each do |task| %>
$('#quadrant-<%= task.priority %>').append('<tr><td><%= task.description %></td></tr>');
<% end %>
</script>

Just replace hard-coded priority numbers (1..4) with your variables and this should, at least, point you in the right direction.



Related Topics



Leave a reply



Submit