在Rails中为多态对象创建AJAX表单

时间:2010-12-25 21:50:30

标签: ruby-on-rails polymorphic-associations

我正在尝试为多态关联模型创建一个AJAX表单。

我创建了“评论”,它与您可以评论的所有对象(例如用户个人资料,组织个人资料,事件等)具有多态关联。

我目前可以使用由以下方式创建的表单向对象添加注释:

form_for [@commentable, @comment] do |f|

我正在尝试通过Ajax创建此表单,但我一直在收到错误。

我尝试了至少十种不同的代码,使用remote_form_tag,remote_form_for等等所有不同的选项,没有任何效果。注释不会插入到数据库中。

具体来说,我试过了:

<% remote_form_for(:comment, :url => comments_path(@profile)) do |f| -%>

在我的routes.rb中,个人资料有很多评论。评论属于个人资料。但是当我提交表单时没有任何反应,评论也没有发布到数据库中。

有谁能告诉我我做错了什么?

供您参考,这是我的控制器。评论控制器:

class CommentsController < ApplicationController
  layout 'index'
  def index
    @commentable = find_commentable
    @comments = @commentable.comments
  end

  def show
    @comment = Comment.find(params[:id])
  end

  def new
    @comment = Comment.new
  end

  def create
    @commentable = find_commentable
    @comment = @commentable.comments.build(params[:comment])
    @comment.user_id = current_user.id
    if @comment.save
      responsetext = "<p><b>Comment: </b>" + @comment.content + "</p>"
      render :text => responsetext
    else
      responsetext = "error"
      render :text => responsetext
    end
  end

  def edit
    @comment = Comment.find(params[:id])
  end

  def update
    @comment = Comment.find(params[:id])
    if @comment.update_attributes(params[:comment])
      flash[:notice] = "Successfully updated comment."
      redirect_to @comment
    else
      render :action => 'edit'
    end
  end

  def destroy
    @comment = Comment.find(params[:id])
    @comment.destroy
    flash[:notice] = "Successfully destroyed comment."
    redirect_to comments_url
  end

  private

  def find_commentable
    params.each do |name, value|
      if name =~ /(.+)_id$/
        return $1.classify.constantize.find(value)
      end
    end
    nil
  end
end

配置文件控制器:

class ProfilesController < ApplicationController
  # GET /profiles
  # GET /profiles.xml
  layout 'index'
  def index
    #@profile = Profile.find_by_user_id(current_user.id)

    @profile = current_user.profile
    @comment = Comment.new

    respond_to do |format|
      format.html # index.html.erb
      format.xml  { render :xml => @profile }
    end
  end

  # GET /profiles/1
  # GET /profiles/1.xml
  def show
    @profile = Profile.find(params[:id])
    @commentable = @profile
    @comment = Comment.new(:commentable => @profile)
    respond_to do |format|
      format.html # show.html.erb
      format.xml  { render :xml => @profile }
    end
  end

  # GET /profiles/new
  # GET /profiles/new.xml
  def new
    @profile = Profile.new

    respond_to do |format|
      format.html # new.html.erb
      format.xml  { render :xml => @profile }
    end
  end

  # GET /profiles/1/edit
  def edit
    #@profile = Profile.find(params[:id])
    @profile = current_user.profile

  end

  # POST /profiles
  # POST /profiles.xml
  def create
    @profile = Profile.new(params[:profile])

    respond_to do |format|
      if @profile.save
        flash[:notice] = 'Profile was successfully created.'
        format.html { redirect_to(@profile) }
        format.xml  { render :xml => @profile, :status => :created, :location => @profile }
      else
        format.html { render :action => "new" }
        format.xml  { render :xml => @profile.errors, :status => :unprocessable_entity }
      end
    end
  end

  # PUT /profiles/1
  # PUT /profiles/1.xml
  def update
    @profile = Profile.find(params[:id])

    respond_to do |format|
      if @profile.update_attributes(params[:profile])
        flash[:notice] = 'Profile was successfully updated.'
        format.html { redirect_to(@profile) }
        format.xml  { head :ok }
      else
        format.html { render :action => "edit" }
        format.xml  { render :xml => @profile.errors, :status => :unprocessable_entity }
      end
    end
  end

  # DELETE /profiles/1
  # DELETE /profiles/1.xml
  def destroy
    @profile = Profile.find(params[:id])
    @profile.destroy

    respond_to do |format|
      format.html { redirect_to(profiles_url) }
      format.xml  { head :ok }
    end
  end
end

查看:

<% remote_form_for([@commentable,@comment], :url => '/profiles/1/comments') do |f| -%>

<% #form_for [@commentable, @comment] do |f| %>



  <br /> 
<%= f.text_area :content %><br />

<%= submit_tag 'Post', :disable_with => 'Please wait...' %>

 <% end %>


    <h2>Comments</h2>
    <div id="comments">
    <% @profile.comments.each do |c| %>
      <div>
        <div style="float:left;width:50px">
        <%= image_tag c.user.profile.photo.url(:thumb) %>
        </div>
        <div style="float:left;padding:5px">
        <b><%= link_to c.user.name, profile_path(c.user.profile) %></b>
        <%=h c.content %><br />
        <font style="color:grey"><%=h distance_of_time_in_words(Time.at(c.created_at.to_i).to_i,Time.now.to_i, include_seconds = true)  %> ago</font>
        </div>
      </div>
    <div style="clear:both"></div>
    <% end %>
    </div>

1 个答案:

答案 0 :(得分:1)

你能粘贴控制器代码吗?您需要在个人资料模型中包含accepts_nested_attributes_for:comments。在您的控制器中,您需要

profile[:comments_attributes] = params[:comment]
profile.save