Rails - return html black from model - ruby-on-rails

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.

Related

ActiveSupport::SafeBuffer not rendered in view?

Very simple question really, but it's driving me nuts.
I have this method call in a Rails view:
<%= get_image(#document) %>
The method in here returns an object of type ActiveSupport::SafeBuffer. If I call .to_str on it in a console, I see the expected result (just an html img tag). However, when I stick the above code in a view, there's nothing there. Just an empty string.
If I do this:
<%= get_image(#document).to_str %>
in an effort to convert the SafeBuffer into a String object, I see the literal HTML in the page, and not the image tag.
I feel like I'm missing something simple or elementary here. Can anyone point the way? Thanks in advance!
EDIT: since it's been requested, here's what the get_image method looks like:
def get_image(document)
document[document.type+'.image'].nil? ? nil : document[document.type+'.image'].as_html_safe
end

Where do I put this helper function, in the model or post_helper or?

All my controllers inherit from applicatin_controller.rb, and I added:
helper :all
I want to use this function in my view to make url's like:
/post/some-title
instead of using the ID int he url
def post_path_for(post)
post_path(:id => post.title_parameterize)
end
This is rails 3.
Can't you use "to_param" in your model to change that without having to write a helper?
http://apidock.com/rails/ActiveRecord/Base/to_param
It depends on how you want to use it.
If it's in helper, you could call <%= post_path_for post %> in your view.
If it's in model, with small change you could call it like this: <%= post.path %>
Although second way is shorter, I usually put such functions in helpers, for the sake of separation of logic and presentation.
I agree with Nikita. It sounds like a helper to me. I use helpers for anything that is meant for display. It sounds like you want this available to all views. If that is the case, I would place it in helpers/application_helper.rb
+1 for Robin's to_param method. By using to_param, you could use the built-in url helpers (like link_to, url_for, ...natively)
For the other point, you mentioned you put it in controller and wanted to use it in view. You need the following line:
helper_method :post_path_for

render erb from database into view problem please help!

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.

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

MVC 2 RenderVirtualPartial helper

I would like to create a HTML helper that works the same as Html.RenderPartial except I would like the partial to be rendered from ViewData or a model object and not a file on the filesystem.
example:
<% Html.RenderVirtualPartial("Name", Model.MyPartialContent") %>
I can return string in my helper but i need to have code tags (ie <%= DateTime.Now %>) interpreted so assume I need do some kind of binary output stream writer?
thanks in advance
-Mark
why not add the futures and you can use renderAction, or you can render partial put pass a model over also

Resources