which advised me to use a regular link with the class "remote", and if
javascript is disable it will function like a regular link, and if
it's enabled it will function as an ajax call.
> which advised me to use a regular link with the class "remote", and if
> javascript is disable it will function like a regular link, and if
> it's enabled it will function as an ajax call.
"Programming today is a race between software engineers striving to build
bigger and better idiot-proof programs, and the Universe trying to produce
bigger and better idiots. So far, the Universe is winning."
If you're using the lowpro linked from the blog post, you should be set, it
defines Remote. If you downloaded from somewhere else, check inside your
lowpro.js and make sure that it has something like "Remote =
Behavior.create...". If it doesn't, there should have been a remote.js with
your download that you'll need to include as well.
> If you're using the lowpro linked from the blog post, you should be set, it
> defines Remote. If you downloaded from somewhere else, check inside your
> lowpro.js and make sure that it has something like "Remote =
> Behavior.create...". If it doesn't, there should have been a remote.js with
> your download that you'll need to include as well.
Aha.. Yes, that indeed was the problem. Thank you!
I have another problem with this-- It seems that when I try to do a
link to a destroy action, by having :method => :delete in the link_to,
it initiates both an ajax call AND a http request simultaneously when
clicking the link... Very strange... I even tried making a separate
route map.resources :comments, :member => {:remove => :delete} .. but
that did not work either.
The only way I was able to figure out how to get around this, was to
rename my destroy action to 'show', and then get rid of the :method
=> :delete in the link_to.
If anyone has any ideas as to why this is, and how perhaps a better
way of how to correct it would be, I'd greatly appreciate it...
my code was:
<%= (link_to "remove that does not work", comment, :method
=> :delete, :class => 'remote' ) %>
and then my controller action was just:
def show # works
@comment = Comment.find(params[:id])
@comment.destroy
respond_to do |format|
format.html { redirect_to :back }
format.js { render :template => 'comments/destroy.js.rjs' }
end
end
def destroy # does not work
@comment = Comment.find(params[:id])
@comment.destroy
respond_to do |format|
format.html { redirect_to :back }
format.js { render :template => 'comments/destroy.js.rjs' }
end
end
> I have another problem with this-- It seems that when I try to do a > link to a destroy action, by having :method => :delete in the link_to, > it initiates both an ajax call AND a http request simultaneously when > clicking the link... Very strange... I even tried making a separate > route map.resources :comments, :member => {:remove => :delete} .. but > that did not work either.
The problem is with the javascript, not within the Rails code. When you add a :method => :delete option to a link_to call, Rails makes the link to use javascript to send a form with http (pseudo-)method DELETE. This is because normal links always use the GET method, which can never reach the destroy action in Rails. Check out the html source of the page in your browser to see what a link_to call produces when you use the DELETE method.
Now your problem is that two javascript event handlers are competing for your link and it seems in your case they are both firing. However, the Low Pro version has no way to know about the DELETE method you need for your Remote.Link call to go to the destroy action. Thus it will just load the /comments/x page normally, resulting in a request to the show action.
The first thing you should do is to remove the :method => :delete option from the link_to call. It will just add inline javascript to your html which is basically impossible to make accessible, because *normal links can not reach you destroy actions* (and neither should they).
It's worth repeating here: normal links can only fire GET requests and they should never cause any changes in the application state.
Instead, replace the delete link with a simple form (with :method => :delete) and attach Remote.Form to it the same way you're doing with links. You can then either use normal submit form button (<input type="submit">) in there, or an image input element (<input type="image"...>) or if you really want to go hard-core, a button element [1]. Then you can just wrap this all up in a reusable helper method.
I have another question... I currently have a button that toggles the
visibility of my shopping cart:
button_to_function "view cart", "toggleCart();"
....
I am just curious, if I want to modify this so that when users without
javascript click on this, it would refresh the page with cart=true/
false in the params-- but users with javascript it will simply call
the js function... Would the most logical thing to do be to just do
something like
> I have another question... I currently have a button that toggles the
> visibility of my shopping cart:
> button_to_function "view cart", "toggleCart();"
> ....
> I am just curious, if I want to modify this so that when users without
> javascript click on this, it would refresh the page with cart=true/
> false in the params-- but users with javascript it will simply call
> the js function... Would the most logical thing to do be to just do
> something like
> On 22 May, 02:08, Jarkko Laine <jar...@jlaine.net> wrote:
> > Hi Patrick,
> > On 22.5.2009, at 10.46, patrick wrote:
> > > I have another problem with this-- It seems that when I try to do a
> > > link to a destroy action, by having :method => :delete in the link_to,
> > > it initiates both an ajax call AND a http request simultaneously when
> > > clicking the link... Very strange... I even tried making a separate
> > > route map.resources :comments, :member => {:remove => :delete} .. but
> > > that did not work either.
> > The problem is with the javascript, not within the Rails code. When
> > you add a :method => :delete option to a link_to call, Rails makes the
> > link to use javascript to send a form with http (pseudo-)method
> > DELETE. This is because normal links always use the GET method, which
> > can never reach the destroy action in Rails. Check out the html source
> > of the page in your browser to see what a link_to call produces when
> > you use the DELETE method.
> > Now your problem is that two javascript event handlers are competing
> > for your link and it seems in your case they are both firing. However,
> > the Low Pro version has no way to know about the DELETE method you
> > need for your Remote.Link call to go to the destroy action. Thus it
> > will just load the /comments/x page normally, resulting in a request
> > to the show action.
> > The first thing you should do is to remove the :method => :delete
> > option from the link_to call. It will just add inline javascript to
> > your html which is basically impossible to make accessible, because
> > *normal links can not reach you destroy actions* (and neither should
> > they).
> > It's worth repeating here: normal links can only fire GET requests and
> > they should never cause any changes in the application state.
> > Instead, replace the delete link with a simple form (with :method
> > => :delete) and attach Remote.Form to it the same way you're doing
> > with links. You can then either use normal submit form button (<input
> > type="submit">) in there, or an image input element (<input
> > type="image"...>) or if you really want to go hard-core, a button
> > element [1]. Then you can just wrap this all up in a reusable helper
> > method.