render erb from database into view problem please help! - ruby-on-rails

i am saving some erb in my database and rendering it in the view like this:
erb = ERB.new(content)
render :text => erb.result
I am getting errors when trying render erb that has the image_tag in the erb saved in the database. The error is :
undefined method `image_tag' for main:Object
Anyone help on this ? i also get the error with the stylesheet_link_tag ?
Thank alot
rick

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) %>

It's because you haven't helper in your controller. You need include all Helper do you use.

Related

Rails: Partial Rendering fails with ActiveModel-Object

I am setting up a rails5 project using OrientDB as Database (ActiveOrient, https://github.com/topofocus/active-orient)
I have two Models "Basiswert" and "Aktie", both provide a method "to_partial_path" and "to_parm" and should be qualified for partial-rendering
fast = Basiswert.like 'fast*'
fast.first.to_partial_path
=> "basiswerte/basiswert"
fast.first.aktie.to_partial_path
=> "aktien/aktie"
Unfortunately
<%= render #basiswert.aktie if #basiswert.aktie.present? %>
fails with the following error
ActionView::Template::Error ('["id", 134]' is not an ActiveModel-compatible object. It must implement :to_partial_path.)
Any suggestion where to look to solve this issue?
EDIT:
Further Investigation reveals that it's possible to render collections,
i.e
<%= render [#basiswert.aktie] %>
renders the view correctly. Rendering the plain Object still fails with the error-message shown above.

Missing partial comments/_comment in rails

I'm using a tutorial to get a feel for ruby, I am very much a beginner. Before posting this I have spent a couple of hours trying to resolve this myself with no luck. Sorry in advance if my explanation isn't great:
So I am getting this error screen:
error message
I am following a tutorial to make a basic reddit style app and I am trying to add the comments functionality.
When you render a collection like render #comments, Rails will check the type name of the items in #comments (i.e. 'comment') then look for a partial under app/views/comments/_comment.html.erb by default (note the plural singular distinction between the partial name and the folder/collection name).
The following steps should resolve your issue:
Create a comments partial under:
app/views/comments/_comment.html.erb
Now, when you call render #comments, each item of your collection is passed to the partial as a local variable as the same name without the underscore:
In _comment.html.erb
<%# comment is defined because it matches the name of the partial %>
<%= comment.<some_attribute_on_comment> %>

Rails - return html black from model

I have a method in my model which is called in a modal like this:
<div class="col-sm-9"><%= #notification.notifier_context %></div>
In this method, I want to return some HTML like this, which has a rails helper path in it.
context = "<td><%= link_to('link', account_item_path(#item.account, #item))%></td>".html_safe
But its just out-putting the string as is. How can I get it evaluated to HTML proper?
Thanks
Try:
"<td>#{link_to('link', Rails.application.routes.url_helpers.account_item_path(#item.account, #item))}</td>".html_safe
String is not erb template so you can't use <%= %> syntax.
Also it's great idea to use helpers (with content_tag) or partials for that.
Also if you need to use routes inside model class, read following answer.

Template path in Rails 3

Let's say, I connected the route / to WelcomeController's index action.
Inside of the index.html.erb-Template I want to display the path of the template from Rails.root upwards, ie.
<h1> We are rendering: <%= how_do_i_do_this? %></h1>
to render to
<h1> We are rendering: app/views/presentation/index.html.erb</h1>
In Rails 2 I could access template.path, but this doesn't work anymore
Any ideas?
Because of how template rendering works in Rails, you will now be able to use __FILE__ for this instead. This works for me:
<%= __FILE__.gsub(Rails.root.to_s, "") %>
There may be a better way to do this however, but I couldn't find it when I went looking.
Ryan's answer works. If you also want to put your method in a helper, use Kernel#caller. Here is a method I'm using to do something similar:
def has_page_comment? code = nil
if code.nil?
# grab caller file, sanitize
code = caller.first.split(':').first.gsub(Rails.root.to_s,'').gsub('.html.erb','')
end
...
end

How can I create a "partial" that has its own action?

In my rails application, I render a partial on multiple pages, and in that partial is a variable. So currently, lets say I have 5 pages that render :partial => "partialname", and inside of partialname is #variable.
Can I have it so that partialname has its own action with #variable instantiated inside, rather than having #variable be called 5 times from each action that renders the partial?
Thanks!
I would create a before_filter on all the methods that need the common behavior.
But if you really want the partial to have its own "action," make a helper method that does whatever "action-y" things you want and then renders the partial. That works out to essentially the same thing. I've done this before to make a template-type partial that contains various pieces of data that need processing.
Rails Sub-controllers?
See my answer on this.
Very similar method here, using before filters either using controller inheritance or modules when needed.
So, is this a problem of code running 5 times per request that you'd rather not? Like, you've got a partial and in it is:
#my_var = MyModel.some_expensive_method
If so, you could just cache the result in the model:
def cached_some_expensive_method
#some_expensive_method ||= some_expensive_method()
end
you could load #variable from the view:
&lt% #variable = Variable.find(:whatever) %>
but some consider this bad practice in not adhering to strict MVC. This does have the benefit of supporting fragment caching out of the box:
&lt% cache({:variable_id => :whatever}) do %>
&lt% #variable = Variable.find(:whatever) %>
. . .
&lt% end %>
Is there a common model that's being rendered in the main views that you could delegate the variable access to?
&lt%=h #model.variable %>

Resources