Devise devise_error_messages! shows up without any event - ruby-on-rails

I have a devise_helper.rb file where I have:
module DeviseHelper
def devise_error_messages!
'Looks like you missing a password'
end
end
But whenever I go to /user/sign_up, it shows up. I wonder why that's still showing up without an action?

Take a look at the devise helper file
devise_error_messages! is just a stub. You need to override it with your own implementation of the error messages you want or copy the views to your app and change them to your preference. It looks like you are just printing out 'Looks like you missing a password' the way you have it now. Hope this helps point you in the right direction.

Related

Rails 4 Mailer, Amazon S3, and inline images

I have a Rails 4 application hosted on Heroku that serves assets from an S3 bucket. I am trying to customize my mailer (in this case, a customization of the Devise mailer) so that I can embed inline images into my emails.
Per the Rails documentation, the mailer should include code such as the following:
def confirmation_instructions(record, token, opts={})
# Prepare image for embedding
attachments.inline['logo'] = File.read("#{Rails.root}/app/assets/images/logo.jpg")
# Allow Devise to do its thing
super
end
And the view should contain the following:
<%= image_tag attachments['logo'].url, :style => "my styling here" %>
On Heroku, this fails with the following log:
ActionView::Template::Error (undefined method `url' for nil:NilClass):
"my styling here" %>
In other words, it looks like attachments.inline['logo'] is returning nil, and the view is then calling .url on nil.
I've tried numerous fixes and at this point am pretty exasperated. I know it must be something simple I'm overlooking and I hope somebody out there can point out where I'm going wrong.
Thanks in advance.
Try this:
File.read(Rails.root.join("app/assets/images/logo.jpg")
That's how I got it to work.

ApplicationHelper not loaded in Rails 4 engine

Long time reader of SO here. I'm working on a Rails Engine. The big picture problem is that I get a NoMethodError on a helper method living in my Engine's ApplicationHelper. This is for work so I'm going to call the engine Blorge.
I have my helper method that is causing issues anywhere it is called. The Helper method is returning a NoMethodError. I thought maybe I needed to manually add helper Blorge::ApplicationHelper to Blorge::ApplicationController but the issue is still happening.
Am I missing something fundamental about Engines here?
Here is some actual code to give you a better idea of what I'm looking at.
index_header partial
app/views/blorge/shared/_index_header.html.erb
# require_locals is the helper method in question here
<% require_locals ['title'], local_assigns %>
<% title = title.pluralize %>
<section class="main_content-header">
<div class="main_content-header-wrapper">
<%= content_tag :h1, title %>
<div class="main_content-header-save">
<%= link_to "New #{title.singularize}", #new_path, class: "add-button" %>
</div>
</div>
</section>
Pages#home view
app/views/blorge/pages/home.html.erb
<%= render 'blorge/shared/index_header', title: "Welcome, #{current_user.full_name}" %>
...
Engine Application Helper
app/helpers/blorge/application_helper.rb
module Blorge
module ApplicationHelper
def require_locals(local_array, local_assigns)
local_array.each do |loc|
raise "#{loc} is a required local, please define it when you render this partial" unless local_assigns[loc.to_sym].present?
end
end
end
end
Engine Pages Controller
app/controller/blorge/pages_controller.rb
module Blorge
class PagesController < ApplicationController
def home
end
end
end
Engine Application Controller
app/controllers/blorge/application_controller.rb
class Blorge::ApplicationController < ActionController::Base
helper Blorge::ApplicationHelper
...
end
If I restart the server and reload the page, it will usually work just fine, and once it works, the issue doesn't come back for a couple days. After reading Helpers in Rails engine and Rails Engines: Helpers only are reloaded when restarting the server it sounds like I need to include the helper in my application controller with the to_prepare method in my engine.rb file. I am going to try this next but I most want to know if I'm missing something very basic here, If i do just have to add it to engine.rb, can someone explain why?
This might have been too much information, but I'd rather give more than not enough. Thanks in advance.
Edit
the fix seems to have been adding the helpers to application controller within engine.rb. I suspected this would be the fix, but I still have no clue why this is. Does anyone know why I should have to do this?
The Solution
config.to_prepare do
ApplicationController.helper(MyEngineHelper)
end

Rails: method in a view

So, I tried searching (a lot :( ) and haven't been able to find anything to help.
I feel like this is something I've done before, but I just can't seem to figure it out.
I have installed a gem (Recommendable, yay!), that allows me access to a bunch of methods(?):
user.like(movie)
=> true
In a view, I tried putting <%= link_to "Like", #user.like(#movie) %>... however, this seems to actually just run #user.like(#movie) on page load... automatically setting that user to like that movie.
What am I missing? :\
Much thanks in advance!
You want to have a a LikesController for stuff like this I guess. And then you can remote link to the create action and inside this create action you can actually do the like you want to do:
class LikesController < ApplicationController
def create
# assuming you have some method to get you the current_user
# and assuming you just want to like movies
movie = Movie.find_by_id(params[:id])
current_user.like(movie) if movie
# maybe check for success and return some meaningfull message
end
end
and then inside the view you can do:
<%= link_to "Like", likes_path(#movie), remote: true %>
This should trigger the like.
Don't forget to create a route in routes.rb for the LikesController.
You should always keep in mind, that for an action a user should be able to take, you need an action in a controller. The view always just presents a given state to the user with options to take action.

Rails: Proper way to add functionality to rails methods

I'm just starting to tinker with extending the rails framework, and as an experiment, I thought I'd add some extra info inside the form_for helper. Specifically, when form_for is called, I'd like to generate an extra h1 tag such as:
# regular form_for <form> opening tag
<h1>Woohoo! It's added!</h1>
# tags fed into form_for via &proc
# form_for close <form> tag
At the moment I've added a /lib file that opens up ActiveRecord::FormHelper and overrides "form for". Needless to say writing out the whole form_for method with just the one added line added is dog ugly...but I can't call super() because, well, instead of inheriting from the method I'd like to super(), I've just overwritten it in /lib.
So, assuming I stubbornly want the functionality to be called via the same form_for tag (instead of, for example extended_form_for), what's the standard way for calling back to the original form_for method I'm overwriting? alias_method_chain? Thought I'd ask before I cement in some potentially lousy practices. If any hardened veterans could give an example I'd be appreciative.
Cheers
You could override form_for in your ApplicationHelper:
module ApplicationHelper
def form_for(*)
content_tag(:h1, "Woohoo! It's added!") + super
end
end
alias_method_chain is by far the simplest way to overwrite the method while still being able to call the original method. So in your lib file you'll want something like this:
def form_for_with_header(...)
form_for_without_header(...)
content_tag(:h1, "Header tag here")
# etc...
end

change the return of to_param by action? or, terribly broken edit action if perma-url is returned instead of id

I did some stuff to have to_param return a perma-url so I'd have seo friendly links. Upon creation and update it will generate and return the perma-url in a to_param method that I put in the model. However this causing me some grief in other areas. I have a form that looks like this:
<% #apps.each do |app| %>
<% fields_for "[id][]", app do |fields| %>
....
this fails terribly since it tries to insert the perma-url in place of the id. If I could just restrict to_param by action, that could solve things. If I change app to app.id, that fails since I have stuff like this: fields.check_box :is_featured . I suppose I could get the app instance, but that feels hacky.
Again please excuse my lack of rails-foo ;)
It is not seo friendly it is human friendly
And about problems with fields_for — try to use latest rails version and accepts_nested_attributes_for

Resources