Render Erb from Database into View Problem Please Help!

render erb from database into view problem please help!

I think that you would need to pass the optional binding parameter to the ERB::render method. This effectively provides the local variables in the scope of the ERB template. In other words the binding needs to provide the image_tag variable to the template.

I don't know what 'content' is in your case but the following will pass the binding from the 'parent' view assuming that @obj.image_tag is visible from that view:

<%= ERB.new("image tag - \<\%= @obj.image_tag \%\>").result(binding) %>

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.

show.html.erb rendering as plain text mime time

The Linux admin for my host said they are running a version of Mongrel where this is a known bug.

He put a newer version of mongrel.rb in my /config/initializers and that solved the problem.

How to insert string into an erb file for rendering in rails 3.2?

I think i got your question.
you can append any html string in erb using
in view:

<%= render inline:"<p>new Field</p>"%>

or

<%= render inline: "<% products.each do |p| %><p><%= p.name %></p><% end %>" %>

or
in controller as:

render inline: "<% products.each do |p| %><p><%= p.name %></p><% end %>"

writing it in any _xyz.html.erb or xyz.html.erb and also can be used from controller, as well. for more check following link. in sub topic - 2.2.6 Using render with :inline.
rails guide

I have checked working of this. Let me know in case of any issue.

How can I render a partial from .js.erb file in rails 4?

Asset Pipeline

Firstly, you need to make sure that you don't use any Rails dynamic path helpers in your javascript directly.

The issue is that if you pre-compile your asset pipeline, you'll typically find these dynamic path helpers won't work correctly. Although there's nothing "wrong" with it - I tend to keep Rails back-end code completely separate from the front-end, as to ensure the versatility of the application:

#app/assets/javascripts/application.js
$(document).on("click", ".element", function(){
$.ajax({
type: "GET",
url: "/cookbooks"
});
});

This will send a "naked" Ajax request (no params / body) to your controller backend:

#app/controllers/cookbooks_controller.rb
Class CookbooksController < ApplicationController
def index
@cookbooks = Cookbook.all
respond_to do |format|
format.html
format.js #-> loads /views/cookbooks/index.js.erb
end
end
end

The trick here is that you can now populate your index.js.erb file with the params necessary to render the partial:

#app/views/cookbooks/index.js.erb
$("#property").html("<%=j render :partial => 'recipes/package_form' %>")

--

This should work for you

Render not working on second method in controller

In the error you can see that it's looking for html :formats=>[:html] or one of those variants: :erb, :builder, :raw, :ruby, :coffee, :axlsx, :haml. But you want it to look for a .js.erb. You need to add a respond_to method within the controller method like:

def checksim
@sim_number = Phone.where('sim_number = ?', params[:sim][:sim_number])

respond_to do |format|
if @sim_number.exists?
format.js {render 'phone-found'}
elsif @sim_number.blank?
format.js {render 'phone-not-found'}
else
format.js {render 'errors'}
end
end
end

ruby on rails render deosn't find the file

You "usually" don't render a form in an index action. Most form partials are setup semantically to expect a @my_resource, but if you're doing everything the rails way you're not going to have a instance variable during your index action. There's a number of ways you can do this but this is probably the quickest.

You probably have some collection (let's pretend you're using books) in your index action:

#views/books/index.html.erb
<% @books.each do |book| %>
...
<%= render "form" %>
...
<% end %>

You can just set an instance variable somewhere prior to rendering the form:

#views/books/index.html.erb
<% @books.each do |book| %>
<% @book = book %>
...
<%= render "form" %>
...
<% end %>

Another way to do it would be through passing in some locals to a partial. You'd have to change all of your references in _form to use a local variable instead. Then you can call render like this:

<%= render :partial => "form", :locals => {:book => book } %>


Related Topics



Leave a reply



Submit