Display checkbox best_in_place ruby gem - ruby-on-rails

I am using the best_in_place gem in a project, and i am trying to display a checkbox on it's own place, but i cannot do it. I am searching about it, but i cannot find out the answer anywhere. I just can find people talking about display either "no" or "yes"... Or anything else. But i just want to see the checkbox checked or not.

I am using font-awesome but you can use it also with bootstrap or other images. My solution for displaying checkboxes were simply using the raw method to render a checkbox image (in my case one of the font-awesome icons)
= best_in_place #project,
:active,
:type => :checkbox,
:collection => {false: raw("<i class='icon-check-empty'></i>"), true: raw("<i class='icon-check'></i>")}
And now it looks like the following:

I've managed to make a hack-around using unicode characters ☐ and ☑
<span style='font-size: 1.75em;'>
<%= best_in_place a,
:bool,
:type => :checkbox,
:collection => ["☐", "☑"]%>
</span>
The <span> wrapper helps with choosing the size in this case. This is a short term solution and a checkbox with an AJAX call would definitely be better I guess...

I've forked the original best_in_place gem and modified it with support for displaying an actual, standard HTML checkbox. My fork can be found here: https://github.com/jdashton/best_in_place
In case you're unfamiliar with using gems directly from github, the following line in your Gemfile should do the trick:
gem 'best_in_place', github: 'jdashton/best_in_place'

It's not supported in the current version. However, it's pretty easy to do. Create a checkbox where you need it, and fire an ajax call to the server with some javascript.
Check out this stackoverflow answer for an example.

Related

How to interpolate ruby inside of an interpolated bit of ruby in ERB

