Justas Justas - 10 months ago 73
Ruby Question

Rails 5 ransack gem, syntax error, unexpected end-of-input

maybe after all day non-stop coding I'm blind, but I cant seem to find any errors, missing end's or mistypes.

I'm trying to use ransack search gem in rails 5. My code looks like this:


class ApplicationController < ActionController::Base
before_filter :site_search

def site_search
@search = Post.ransack(params[:q])
@search_posts = @search.result(distinct: true)


class PostsController < ApplicationController
def index
@search = Post.ransack(params[:q])
@posts = @search.result(distinct: true)

Form looks like this:

<div id="search">
<%= search_form_for @search do |f| %>
<%= f.search_field :title_or_body_cont, placeholder: 'Type to search' %>
<%= f.submit %>
<% end %>

Error looks like this:

/.../myapp/app/controllers/posts_controller.rb:6: syntax error, unexpected end-of-input, expecting keyword_end

Extracted source (around line #6):
4 @posts = @search.result(distinct: true)
5 end
6 end * Red line here, problem with this line i guess *



{"utf8"=>"✓", "q"=>{"title_or_body_cont"=>"e"}, "commit"=>"Search"}

Crash occurs when I click search button. There is something wrong with site_search method, because when its in controller, then all other Post classes methods crash too ( with same error ), but if I remove site_search method, then all the other methods runs fine.

Answer Source

This is occurring because at the end of line 5 of your posts controller, you had, before the line break, Unicode character 65279, ZERO WIDTH NO-BREAK SPACE. This character was directly adjacent to the d in end. I can't speak to the exact decisions the interpreter makes about handling unusual whitespace characters in different versions of Ruby, but at a guess I suspect it was interpreting your end* (with * representing the invisible character) as a four-letter identifier that would be interpreted down the road as a variable name or method call, rather than the three-letter keyword end.