I'm trying to render an image inline in an email, and my rails app is version 2.3.8.
Can someone provide me with an example of how to do this? Here's what I got so far, but I keep getting errors.
Here's my method:
def notice(contact)
subject 'notice'
recipients contact.email
from 'something.com'
sent_on Time.now
attachments.inline['paypal_seal.gif'] = File.read('/images/paypal_seal.gif')
body :contact => contact
end
And in the view:
<%= image_tag attachments['paypal_seal.gif'].url %>
This is the error I get:
undefined local variable or method `attachments' for #<ContractorNotifier:0x61413c8>
Thank you
What you are trying to do is the 'Rails 3' way of handling attachments. If you are starting out, I highly recommend upgrading to Rails 3. Otherwise, try an older 2.3 guide like:
http://blog.thoughtobject.com/2007/05/26/5/
Related
I am using a lynda.com tutorial for Ruby on Rails. The very first instance of creating and using array instance variables does not work. At first I thought it was that #array might have become a reserve word in the newer 5.0 version of rails that I am using, but changing it did not cause the "nil" (undefined) error to go away.
What is wrong with Ruby Rails 5.0? It is refusing to define instance variables and to pass them to the appropriate template.
This is extremely aggravating, since rails is not behaving as documented (i.e. RAILS IS BRAIN DEAD OUT OF THE BOX).
****************
demo_controller.rb
class DemoController < ApplicationController
def index
render('hello')
end
def hello
#zarray = [1,2,3,4,5] <------------ this is defined
end
def other_hello
render(:text => "Hello EVERYONE!")
end
end
******************
hello.html.erb
<h1>Demo#hello</h1>
<p>Hello World!</p>
<%= 1 + 1 %> <------ works
<% target = "world" %>
</br>
<%= "Hello #{target}" %> <----- works
</br>
<% #zarray.each do |n| %> <---- line 10. Rails claims that #zarray is
not defined
<%= n %></br>
<% end %>
ActionView::Template::Error (undefined method `each' for nil:NilClass):
I copied and ran your code and it worked fine. I guess it could be down to the fact that controllers should as a rails convention be pluralized, and your controller is called DemoController and perhaps you've called demoS#action somewhere? So, generate a new controller from your terminal called:
DemosController
with the generator:
rails g controller Demos
And copy paste everything from the old controller to the new controller.
And in your routes.rb you need to first make sure you have the correct resources :demos (the name of your model) which will give you the standard RESTful resources (index, new, create, etc), but as your 'hello' method is not a part of the RESTful api, you need to create a custom route for that:
get 'hello' => 'demos#hello', :as => :hello
get = HTTP method
'hello' = The URL you want to hello.html.erb to be reachable on: localhost:3000/hello
'demos#hello' = demos is the DemosController and #hello is the action in the controller.
':as => :hello' (_path)is the named helper you can call in a link_to on any page for instance: link_to hello_path.
I am new to Ruby and Rails. I tested using a plain STRING instance variable, and THAT got passed from my controller to my template, so the issue was the array definition. Apparently the older form of array definition that was in the tutorial that I was using no longer works in the version of rails and ruby that I am using.
I am using Ruby on Rails 5.0.1 with Ruby 2.2.4p230 (2015-12-16 revision 53155) [i386-mingw32] on Windows (rails/ruby compatibility is kosher).
#zarray = [1,2,3,4,5] <-- the array is not defined when passed to template
#zarray = Array.new << 1 << 2 << 3 << 4 << 5 <-- this works
It is a bit distressing that Ruby does not even BOTHER to complain about the format of the array definition. It just DOES NOTHING (i.e., refuses to initialize the array).
In the template:
<% #zarray.each do |n| %>
<%= n %></br>
<% end %>
I should add that #zarray = {1,2,3,4,5] works if you use it after #zarray = Array.new
In other words, at some point in the evolution of Ruby, EXPLICITLY classing arrays was introduced? It is not always clear in the documentation. The earlier tutorial I was using does NOT explicitly class the array.
The problem seems to be in the current RAILS version (5.0.1) that I am running. I has done SOMETHING to destroy the ability of Ruby to create an array using array literal syntax. When I run array definitions with literal syntax in IRB, I have no problem creating them. So Ruby is fine. It is a serious BUG in the 5.0.1 version of rails. So I think I should report it.
Welcome to Git (version 1.8.1.2-preview20130201)
$ irb
irb(main):002:0> service_mileage = [5000,15000,30000,60000,100000]
=> [5000, 15000, 30000, 60000, 100000]
irb(main):003:0> service_mileage[0]
=> 5000
irb(main):004:0> service_mileage[2]
=> 30000
irb(main):005:0>
I have removed gem routing-filter from my Rails 3.2.13 application in preparation to upgrade it to Rails 4. The author(s) have a beta version for Rails 4 but I could never get it to work. I have implemented the normal Rails process for localization/internationalization. The last issue I need to resolve is a link_to statement that displays a thread in a mailer view.
When I was using routing-filter I had the link_to statement as below where I set #host in my mailer depending on the environment.
<%= link_to "#{t :view_thread}", micropost_url(#item), host: "#{#host}" %>
When I uninstalled routing-filter I started getting errors because of a missing locale when the attempt is made to send the email. It crashes and sends the application back to the landing page. The gem took care of this.
I have been searching for about an hour trying to find the correct syntax for this but I'm not finding any examples that include both the host and locale parameters. I have tried several guesses but I get syntax errors.
Any help would be appreciated. I will keep searching and trying.
Here is the solution I came up with. I was not sure where the locale clause should go related to the host clause. I decided to try the statement below and it worked.
<%= link_to "#{t :view_thread}", micropost_url(#item, locale: I18n.locale.to_s), host: "#{#host}" %>
Updated solution for Rails 6 (and probably 5).
First, make sure you followed Rails guides on localization, so it's all set up
Then, add this to your ApplicationMailer:
def default_url_options(options = {})
ActionMailer::Base.default_url_options
.merge(locale: I18n.locale)
.merge(options)
end
This will use your selected locale for links generation, so you don't have to pass locale: #locale every time.
And then, set current locale in your mailers with the following block.
I18n.with_locale(#locale) do
mail(to: #email,
subject: I18n.t("mailers.my_mailer.subject"))
end
The last piece of advice - don't forget to fallback with your #locale var, so it's smth along these lines: #locale = #user.locale || I18n.default_locale
Update:
Or you can simply monkeypatch mail method, but make sure you know what you're doing.
def mail(**)
I18n.with_locale(#locale || I18n.default_locale) { super }
end
I have a Rails app deployed on Heroku. It's the Twitter app given in a lot of samples. I was trying to learn how to git a Rails app deployed on Heroku. Anyway, when clicking on the follow button, nothing happens. I used heroku logs and found this to be the issue:
NoMethodError (undefined method `[]' for nil:NilClass):
app/controllers/user_relationships_controller.rb:6:in `create'
The parameters passed are this:
Parameters: {"utf8"=>"✓", "authenticity_token"=>"NIiiOgQ4iowSxezGmvLk3oV/vul+4ysWoFrgh/1eOAY=", "user_id"=>{"follower_id"=>"1"}, "commit"=>"follow"}
Which corresponds to this line:
#user = User.find(params[:user_id][:follower_id])
Could someone please point me in the right direction? Thanks!
As requested:
<%= form_for current_user.user.build(:follower_id => #user.id),
:remote => true do |f| %>
<div><%= f.hidden_field :follower_id %></div>
<div class="actions"><%= f.submit "follow" %></div>
<% end %>
$('#follow_form').replaceWith("<%= escape_javascript render(:file =>'users/_follow_form') %>");
$('#followers').replaceWith('<div id="followers"><h1><%= "Followers: #{#user.followers.count}" %></h1></div>');
Some suggestions for how you can get more info and figure out what is going on:
Prove that it works locally. This doesn't seem like a heroku specific issue yet.
Figure out which tutorial you got this from, as there may have been a syntax error in an example that has since been fixed or highlighted in a comment on that site. (we don't have enough info to help you yet)
Add some logging, print out params prior to that call.
Push your code as a public github repo, and link back here. Then we can look at your code and run it to see what the issue is.
I'm terribly sorry I bothered you with this. For whatever reason, I had two controllers named very similarly. It was using the wrong one and I didn't realize it. I switched the code and now it works. Thanks for the input!
I recently started working with rails 3. I'm trying to add a multi-modal form into my application. I'm following the steps mentioned at Handle multiple models in one form
When I try to add a link via following helper function, I get
wrong number of arguments (1 for 2)
Code block below.
def add_task_link(name)
link_to_function name do |page|
page.insert_html :bottom, :tasks, :partial => 'task' , :object => Task.new
end
en
Googling for solution didn't take me anywhere.
Thank you.
You should find another tutorial, RJS and prototype are deprecated in rails 3, they've been replaced with jQuery.
If you really insist on going forward, you can bring RJS and prototype back by adding the following to your Gemfile:
gem 'prototype-rails'
This will bring back the correct version of link_to_function.
I'm upgrading my rails 2.3.8 app to rails 3.0.1 while using will_paginate in one module
for example:
<%= will_paginate #sample , :renderer => 'RemoteLinkRenderer' , :remote => {:loading => 'loadingPanel.show()',:complete => 'loadingPanel.hide()' } %>
This code isn't working.
Any ideas are greatly appreciated
I don't want to look rude but "This code doesn't working" isn't enough to be helped. You should provide more informations about your problem :)
You are not simply using will paginate. Apparently, you are also using this patch, and it is not working with Rails 3.
You should edit the question and update it's title to somthing like "remote pagination via Ajax with will_paginate and rails 3".
There is one html5-based solution described here
To convert this solution into non-html5 you may maybe do something like this(*):
$$('.pagination a').each(function(e){
var link=e.getAttribute('href');
e.setAttribute(
'onclick',
'new Ajax.Request(\'' + link + '\', {asynchronous:true, evalScripts:true})');
e.writeAttribute('href',null);
}
* has to be trimmed if there are multiple paginations...