I'm trying to create a situation where one user makes message templates and another one can plug in values. I'm using the best_in_place gem, which will allow a user to edit the message on the show page.
The problem is this. When I call the message, with the required erb to make the gem work, it treats all of this as a regular string, not as ruby.
This is unclear, I'm sorry.
Here's the code.
#announcement.content = "The <%= best_in_place #announcement, :train %> is arriving in five minutes."
/show.html.erb
<%= #announcement.content %>
I want it to put "The click to set train is arriving in five minutes." and if the user clicks where it says "click to set train," a text field will open for them to edit (this is something the best-in-place gem does).
Instead, it puts "The <%= best_in_place #announcement, :train %> is arriving in five minutes."
I understand why it is doing this, but I don't know how to make it instead interpret the ruby I'm trying to pass in.
Ideas?
Use regular old string interpolation:
#announcement.content = "The #{best_in_place #announcement, :train} is arriving in five minutes."
You can use ERB to render any ERB template string. In this case something like:
<%= ERB.new(#announcement.content).result %>
Although you likely won't have access to all your Rails helpers, etc.
The Rails way to do this:
#announcement.content_type = :arriving
Later:
<%= render(partial: #announcement.content_type)
In _arriving.erb:
The <%= best_in_place #announcement, :train %> is arriving in five minutes.
TL;DR: ERB is not Ruby, and Rails uses both at different times.
You want simple Ruby string interpolation here:
#announcement.content = "The #{best_in_place #announcement, :train} is arriving in five minutes."
This is unclear, I'm sorry.
Not to worry, the Rails framework throws so many different new concepts at you it can be frustrating for newcomers.
Start from this: the Ruby framework builds the answer to the user's browser from a collection of resources Each file is evaluated by an interpreter for its own language. The trick is: look at the extension.
Files ending in .coffee will be compiled into javascript, files ending in .scss will become CSS, and in the same way files ending in .erb will yield HTML.
ERB is a language composed of mostly HTML already, plus a tag that allows you to interpolate Ruby. ERB stands for Embedded Ruby.
What about files ending in .rb, like the file in which you (surely) are evaluating #announcement.content = "The <%= best_in_place[...]" (a controller, I guess)?
Well, that's just pure Ruby :) that's why the ERB interpolation syntax <%= ... > is not recognized.
What you want to do in the controller, is (as you're trying to do) preparing the data for the view. The ruby in the <%= ... > tag in ERB will have access to the controller's instance variables, i.e. the variables with an # in front defined in the controller. But to define those, inside the controller, you should rely on Ruby alone.
Take-home message:
Be aware of which language you are writing in at each moment. For example:
# show.html.erb
<p>Here is ERB, which will be interpreted straight into HTML</p>
<% "Inside the '<% ...' tag is Ruby, but results won't show up in the HTML because there's no '<%='."%>
<% which_language = "Ruby" # Even variable assignments, and comments, do work %>
<%= "Inside the '<%=' tag, you're writing and interpolating #{which_language} :)" %>
I think the fact that I wasn't clear made it hard to answer this question.
What I'm doing is transforming user-inputted text (using a method in the model, called by the controller) to replace certain keywords with erb tags that call the best_in_place plugin. In my view, when presenting this content to another user, I wanted to call this content, which is saved as an attribute in the database, in such a way that it would render correctly for the other user to have the best_in_place functionality active.
Here's what I ended up doing. It is working, but if you have better ideas, please let me know.
In the announcements#create view, the user creates an announcement with certain pre-defined blocks of bracketed text as well as free-input text. For example, they might write "[train] is leaving from [platform] in [time] minutes."
When they hit save, the controller's create action calls the construct_message method from the model. It looks like this:
def construct_message(msg)
msg.gsub! '[train]', '<%= best_in_place #announcement, :train_id, :as => :select, collection: Train::list_trains, place_holder: "Click here to set train." %>' #note: list_trains and list_platforms are methods on the model, not really important...
msg.gsub! '[platform]', '<%= best_in_place #announcement, :platform_id, :as => select, collection: Platform::list_platforms, placeholder: "Click here to set platform." %>'
msg.gsub! '[time]', '<%= best_in_place #announcement, :number_of_minutes, placeholder: "Click here to set." %>'
end
Then, when I want to show that attribute in my view, I'm using render :inline, like this.
on announcements/:id
<p id="notice"><%= notice %></p>
<p>
<strong>Content:</strong>
<% announcement = #announcement %>
<%= render :inline => announcement.content, locals: { :announcement => announcement } %>
</p>
This allows the erb call that I wrote into the attribute to be functional.
Also note that I'm choosing to use a local rather than instance variable here; this is because in announcements#index, I also render this text and the table there uses local variables.

display text best_in_place gem

I'm using the best_in_place gem for in place editing. I have this around a link that I want editable, but what's displayed to the user should be "Edit Link".I want to be able to edit the link when this field is clicked on, but I want what's displayed to the user (the anchor text) to be "edit link". I tried using "display_with" and "value" but neither has worked.
It's currently displaying the value of the database field which is the URL: https://plus.google.com/+SteveQuatrani/posts
<%= best_in_place c, :google_maps_url, value: "Edit Link" %>
Is there a way to do this? Thank you in advance for your help!
display_with requires a helper or proc, so try giving it one:
<%= best_in_place c, :google_maps_url, display_with: Proc.new {"Edit Link"} %>

Rails: Twitter Bootstrap Buttons when visited get black text color

I have a rails 3.2 app using Twitter Bootstrap via the gem 'twitter-bootstrap-rails'. Additionally the forms are created with the SimpleForm gem For a number of the pages I've used the twitter buttons on the form via
<%= link_to "Back", :back, :class => 'btn btn-warning'%>
<%= form.button :submit, :class => 'btn btn-primary' %>
The buttons are rendered ok. The issue is that after you select one of the buttons, which visits the link, on returning to the page the text is stuck on the greyed out version as shown below for the 'back' button:
This causes a problem, especially on the buttons styled with 'btn-primary' as the text is hard to read. An example of this is below:
Wondering what setting needs to change and where. I expected it should be in the bootstrap_and_overrides.css.less file but not sure what setting to try. Tests on #linkColorHover didn't work.
Any thoughts ?
The best solution I found for this problem is to remove scaffolds.css.scss in the app/assets/stylesheets directory as suggested by #tonymarschall above in the comments.
You could always style the a.btn items, to remove the decorations on the pseudo classes such as :visited
As an alternative you can use button_to instead of link_to in your templates.
Because this creates a new form, if you want to simulate the link behaviour, you need to use the :method => :get parameter. You can find more info here.
Kind of a combination of a few of the answers, but simply replace:
color: #666;
With:
color: white;
In scaffolds.css.scss
You should use Firebug or Chrome's Developer Tools to find out which CSS property you'd have to override in bootstrap_and_overrides.css.less.
It's kinda hard to tell by looking at images.

how to find values of best_in_place checkbox dynamically without refresing the page in ruby injection

This is the code i'm using to get value of the checkbox & set the flag accordingly,
:checkbox, :collection => ["No, thanks", "Yes, of course!"] %>
<% if (post.content == "true") %>
<%= #flag=1 %>
<%else %>
<%= #flag=0 %>
But i'm not getting the new value of the checkbox (value changes by clicking on it) without refreshing a page
The changing check box is client-side... the server (rails and your erb template) knows nothing of it changing. You will probably want to use a client-side technology like javascript to watch for the change and manage the behavior you are looking for. I would likely use jQuery to do this -- I'd help with the jQuery, but I'm a little unsure of what you are up to. Hope this helps.

Tagging with autocomplete in Rails

My (long, I apologize) question is a follow-on to: How to add tagging with autocomplete to an existing model in Rails?
I am using acts-as-taggable-on and rails3-jquery-autocomplete, and trying to set up a system (much like Stack Overflow) where users begin to enter in a tag and suggestions appear in a drop-down box.
Goal
I am in the answers#new form and I want to see a list of tags that relate to questions. i.e. Imagine being on SO looking for new Rails questions to answer, and searching for ra. Ruby-on-Rails pops up, you click it, and you see a list of questions under RoR, any one of which you can answer.
These are the steps I've taken.
Installed both gems. Both seem to work on their own.
Added <%= javascript_include_tag "ui/jquery.ui.position", "ui/jquery.ui.autocomplete", "autocomplete-rails.js", "rails.js", "application.js" %>. (I already have Jquery, UI Core and UI Effects.)
Answers controller: I added at top autocomplete :question, :tags, :full => true. I also tried autocomplete :tag, :name, :full => true.
Question.rb: acts_as_taggable_on :tags.
View:
<%= form_tag new_answer_url, :method => "get" do %>
<%= autocomplete_field_tag "tag_list", 'tags', autocomplete_question_tags_answers_path %>
<% end %>
A simple autocomplete (no tagging) works (but it only works once per page load). With tagging, no success.
Problems
With lots of experimentation (and many hours) I am getting these problems:
I get NameError (unitialized constant Tag) in server response to initial entry.
With a non-taggable implementation (searching for the simple question text itself) I get a JQuery Autocomplete-style drop-down but my cursors cannot access the options with up/down. I have to click them with the mouse. Also, the dropdown doesn't disappear unless I reload the page!
After the server responds with results once (only non-taggable is working as I mentioned), it doesn't respond again to key presses or changes in the text entry.
I would greatly appreciate any help you are able to give. I have gone through a few tutorials step-by-step but no luck today.
I know it only answers one of your questions, but I was able to solve the "unitialized constant Tag" issue by explicitly specifying the class name in my controller:
autocomplete :tag, :name, :class_name => 'ActsAsTaggableOn::Tag'
It seems some of the changes to the acts_as_taggable_on library broke the underlying assumption that the Tag class exists.
Beyond that, I noticed some strange behavior myself when I didn't have my jquery-ui css correctly included on the page—have you verified that everything's getting linked in properly?
One thing I notice you're missing is anything your routes. I had to put something like this:
resources :resources do
get :autocomplete_resource_tag, :on => :collection
end
In my _form.html.erb
<%= f.autocomplete_field :tag_list, autocomplete_resource_tag_resources_path %>
Now my problem is that the autocomplete still isn't loving me
SQLite3::SQLException: no such column: resources.tag: SELECT resources.id, resources.tag FROM "resources" WHERE (LOWER(resources.tag) LIKE 'woo%') ORDER BY resources.tag ASC LIMIT 10
Completed 500 Internal Server Error in 1ms
ActiveRecord::StatementInvalid (SQLite3::SQLException: no such column: resources.tag: SELECT resources.id, resources.tag FROM "resources" WHERE (LOWER(resources.tag) LIKE 'woo%') ORDER BY resources.tag ASC LIMIT 10):

Resources