Rails + best_in_place + Twitter Bootstrap - ruby-on-rails-3

I have really annoying issue with best in place and Twitter bootstrap. I have spent with that a lot of time. I have gone through several pages - best_in_place github page and Issues. So I thought, that I fixed it but not and I don't know where the mistake is.
When I'm using best_in_place for the first edit, it works, save the value and the input is changed back to the "normal" view with new value. But when I want to do the same action( edit it) second time, its not working. I have to reload the page and then I can edit it again only once. My code is bellow:
AccountsController.rb
def update
#account = current_user.account
respond_to do |format|
if #account.update_attributes(params[:account])
format.json { respond_with_bip(#account) }
else
format.json { respond_with_bip(#account) }
end
end
end
View
<%= best_in_place current_user.account, :introduction, :type=> :input,
:nil => "Click me to add your introduction!",
:html_attrs => {:'data-provide' => "typeahead" } %>
account.js.coffee
jQuery ->
$('.best_in_place').best_in_place()
I have already seen this pages and haven't found the answer:
SO Thread, ExampleApp, Best_in_place Issue
Please, could someone help me? I know that is maybe caused by Twitter Bootstrap, but I don't know how to fix it:(

OK, I have found the solution for that.
The "issue" is in this line:
format.json { respond_with_bip(#account) }
I replaced it with:
format.json { render json: #account}
And its working now. But it was just luck. Do anyone know what is the main difference here? Why one is working and the second no? What exatly the respond_with_bip does?

Related

Can't submit form after render

I'm having an issue with UJS, jQuery and partials. I have a table with a bunch of rows. When I click edit on one of the rows, the row transforms into text fields where I can edit the row. Then I have a submit; however, clicking it does nothing. It just doesn't look like its triggering. Clicking edit sends a call to the controller, which responds with edit.js.erb which has jQuery replace the contents of the row with the edit partial.
Is there anything obvious I'm overlooking? I'd post code, but am on my phone and can't get online on the computer. I'll edit later if I can't figure it out. I was hoping for some feedback in the meantime.
Thanks
Ok, here's some code:
Item controller:
def edit
respond_to do |format|
format.html { respond_with #item }
format.js { }
end
end
def update
#item.update_attributes(params[:item])
respond_to do |format|
format.html { respond_with #item, :location => items_path }
format.js { }
end
end
edit.html.erb:
<%= form_for(#item, item_path) do |form| %>
<%= render form %>
<% end %>
edit.js.erb:
$(' .item ').html(" 'item/edit', :locals => {:item => #item})) %>");
update.js.erb:
$(' .item ').html(" 'item/item', :item => #item) %>");
It actually ended up being an issue with html closing my form tag prematurely. I didn't know you couldn't put a form in a table cell. I had stared at the source for so long and couldn't see what was wrong.
If this is happening to you, make sure you check where your closing form tag is.

After deleting a record in Rails 3, the refreshed view isn't updated

I'm dealing with a basic one to many relation where I'm deleting a record on the many side. The models are "places" and "search_terms" where places has_many search_terms. When I create a new record, the view is updated and the new search_term appended to the list. However, when I delete a search_term record the view is not refreshed even though it deletes the record and runs the "show" method for Place.
I'm quite new to rails 3 so can't really figure out whats going on here...
Cheers,
Gearoid.
Edit: the search_terms controller destroy method:
def destroy
#search_term = SearchTerm.find(params[:id])
#search_term.destroy
#place = Place.find(params[:place_id])
redirect_to place_path(#place)
end
The places controller show method:
def show
#place = Place.find(params[:id])
respond_to do |format|
format.html # show.html.erb
format.xml { render :xml => #place }
end
end
I might be misunderstanding you, could you post your controller's code?
Is this happening over ajax? If not, can you redirect to the Show instead of just re-rendering it? That's probably a preferred experience for the user anyway.
UPDATE
Ok, if this is going over ajax, then the problem is simple. Your destroy action is only expecting a normal browser event and doing a redirect_to call. The ajax call doesn't know how to handle it and just sits there. You can probably see the redirect code in something like Firebug.
I'm not super familiar with jquery-rails (I prefer to write all my js myself because I'm anal). You can have the destroy action return a js format like so:
def destroy
#search_term = SearchTerm.find(params[:id])
#search_term.destroy
#place = Place.find(params[:place_id])
respond_to do |format|
format.html { redirect_to place_path(#place) }
format.js { render :nothing => true }
end
end
That will give the ajax caller the ok signal that it has done its thing. Your javascript will still have to intelligently handle this response though, like remove the element from the DOM.

Rails 3 showing Twitter Button in success notification

In my Rails 3 web app, I show a success message when someone updates their profile. With this code: redirect_to #user, :flash => { :success => "Profile updated." }
What I want to be able to do is show a Twitter button, which is an a href and javascript see here
How would I add it to the success flash? I have tried just copying and pasting the code but I then get errors basically saying syntax is wrong...
Thanks in advance...
This answer worked for me and I now have a twitter button showing with the success flash message! :)
You might need to wrap your twitter code in a raw method to force it to not escape the html and an escape_javascript method to force it to escape the javascript
So try something like this
redirect_to #user, :flash => { :success => raw(escape_javascript(twitter_code)) }
You would do something along the lines of this:
redirect_to #user, :success => "Profile updated.", :flash => { :url => "Tweet<script type="text/javascript" src="http://platform.twitter.com/widgets.js"></script>" }
And in the layout:
- if flash[:success]
## Display code, etc...
- if flash[:url]
= flash[:url]
Or something along those lines

How to render validation errors on the page that submitted the form?

I've got a comments form in the article/show page. In this page, it displays the article and has a comments form.
When I submit a comment that has validation errors, I need it to go back to the article/show page, and display the errors there.
Should I change render :action => 'new' to something else?
In the Comment controller, I tried:
def create
...
if #comment.save?
redirect_to article_path(#comment.article), :notice => "Posted comment!"
else
# render :action => 'new'
render 'articles/show"
end
end
But this will complain, since the app won't know which article to show based on the ID.
EDIT: I found this solution. The approach would be to use a session to pass the errors instead. Is this the right way to go with this?
Try this
def create
...
if #comment.save?
redirect_to article_path(#comment.article), :notice => "Posted comment!"
else
# render :action => 'new'
#article = #comment.article
render 'articles/show"
end
end`
So fix your routing so the app does know what article to show based on the ID.

How can I toggle a boolean field in Rails 3 using AJAX?

I have a column in a table called Complete that is a boolean.
How can I (using the Rails 3 / JQuery way) provide a link that will toggle that via AJAX?
Historically, I've simply created my own jquery file, grabbed the click event, and done it by hand. But it really seems like I'm missing something by not doing it the "rails" way. If that makes sense.
I guess I still don't understand how the responds_to JS works, JS with ERB, etc.
Is there a good, up-to-date tutorial on how to do this?
Thanks.
see this post,
Rails 3, Custom Actions, and HTML request methods
and use UJS on top of it.
so you have a fallback, if javascript is disabled
a possible method in the controller looks like that:
# GET /surveys/1/close
def close
#survey.close!
flash[:success] = "Survey successfully closed!"
respond_to do |format|
format.html { redirect_to(surveys_url) }
format.xml { head :ok }
format.js { render :partial => 'list_item', :locals => { :survey => #survey } }
end
end
you could also use a state machine to change the state of your object.