开发者

Jquery & Rails 3: Link_to Edit Action Remotely

开发者 https://www.devze.com 2023-03-21 16:23 出处:网络
I have a discussions controller for which I would like to have an edit action that is linked to remotely, allowing for an AJAX call to render a partial on a page (rather than rerouting to a completely

I have a discussions controller for which I would like to have an edit action that is linked to remotely, allowing for an AJAX call to render a partial on a page (rather than rerouting to a completely new "edit" page.

My problem is that I have set up the actions in the controller, and all the files (i think are) necessary. But the link on the discussion show page redirects the browser to a new page, rather than linking through to the action on the controller and the corresponding edit.js.erb file. I am wondering where in my code I went wrong..

Here is the code for the discussions controller:

 class DiscussionsController < ApplicationController
   include DiscussionsHelper

   def create
     ...
   end

   def show 
      ...
   end

   def edit
     respond_to do | format |  
        format.js
     end
   end 
 end

Which i would hope would call the js.erb file edit.js.erb (which I have filled just with an alert, for now -- once this links through properly I intend t开发者_开发问答o fill it with jquery code to render the edit partial I would like.:

 alert("hello");

The link_to that I have put on the show page is:

 <div class="discussion_overview">
   <h1><%= @discussion.title %></h1>
   <div class="edit_disc">
<%= link_to "Edit", :url => {:action => "edit", :id => @discussion}, :remote => true %>
   </div><br>

As I said, I thought that this would call the edit.js.erb file, which I could then fill with the appropriate code. However, the link_to links instead to :

 http://localhost:3000/discussions/54?remote=true&url[action]=edit&url[id]=54

Any ideas as to what I did wrong?

UPDATE dropping the url => {} to become:

 <%= link_to "Edit", :action => "edit", :id => @discussion, :remote => true %>

Does not solve the solution, instead just changes the redirect to :

http://localhost:3000/discussions/54/edit?remote=true

rather than doing an AJAX call to the edit action..


It's just a syntax issue in the link_to line.

<%= link_to "Edit", {:action => "edit", :id => @discussion}, :remote => true %>

The second param in the link_to() method is the url. By prepending with :url => {} your url hash is within another hash. Just drop the :url =>. :)

0

精彩评论

暂无评论...
验证码 换一张
取 消