After deleting a record in Rails 3, the refreshed view isn't updated - ruby-on-rails-3

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.

Related

Rails goes to show action when I want custom action of open or expired

I am basically trying to do a custom index
In my routes.rb I have:
resources :tokens do
resources :visits
collection do
get :open
get :expired
end
end
My routes show as :
open_tokens GET /tokens/open(.:format) tokens#open
expired_tokens GET /tokens/expired(.:format) tokens#expired
I have open and expired defined in my tokens controller
def open
#q = Token.open_token.includes(:households, :visits).search(params[:q])
#tokens = #q.result
respond_to do |format|
format.html {render template: 'tokens/open'}
end
end
def expired
#q = Token.expired_token.includes(:households, :visits).search(params[:q])
#tokens = #q.result
respond_to do |format|
format.html {render template: 'tokens/expired'}
end
end
But it goes to my show controller with the following error:
Couldn't find Token with id=open
and it crashes at my Show action:
Better Errors shows the request as:
{"action"=>"show", "controller"=>"tokens", "id"=>"open"}
It seems to me that the action should be open with no parameters
What am I doing wrong?
I think I may have answered my question since it appears that Rails can only have one index per controller so I couldn't define another view that acts as an index.
So now what I want to do is pass a parameter so I can apply either an open scope or an expired scope

not able to load a view when I disable javascript rails 3

Here is my controller code:-
def image_test
respond_to do |format|
format.js {render :layout => false}
format.html {redirect_to image_test_path}
end
end
I have got a partial by the name of _image_test.html.erb and and a simple view image_test.html.erb
In my routes I have done this:-
match "/image_test", :to => "/index#image_test"
It works fine when the javascript is enabled in the borwser however when I disable the javascript I want it to redirect me to my image_test.html.erb file. Instead I get a no route match error.
Please help me with this.
Thanks,
I created a workaround for this solution which is giving the desired result of redirecting to another page if javascript is disabled in a browser but I do not know if this is the rails way.
I created empty action corresponding view for those actions and redirected to those views in case javascript is disabled.
Here is an example of what I did :-
def javascript_enabled_view
respond_to do |format|
format.js {render :layout => false}
format.html {redirect_to :action => "javscript_disabled_view"}
end
end
I have got a corresponding js.erb file and the partial for the above action which will work if javascript is enabled in the browser.
def javascript_disabled_view
end
I have got the corresponding html.erb file which will work in case javascript is disabled in the browser.
Thanks,

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.

problem with RESTful forms in Rails 3

okay, so basically, I have a normal form for my model:
= form_for #operator do |f|
blah blah blah
In my operators controller, i have this:
def new
#operator = Operator.new
#operator.build_user
respond_to do |format|
format.html {}
end
end
def create
#user = User.create(params[:operator].delete(:user))
#user.update_attributes(:login => #user.email)
#operator = Operator.new(params[:operator].merge(:user => #user))
respond_to do |format|
if #operator.save
format.html {redirect_to new_operator_aircraft_path(#operator)}
else
format.html { render :action => "new", :error => #operator.errors }
end
end
end
very basic stuff. I have some validates_presence_of stuff in my model so naturally when I submit my form, it should show me that I have errors(and keep the fields I have filled up)
Right so far? yeah. The problem is, it seems I am posting to /operators and that's what renders. I seem to have forgotten about what happens in Rails2.3+ but shouldn't I be redirected to /operators/new again? or was that the intended behavior all along?
Here's what I think you are asking:
After I submit a form with errors, why does the URL
read "/operators" rather than
"/operators/new".
Thanks to resourceful routing, when submitting a form via POST to "/operators" the create action is called on the OperatorsController. If you encounter errors when saving your operator, you've instructed the controller to render the new action within the same request.
render :action => "new", :error => #operator.errors
This means a redirect is not occurring and therefore the URL remains "/operators".
If a redirect were to occur, you would lose all the state information of the #operator object in the current request, including the errors you encountered as well as the form values you just submitted.
In other words, working as intended.

Rails3 and Respond_with problem

I have an application, on which I have two user interfaces.
The first one is for normal users and the second one is for iphone users.
Everything was working fine until i refactored my code within controller to use the respond_with declarative instead of respond_to.
The application is still working for the html interface(:format => :html) but not on the iphone interface(:format => :iphone).
On the iphone, when I do the following action (:index, :new, :edit, :show) it works.
But when i do (:create, :update, :destroy), I get errors saying the template is not found(create.iphone.haml for example).
On my controller I have
respond_to :html, :iphone
And then for example, the edit and the update action
def edit
#refund = Refund.find(params[:id])
respond_with(#refund)
end
def update
#refund = Refund.find(params[:id])
if #refund.update_attributes(params[:refund])
flash[:notice] = 'Refund was successfully updated.'
end
respond_with(#refund, :location => project_refunds_path(#project))
end
In fact, I would like the :iphone format is handle as :html is ... and not by calling the to_format method as it is specified into the doc.
Solved it by myself.
Just need to add this to an initializer file :
ActionController::Responder.class_eval do
alias :to_iphone :to_html
end
What if you do:
respond_with(#refund, :location => project_refunds_path(#project)) do |format|
format.iphone { whatever you had here before refactoring }
end