Syntax Error in code for 'bootstrap Custom forms' in Ruby - ruby-on-rails-3

<%= form_for :content, url: contents_path, method: :post do |f| %>
<%= f.label :inout %> <!-- 수입 지출 목록 -->
<%= f.text_field :inout %><br>
<div class="input-group mb-3">
<div class="input-group-prepend">
<label class="input-group-text" for="inputGroupSelect01">Options</label>
</div>
<select class="custom-select" id="inputGroupSelect01">
<option selected>Choose...</option>
<option value="1">결혼식</option>
<option value="2">장례식</option>
<option value="3">돌잔치</option>
</select>
</div>
<% if option == 1? %>
<%= f.hidden_field :category, value=>"결혼식" %>
<% end %>
<%= f.label :title %>
<%= f.text_field :title %>
<%= f.label :cost %>
<%= f.text_field :cost %>
<%= f.label :memo %>
<%= f.text_area :memo %>
<%= f.submit %>
<% end %>
I'm getting the following syntax error in my code :
Unexpected ";", expected ";" .... syntax error, unexpected keyword_ensure, expecting_end_of_input
enter image description here
If I select option value one, I store "결혼식" in category
and I want to see "결혼식" in the show view
Can you identify what is causing the error?

Did you copy and paste the code from somewhere else? It's quite possible there might be some zero-width space or other hidden characters in your code that the ruby compiler is not liking.
You can use sublime text on OS/X or Notepad++ to try and see if there are hidden characters in your code.
If you're on OS/X or Linux, you can use Sublime Text and this plug-in to identify those characters
https://pastebin.com/ehWxNfMe
# http://sublimetext.userecho.com/topic/104394-is-it-possible-to-show-all-characters-spaces-tabs-cr-lf-etc/#comment_180170
import sublime_plugin
class ShowZeroWidthSpace(sublime_plugin.EventListener):
def on_modified(self, view):
spaces = []
p = 0
while 1:
s = view.find(u'\u200b', p + 1)
if not s:
break
spaces.append(s)
p = s.a
if spaces:
view.add_regions("zero-width", spaces, "string")
else:
view.erase_regions("zero-width")
If you're on Windows, you can try using Notepad++ and changing the encoding to Encode in utf-8 BOM
If allo , you can also create a new ruby file and type in the code manually to ensure there's no zero-width or invisble characters that ruby doesn't like.
Rails 4 - syntax error, unexpected tIDENTIFIER, expecting end-of-input
How to remove non-printable/invisible characters in ruby?

Related

Rails bootstrap-datepicker-rails not working

