I am using rails 3.2.8. This is how mailer class looks like:
class Newsletter < ActionMailer::Base
default :charset => "UTF-8",
:from => "\"Example\" <info#example.com>"
def campaign
attachments.inline['image1.gif'] = File.read("#{Rails.root}/app/assets/images/image1.gif")
attachments.inline['image2.gif'] = File.read("#{Rails.root}/app/assets/images/image2.gif")
attachments.inline["image3.jpg"] = File.read("#{Rails.root}/app/assets/images/image3.jpg")
mail(:to => "mytest#example.com", :subject => "Test")
end
end
When I send it, I only receive one attachment without main body. When I look at mail source I see other parts, but I don't understand why they are not showing as they should.
I checked other similar questions but nothing helped.
Please, help me. Do you need more code to solve the problem?
Regards,
Tomaž
Did you mention the attachments in your mail-content view file?
Add this code in you mail-content view file:
<%= image_tag attachments['image1.gif'].url %>
<%= image_tag attachments['image2.gif'].url %>
<%= image_tag attachments['image3.gif'].url %>
You can also have a look at a more detailed explanation available at:
http://technical-feeds.blogspot.in/2014/02/how-to-add-attachments-and-inline.html
Related
I am creating blog application in rails. I have a common form for creating and updating blog.
This is view of edit and new.html.erb
<%= render :partial => "form"%>
This is view of _form.html.erb blog:
<%= form_for #blog do |f| %>
<%= f.text_field :title, :placeholder => "Title" %><br>
<%= f.cktext_area :article, :placeholder => "Content", :ckeditor => {:toolbar => "MyToolbar"} %>
<%= f.submit %>
<% end %>
My blog is creating successfully but I am getting error on update action. This is my edit and update action in blog controller:
def edit
#blog = Blog.find_by_slug(params[:id])
end
def update
#blog = Blog.find(params[:id]) || not_found
#blog.update_attributes(params[:blog])
redirect_to "/blogs/#{#blog.slug}"
end
When I open form from edit view, and click on update button, it throws error:
No route matches [PUT] "/blog/2"
My routes.rb is:
resources :blogs
get 'blog', to: 'blogs#index'
get '/blog/:id', to: 'blogs#show', as: 'blog'
I am not getting where it is going wrong. I tried to add "url: blogs_path" in form_for, it removes the error but doesn't save the edit changes.
Can anybody help me where I am going wrong here?
Thank you.
Okay. I dont understand why you want to go against conventions. Anyway, using form_for resource would automatically generate action URL as a PUT to /resources/:id if its an update operation.
So to override this you need to do two things.
update your routes to support this:
Add this line to your routes file:
put 'blog/:id' => 'blogs#update', :as => 'update_blog'
It is important that you put this line above your 'resources :blogs` call.
2 . specify the URL to which the form should submit:
You will need to create the form tag like this:
<%= form_for #blog, :url => update_blog_path(#blog) do |f| %>
Try this and let us know.
I am looking for displaying company logo in the email when email is sent. I went thought action mailer base and there is attachment inline feature which supports images in mail.
I implemented it in this way:
in user_mailer.rb
def welcome_email(user)
#user = user
#url = "http://mealnut.com"
attachments.inline['mealnut.png']
mail(:to => user.email, :subject => "Mealnut: New Order #{order.id}")
end
in config/application.rb:
config.action_mailer.default_url_options = { :host => "mealnut.com" }
in welcome_email.html.web
<div class="logo">
<%= image_tag attachments['mealnut.png'].url, :alt => 'Mealnut', :class => 'photo' %>
</div>
But it is giving error:
undefined method `url' for nil:NilClass
Whats going wrong?
attachments and inline attachments differs only that inline will insert it via base64 but you need to attach a file anyhow to it!
attachments.inline['mealnut.png'] = File.read( Rails.root.join("app/some/path/","mealnut.png") )
than you are able to reference it in the view
hope that helped!
Suddenly flash and params aren't working for me. Even my normal flash error/success messages don't work.
This is just one sample - I have the following in a view:
<%= link_to 'New Comment', new_comment_path, :class => 'btn btn-primary', :onclick => flash[:worequest_id] = #worequest.id %>
And in the new comment form:
<% if flash[:worequest_id] != nil %>
<%= f.hidden_field :worequest_id, :value => flash[:worequest_id] %>
I have been using flash to pass data between a view and a form in quite a few places in my Rails app. Now, none of them are working!!
Could I have set some configuration that would turn off flash and params?
I appreciate your help!!
UPDATE
I read on another post, where someone was having "session problems" with FLASH. Where would I look for session problems?
UPDATE2
I added the following to the view that receives the data from the flash:
<% if flash[:worequest_id].blank? %>
<h3>flash blank</h3>
<% end %>
And "flash blank" showed up.
Really - This has been in many places of my app for months. And now it's not working!!!!!!!
UPDATE3
I also tried this:
<% flash.keep[:worequest_id] = #worequest.id %>
Is there a rails config file that deals with session? Maybe I messed up a parameter or something.
UDPATE4
This is the only line in my initializers/session_store.rb file:
Ndeavor::Application.config.session_store :cookie_store, key: '_ndeavor_session'
UPDATE5
Params don't work either !!!!!
params[:worequest_id]
UPDATE6
I just created a new app from scratch using Rails 3.2.11 and flash and params are working fine. So, it's not the version of Rails.
Im a bit confused about what/how you're trying to achieve.
Have you looked at the output produced by this?
<%= link_to 'New Comment', new_comment_path, :class => 'btn btn-primary', :onclick => flash[:worequest_id] = #worequest.id %>
I removed the jbuilder gem and now flash and params are working. It has to be something I did, because I just tried jbuilder in another app, and it didn't break flash. I'm going to try and figure out why my use of the gem caused the problem.
I am trying to save a model named 'customer' using form_for tag. I do not have a controller for this model, i was hoping to use another controller 'public' for this task. So here is my view:
<%= form_for #customer do |f| %>
<div class="field">
<%= f.label :name %><br/>
<%= f.text_field :name %>
</div>
.... and then
<%= f.submit 'Order', :action => :save_order %><br/>
and here is my controller
def check_out
#customer = Customer.new
end
def save_order
#customer = Customer.new(params[:customer])
credit_card_no = #customer.credit_card
#order = Order.new
#order.line_items << #cart.items
#customer.orders << #order
if #customer.save
# process credit card
#cart = nil
redirect_to(:action => :show_bill, :id => #order.id)
else
flash[:notice] = 'Could not process your credit card information'
render(:action => :check_out)
end
end
The view is loaded from action 'check_out' and it was supposed to go to action 'save_order' but i am getting an error in view for 'form_for' code, what am i doing wrong ? But if i create a controller or scaffold for 'customer' and try to use that, i get redirected to 'customer/show/:id' path, and i dont want that.
If you want to use different controller with form_for you need to use :url option in form_for.
<%= form_for #customer, :url => { :controller => "your controller name",
:action => "save_order" } do |f| %>
#your code
<% end %>
Sorry, but you are asking for help in how to make something work while doing it wrong. I will try to explain and I hope this will help you.
If you want to save a customer model you should use, guess what, a customers controller. Some people like to use scaffolds, some people hate them. But the fact that the scaffold code redirects to the show method after saving a model, which is easily changed, shouldn't stop you from using it. The scaffold is just there to help beginners and/or to have a way to come up with something quick and dirty. Changing the scaffold generated code here and there is not only usually necessary it's a good way to learn.
To save an order you should use, ::drumroll::, an orders controller. I don't even know what a "public" controller would do (which by itself should tell you at least that it is poorly named).
I suggest you get the Agile Web Development with Rails book and go through the depot application. It covers all of this very subject well and you will learn a lot.
I have nested resources like this in my routes.rb - (my rake:routes gist)
namespace(:admin) do
resources :restaurants do
resources :menus
resources :menu_items
end
end
In the controller:
def new
#restaurant = Restaurant.find(params[:restaurant_id])
#menu_item = #restaurant.menu_items.build
end
Trying to create a new MenuItem (action #new), by the url: http://127.0.0.1:3001/admin/restaurants/1/menu_items/new I get the error:
NoMethodError in Admin/menu_items#new
Showing /home/fps/workspace3/peded/app/views/admin/menu_items/_form.html.erb where line #1 raised:
undefined method `admin_menu_items_path' for #<#<Class:0xb6582d78>:0xb6581f2c>
Extracted source (around line #1):
1: <%= form_for #menu_item do |f| %>
...
How do I make this form work? It was created out of a nifty:scaffold
UPDATE
I also tried this in the _form:
<%= form_for [:restaurant, #menu_item] do |f| %>
But ended with a similar error:
Showing /home/fps/workspace3/peded/app/views/admin/menu_items/_form.html.erb where line #1 raised:
undefined method `restaurant_admin_menu_items_path' for #<#<Class:0xb68162b0>:0xb6813dd0>
Extracted source (around line #1):
1: <%= form_for [:restaurant, #menu_item] do |f| %
Should I file a bug?
form_for([#restaurant, #menu_item])
I think the problem is in the form. This worked for me:
<%= form_for(#menu_items, :url => restaurant_menu_items_path(#menu_items.restaurant)) do |f| %>
I'm having the same issue. The only solution I have found is to pass a url to the form_for.
<% url = (action_name == "new" ? {:action=>"create", :controller=>"admin/menu_item"} : {:action=>"update", :controller=>"admin/menu_item"})%>
<%= form_for [#restaurant ,#menu_item], :url=>url do |f| %>
One additional note, you will not get params[:menu_item] back, instead you will see params[:admin_menu_item].
Hope that helps you out!
You can look up your routes by running on the command line.
rake routes
It looks like you're calling your routes incorrectly.
Array notation would be:
form_for([:admin, #restaurant, #menu_item])
And the named route for create:
admin_restaurant_menu_items_path(#restaurant)
Dealing with nested resources and namespaces is a Vietnam (pita).
Here is my nasty solution:
= form_for #admin_menu_item,
:url => (#admin_menu_item.try(:new_record?) ?
admin_restaurant_menu_items_path(#admin_restaurant) :
admin_menu_item_path(# admin_menu_item)) do |f|
...
I hope you can help.
The only solution that worked for me correctly (for both new and edit resource) was:
form_for #menu_item, :url => url_for([:admin, #restaurant, #menu_item])
I'm using Rails 5 (not that I imagine it matters) and this worked for me:
= simple_form_for [:admin, #restaurant, #menu_item] do |f|
The fact that it's simple_form_for rather than form_for probably doesn't matter either.
Funnily enough, I'm building an app with the same exact resource names.