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
Related
I'm redeveloping a .haml form_for for an #edit_profile view to accommodate a controller where the #edit and #update methods have already been claimed. I had the form working before with the standard methods, but apparently Rails was doing a lot behind the scenes that I must now specify and I'm stuck.
I'm the sort who learns by mimicry and breaking things, so I need an example. I've googled around without any luck, so I'm hoping someone here can point me at one. Specifically: where do I put the method::patch and update_profile paths?
I just need the haml. I've established that the controller works with the new methods (#edit_profile and #update_profile), so all I have to do is connect the form to the controller.
Here's the repository if anybody needs that:
https://github.com/sidhene/MetPlus_PETS/blob/Update_CompanyPerson-%23146/app/views/company_people/edit_profile.html.haml
Thanks in advance,
A
The url argument needs to be to the update_profile action instead of the update action. Change update_company_person_path(#company_person) to update_profile_company_person_path(#company_person)
Edit: Your config/routes looks correct, but run rake routes just ot be sure. You should see a line with update_profile_company_person and PATCH in it.
form_for takes a value and populates its other HTML attributes from it (action / method etc):
form_for generates an appropriate form tag and yields a form builder object that knows the model the form is about.
As you see, the HTML reflects knowledge about the resource in several spots, like the path the form should be submitted to, or the names of the input fields.
The whole point of form_for is that you're meant to pass a resource / object to it.
Populated from the model, this object / resource will contain an array of data, such as the Class name etc, which Rails then uses to populate the form.
As per the docs:
...to create a new person you typically set up a new instance of Person in the PeopleController#new action, #person, and in the view template pass that object to form_for:
<%= form_for #person do |f| %>
The HTML generated for this would be (modulus formatting):
<form action="/people" class="new_person" id="new_person" method="post">
Thus, if you wanted to infer a different path / url for your form, you'll need to explicitly define it:
= form_for #profile, url: profile_update_path(#profile) do |f|
--
In your case, you're using a nested resource, which means that you have to pass both parts of the route as an array:
= form_for [:company_person, #company_person] do |f|
This should work fine for you.
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.
I have a partial that I am using in different pages. I want to conditionally hide a certain div inside the partial based on the view that is rendering it.
I was thinking about creating a page specific javascript file that would look up the div and and hide it.
But, if there was a way to retrieve the view name / page name inside the partial it would be central to the partial and would not necessitate loading the same javascript file in multiple pages.
Does anybody know a way to do this inside a partial
While #wahaj's answer would work, if you want to do what you need in a central location, you could check the controller_name and action_name variables in the partial to determine the view you're in (e.g. controller_name == "services" and action_name == "show" would let you know you're in the Show view for the Service controller)
you can get the name of the currently-rendering partial from within a Helper method with the following :
controller.view_context.view_renderer.instance_variable_get('#_partial_renderer').instance_values['path']
You could send the style parameter as a local variable to the partial, varying the parameter depending on where you're calling from. Something like:
render :partial => 'xyz', :locals => {:style => 'display:none or display:block'}
and in the partial you could do:
<div style=<%=style%>></div>
simple solution , inside your partial check if you need to show partial or not
_partial_name.html.erb
<%= content_tag :div, :style=> "display : #{show_div? ? 'block' : 'none'}" do%>
html...or other stuff
<%end%>
and then in application helper
app/helper/application_helper.rb
def show_div? #you can set name of your div like 'show_sidebar_div?'
#decide either show this div or not
action_name == 'show'
end
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.
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:
<% #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:
<% cache({:variable_id => :whatever}) do %>
<% #variable = Variable.find(:whatever) %>
. . .
<% end %>
Is there a common model that's being rendered in the main views that you could delegate the variable access to?
<%=h #model.variable %>