Forum: Ruby on Rails Need help regarding RESTful and Ajax Form

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
07cef9a7f74a2df39fbc3af8d0d0d9a4?d=identicon&s=25 DanielFischer.com (Guest)
on 2007-01-27 02:35
(Received via mailing list)
I'm trying to create an ajax form but I'm a little confused on the
syntax I should use for REST.


I recently created a comments model which belongs to poems, and poem
has many comments.

I set up the routes like so:

[code=ruby]
map.resources :poems do |poems|
   poems.resources :comments
end
[/code]

My Comments Controller
[code=ruby]
class CommentsController < ApplicationController
  def new
    @comment = Comment.new
  end

  def create
    @comment = Comment.new(params[:comment])

     respond_to do |format|
        if @comment.save
          flash[:notice] = 'Poem was successfully created.'
          format.html { redirect_to comment_url(@poem, @comment) }
          format.xml  { head :created, :location => comment_url(@poem,
@comment) }
        else
          format.html { render :action => "new" }
          format.xml  { render :xml => @comment.errors.to_xml }
        end
      end
  end
end
[/code]
But I'm rather confused on what I should be doing to create an ajax
form for comments, while viewing a poem.

poem show.rhtml
[code=ruby]
<p><% form_remote_tag(:update => "comments",
                   :url => { :action => :new },
                   :position => "top" ) %>
    New Comment:
    <%= text_field_tag :new %>
    <%= submit_tag 'Save' %>
  <% end_form_tag -%></p>
[/code]
I did something like that, but I don't think that's right.

If anyone could point me in the right direction, I'd appreciate it.
Thanks!
15c80c9bf8be5ba6e5eeac9cb0304464?d=identicon&s=25 Ed Hickey (Guest)
on 2007-01-27 18:48
(Received via mailing list)
> :position => "top" ) %>
>                 New Comment:
>                 <%= text_field_tag :new %>
>     <%= submit_tag 'Save' %>
>   <% end_form_tag -%></p>
> [/code]
> I did something like that, but I don't think that's right.
>
> If anyone could point me in the right direction, I'd appreciate it.
> Thanks!



this might work for you (if you're using 1.2 or edge):

<% remote_form_for :comment :url => new_comment_path(@poem) do |f| -%>
<%= f.text_field :new -%>
<%= submit_tag 'Save' -%>
<% end -%>
This topic is locked and can not be replied to.