render a partial inside a model rails 3.2 - ruby-on-rails

I understand advice available on the net to never, ever, ever render in a model, for this case I need do it
class Order
def canceled_order_message(order)
admin = User.find_or_create_by(email: "myemail")
message = Message.new(subject: I18n.t(".buyer_has_canceled_the_order"),
body: "#{render_to_string :partial => 'users/messages/templates/sent_by_admin/canceled_order', :locals => {:order => order}}")
message.send_message
message.save
end
end
I need render inside message body attribute:
render_to_string :partial => 'users/messages/templates/sent_by_admin/canceled_order
How can I do it?
Thank you!

Please check my answer here:
undefined method `fragment_for' for nil:NilClass on render partial with cache
I think that could be useful.
P.S. Horrible idea, please rethink your architecture.

Related

HAML partials - partial does not recognized object which I queried for in the controller

I have this controller:
def index
#disclosures = Disclosure.where(:user_id => current_user.id)
respond_to do |format|
format.html{}
format.js{}
end
end
and with the help of the good folks at StackOverflow I am now able to get my HAML to point to the partial like this:
= render :partial => "/influencers/disclosures/shared/list"
but this partial throws and exception:
-if disclosures.empty?
.alert.alert-info
%p=(no_disclosures_message || (t "influencers.influencer_dashboard.disclosures.no_disclosures"))
%table.table.influencer-disclosures
%tbody
-disclosures.each do |disclosure|
=render "influencers/disclosures/shared/row", :disclosure => disclosure
saying that:
undefined local variable or method `disclosures' for #<#<Class:0x133ca8a58>:0x133ca25e0>
But how can this be? I just queried for that disclosures object in my controller. Any idea why this is happening and how to fix it?
Thanks!!
You need to put an # in front of disclosures. This is how the controller passes variables to the view.
-if #disclosures.empty?
and
-#disclosures.each do |disclosure|
Update
Another way to fix this is the change your render call. This will make it backwards compatible with other call sites of the same partial.
render :partial => "/influencers/disclosures/shared/list", :locals => {:disclosures => #disclosures}

Rails rendering file with locals using instance variable

I've just updated to Rails 2.3.11. In an earlier version I could write the following
render :file=>'console/users/show.html.erb', :locals => {:#user => #users.first}
which no longer works, instead I need to write
render :file=>'console/users/show.html.erb', :locals => {:user => #users.first}
which means to access the user in the file I would use 'user' but in the file I would like to use the instance variable #user as this same show file is called from the controller and passes #user
Any tips?
Thanks
Set #user before calling render:
#user = #users.first
render :file=>'console/users/show.html.erb'
The :locals option should only be used if you are passing local variables through.

render partials in actionmailer templates

I am trying to use an existing partial in an actionmailer template, something like..
My merchant_offer.txt.html.erb
<%= render :partial => "offers/offer", :locals => {:offer => #offer} %>
Notifier.rb (my mailer class):
def merchant_offer(offer)
subject "New Offer from #{offer.merchant.name}"
from "xxx#gmail.com"
recipients xxx#
sent_on Time.now
body :offer => offer
end
The offer partial in in another view folder called offers
But it throws a missing tempalate error.
Is there a way to re-use existing view partial in in mailer tempalates?
Thanks
You should be able to render partial from mailer templates.
I believe the error is in your merchant_offer view. Try renaming 'merchant_offer.txt.html.erb' to 'merchant_offer.html.erb'

Rails After Validated Save Go to Edit Path

This seems like a fairly simple problem to me but I have been having some issues.
In one of my views I use something like
<% if current_page?(:controller => "activities", :action => "new") %>
*Do something here*
<% end %>
and it does something specific on the new page for a form. Easy enough and it works great.
Unfortunately, I've found that when you have a "new activity" form (assume normal scaffolding controller), the url will go from
http://localhost:3000/activities/new
after submitting an error prone form to
http://localhost:3000/activities
but it will still show the new activity form with the respective errors. So basically everything works how it is supposed to EXCEPT that I need the url to be http://localhost:3000/activities/new for the current_page? function to recognize that it is indeed a new form page.
I'm wondering if there is some kind of work around to this issue. Thanks!
OH and here is the controller code, in case anybody needs to see it
Controller Code
def new
#activity = Activity.new
end
def create
#activity = Activity.new(params[:activity])
if #activity.save
flash[:notice] = "Successfully created activity."
redirect_to #activity
else
render :action => 'new'
end
end
Think you will need to check for create as well as new
<% if current_page?(:controller => "activities", :action => "new") or current_page?(:controller => "activities", :action => "create") %>
not so pretty maybe wrap it up in a helper method?
You could also check if the created at field is blank. As it won't be set till the activity is created.

Ruby on rails: Render 2 partials or figure out the kind of class and object is

I want to display different types of objects in the same ajax called controller function. I want to render out when I send back an ajax call. The problem is I want the "title" attribute of one object and the "name" attribute of another object. I can't render 2 partials and can seem to figure out how to check the type of object an object is. How do I solve this problem?
Here is my current controller setup (NOT CORRECT)
#objects = Obj.find(:all,:conditions => ["name Like ?", "#{prefix}%"])
#moreObjects = ObjTwo.find(:all,:conditions => ["title Like ?","#{prefix}%"])
if #objects.empty?
render :text => "No Matches"
else
render :partial => 'one', :collection => #objects
end
if #moreObjects.empty?
render :text => "No Matches"
else
render :partial => 'two', :collection => #moreObjects
end
try something like
<%= obj.title if obj.respond_to?(:title) %>
Here's one option that doesn't involve checking its type - in your Obj and ObjTwo classes add a new method:
def fancy_pants
title
end
def fancy_pants
name
end
Then you can combine #objects and #moreObjects - you'll only need one if statement and one partial.
#search_domination = #objects + #moreObjects
if #search_domination.empty?
render :text => "No Matches"
else
render :partial => 'one', :collection => #search_domination
end
You could use Object.is_a? but that's not a very clean solution. You could also try mapping your data before presentation to help keep your views lightweight.

Resources