Rails 4 error: Template missing when saving bookmarks via email

I’m working on a social bookmarking app using Rails 4. Users email bookmarks to save them. Everything runs smoothly in my local environment, but on Heroku, I encounter an error:

ActionView::MissingTemplate (Missing template bookmarks/create_by_mail, application/create_by_mail with {:locale=>[:en], :formats=>[:html], :handlers=>[:erb, :builder, :raw, :ruby, :jbuilder, :coffee]}. Searched in:
  * "/app/app/views"
  * "/app/vendor/bundle/ruby/2.0.0/gems/devise-3.4.0/app/views"
):

I don’t understand why the system is searching for a view since this action is meant to be completely handled in the background. Here’s the controller code I’m using:

skip_before_filter :verify_authenticity_token, only: [:create_by_mail]

 def create_by_mail
   sender = params["sender"]
   subject = params["subject"]
   body = params["body-plain"]

   bookmark = Bookmark.new(
     title: extract_title(subject),
     link: subject,
     description: body,
     category_ids: match_categories(body)
   )
   bookmark.save!
 end

I also have private helper methods to extract the page title and match categories when processing the text. Any suggestions on why Heroku is looking for a view?

hey jack, i had similar prob b4. try adding render nothing: true at end of ur action. it tells rails not to look for template. also, check ur routes.rb file. make sure the route for this action is defined correctly. hope this helps!

I’ve dealt with this issue before. The problem lies in Rails’ default behavior of attempting to render a view after each action. To resolve it, modify your create_by_mail action to explicitly tell Rails not to render anything:

def create_by_mail
# Your existing code here
bookmark.save!
render json: { status: ‘success’ }, status: :ok
end

This approach sends a JSON response, which is more appropriate for background processes. It should eliminate the MissingTemplate error.

Additionally, ensure your routes are properly configured for this action. You might want to specify the format in your routes.rb file:

post ‘bookmarks/create_by_mail’ => ‘bookmarks#create_by_mail’, defaults: { format: :json }

This setup has worked well in my projects. Let me know if you need further assistance.

I’ve encountered a similar issue with Rails on Heroku before. The problem likely stems from how Rails handles controller actions by default. Even if you don’t explicitly render a view, Rails will try to find a corresponding template unless told otherwise.

To resolve this, try adding ‘head :ok’ at the end of your create_by_mail action:

def create_by_mail
  # Your existing code here

  bookmark.save!
  head :ok
end

This tells Rails to send a 200 OK response without rendering a view. It should prevent the MissingTemplate error.

Also, make sure your routes are set up correctly for this action. You might want to use a format constraint to ensure it only responds to the email format:

post 'bookmarks/create_by_mail' => 'bookmarks#create_by_mail', constraints: { format: 'json' }

This approach has worked well for me in similar situations. Let me know if you need any clarification!