I have a Rails 3.2.19 app using twitter-bootstrap-rails 2.2.6 and bootstrap-datepicker-rails 1.3.0.2.
I am trying to get a text_field in a form to display the datepicker but nothing happens when I click or focus on the field.
When I inspect the element I see the following:
<input data-behaviour="datepicker" id="project_due_date" name="project[due_date]" size="30" type="text">
When checking the console I do not receive any JS/Coffee errors.
Here is my code:
projects.js.coffee
$(document).on "focus", "[data-behaviour~='datepicker']", (e) ->
- $(this).datepicker
- format: "yyyy-mm-dd"
- weekStart: 1
- autoclose: true
projects/_form.html.erb
<%= form_for(#project, :html => { :class => "well"}) do |f| %>
<% if #project.errors.any? %>
<div id="error_explanation">
<h2><%= pluralize(#project.errors.count, "error") %> prohibited this contact from being created:</h2>
<ul>
<% #project.errors.full_messages.each do |msg| %>
<li><%= msg %></li>
<% end %>
</ul>
</div>
<% end %>
<%= f.label :project_name %>
<%= f.text_field :project_name, placeholder: "Build Spreadsheet", required: true %>
<%= f.label :project_manager %>
<%= f.collection_select(:user_id, User.all, :id, :full_name)%>
<%= f.label :due_date %>
<%= f.text_field :due_date, 'data-behaviour' => 'datepicker' %>
</br>
<%= f.button :submit, :class => 'btn btn-info' %>
<% end %>
I've tried also calling it based on id and have had no luck. I see the same behavior in Chrome, Firefox, or Safari.
Any help is greatly appreciated.
It looks like my CoffeeScript was wrong. I changed it to the following:
$ ->
$('[data-behaviour~=datepicker]').datepicker(format: "yyyy-mm-dd", autoclose: true)
Now I'm able to select the field and the datepicker comes up.

Bootstrap (2.3.1) + SimpleForm (2.1) inline label for radio button group

I'm trying to create inline radio buttons which also has its label inline (at the very left).
As I state in the title, using Bootstrap and SimpleForm.
My code looks like this:
<%= f.input :breakdown_or_size,
:collection => ["Breakdown", "Manual"],
:as => :radio_buttons,
:item_wrapper_class => 'inline',
:label => 'Select One: ' %>
This is what I get:
(source: webpagescreenshot.info)
I looked high and low for simple_form_for help. In the end I couldn't find anything, but at least with form_for you can do this:
<%= form_for(#book) do |f| %>
<% if #book.errors.any? %>
<div id="error_explanation">
<h2><%= pluralize(#book.errors.count, "error") %> prohibited this book from being saved:</h2>
<ul>
<% #book.errors.full_messages.each do |msg| %>
<li><%= msg %></li>
<% end %>
</ul>
</div>
<% end %>
<div class="actions", style='inline'>
<%= f.label 'Select One: ' %>
<br/>
<%= f.radio_button :collection, "Breakdown" %> Breakdown
<%= f.radio_button :collection, "Manual" %> Manual
<br/>
<%= f.submit %>
</div>
Which at least solves the inline rendering.

RoR: how can I get a micropost to be created by two seperate micropost forms according to an element?

My microposts have a column called type which is a string. This can either be purchase or sale. I want two seperate input forms, where if you input content into one then it automatically fills in purchase as the type (when creating the micropost) and if you input content into the other it automatically fills in sale. .
Here is my form as it is
<%= form_for(#micropost) do |f| %>
<%= render 'shared/error_messages', object: f.object %>
<div class="field no-indent">
<%= f.text_area :content, placeholder: "What's something else you want to buy?" %>
</div>
<%= f.submit "Post", class: "btn btn-large btn-primary" %>
<% end %>
I recently added type as a column in the micropost data table. That is why there is no input for it (yet). Read above for how I want the type to be automatically filled in
Im thinking one way to do it is to id each form with something. Then when the form is being filled in, I could somehow tell it to automatically fill in the hidden type field based on which form is filled in. IS this possible??
I think you are referring to the hidden_field here. Use hidden_field to pass information to the controller without presenting it to the user. Here is one form for the purchase:
<%= form_for(#micropost) do |f| %>
<%= render 'shared/error_messages', object: f.object %>
<div class="field no-indent">
<%= f.text_area :content, placeholder: "What's something else you want to buy?" %>
<%= f.hidden_field :type, 'purchase' %>
</div>
<%= f.submit "Post", class: "btn btn-large btn-primary" %>
<% end %>
and another for the sale:
<%= form_for(#micropost) do |f| %>
<%= render 'shared/error_messages', object: f.object %>
<div class="field no-indent">
<%= f.text_area :content, placeholder: "What's something else you want to sell?" %>
<%= f.hidden_field :type, 'sale' %>
</div>
<%= f.submit "Post", class: "btn btn-large btn-primary" %>
<% end %>

ActsAsTaggableOn Tagging with checkboxes

I'm playing with ActsAsTaggableOn in a small project to see what are the possibilities of this gem.
So I have Products and products can have tags.
In my Product _form
<%= form_for(#product) do |f| %>
<div class="field">
<%= f.label :title %><br />
<%= f.text_field :title %>
</div>
Tags<br />
<% tag_cloud(#tags, %w(css1 css2 css3 css4)) do |tag, css_class| %>
<%= check_box_tag 'product[tag_list][]',tag.name,#product.tag_list.include? (tag),:class => css_class %>
<%= tag.name %>
<% end %>
<div class="actions">
<%= f.submit %>
</div>
<% end %>
In ProductController I have defined the tag_cloud method
def tag_cloud
#tags = Product.tag_counts_on(:tags)
end
So in my editing product, I can tag the product with the checkboxes, but if a product already has tags those checkboxes aren't selected. I assume that here #product.tag_list.include? (tag) I am missing something or doing something wrong.
Any ideas? Thanks for your help =)
Cheers.
Today, after reading more carefully, I found the answer to this one.
This line
<%= check_box_tag 'product[tag_list][]',tag.name,#product.tag_list.include? (tag),:class => css_class %>
should be
<%= check_box_tag 'product[tag_list][]',tag.name,#product.tag_list.include? (tag.name),:class => css_class %>
So instead of the object I want to check if that name exists in the list.
Hope this helps someone.

Ruby on Rails, Paperclip: "identify" command working in cmd but not in app

I've installed ImageMagick on my Windows 7 64bit and I have the Paperclip Gem. My User model looks like this:
class User < ActiveRecord::Base
# Paperclip
has_attached_file :photo,
:styles => {
:thumb=> "100x100#",
:small => "150x150>" }
end
In paperclip.rb and development.rb I have:
Paperclip.options[:command_path] = 'C:/Program Files/ImageMagick-6.6.7-Q16'
My _form looks like this:
<%= form_for(#user, :html => { :multipart => true } ) do |f| %>
<% if #user.errors.any? %>
<div id="error_explanation">
<h2><%= pluralize(#user.errors.count, "error") %> prohibited this user from being saved:</h2>
<ul>
<% #user.errors.full_messages.each do |msg| %>
<li><%= msg %></li>
<% end %>
</ul>
</div>
<% end %>
<div class="field">
<%= f.label :username %><br />
<%= f.text_field :username %>
</div>
<div class="field">
<%= f.label :name %><br />
<%= f.text_field :name %>
</div>
<div class="field">
<%= f.label :email %><br />
<%= f.text_field :email %>
</div>
<div class="field">
<%= f.label :crypted_password %><br />
<%= f.text_field :crypted_password %>
</div>
<div class="field">
<%= f.label :password_salt %><br />
<%= f.text_field :password_salt %>
</div>
<%= f.file_field :photo%>
<div class="actions">
<%= f.submit %>
</div>
<% end %>
enter code here
I'm getting the following error when uploading an image:
[paperclip] An error was received while processing: #<Paperclip::NotIdentifiedByImageMagickError: C:/Users/John/AppData/Local/Temp/stream20110212-6576-1us1cdl.png is not recognized by the 'identify' command.>
I'm able to use identify in my cmd on that image and it returns metadata about the image without problems.
Please help if you can. I've been stuck on this one issue for over a day now.
This is due to a bug in the Paperclip gem in lib/paperclip/command_line.rb file.
def full_path(binary)
[self.class.path, binary].compact.join("/")
end
The full_path function generates command file name with a backslash.
"C:\Program Files\ImageMagick-6.7.0-Q16"/identify
This command fails on Windows as the cmd shell throws an error when the command file is a long file name with a back slash.
There are two ways to fix the issue.
Use the short file name as the command path.
Paperclip.options[:command_path] = 'C:/PROGRA~1/IMAGEM~1.0-Q'
Note: You can get the short file name as follows:
dir /x "C:\Program Files*"
dir /x "C:\Program Files\ImageMagick-6.7.0-Q16*"
Monkey patch the Paperclip gem in config\initializers\paperclip.rb.
I tested this on 2.3.11.
class Paperclip::CommandLine
def full_path(binary)
[self.class.path, binary].compact.join(File::ALT_SEPARATOR||File::SEPARATOR)
end
end
Now, the identify command is generated with the correct path seperator.
"C:\Program Files\ImageMagick-6.7.0-Q16"\identify
I prefer the second approach as command_path is easier to configure.
Updated the following in development.rb and it started working
Paperclip.options[:command_path] = 'C:/Progra~1/ImageM~1.8-q'
This was on a Windows 2008 32 Bit Server
Open a command prompt and type echo %path% your imagemagick path should appear there.
Also try changing the :command_path to C:/Progra~1/ImageM~1