weixin_33691817 2011-08-11 15:23 采纳率: 0%
浏览 20

Rails 3.0.x AJAX问题

This is probably a very simple question to answer. i am new to Rails and am having difficulty figuring out something that I am certain is trivial. I have a simple link_to "Show" which is remote and works great. However once i click a remote link and the content is displayed into/from my partial I cannot seem to update that content by clicking again on another link. My server logs are showing the correct calls being made so I believe there must be something wrong I am doing with the callback. Maybe this has something to do with my :locals assignment in my partial. to better understand here is my code...

application.js

$(function() {
    $("#showreqs a").live("click", function() {
        $.getScript(this.href);
        return false;
    });
});

_showreq.html.erb (very simple to prove concept)

<%= @project.name %>

show.js.erb

$("#showreq").replaceWith("<%= escape_javascript(render :partial => 'showreq', :locals => { :project => @project}) %>")

projects.html.erb (only section that matters)

<div id="showreqs">
    <%= form_for(@project) do |f| %>
    <%= render 'shared/error_messages' %>
    <p>
    Name: <%= f.text_field :name %> 
    Owner: <%= f.select :user_id, options_from_collection_for_select(@users, 'id', 'name'), :class => 'genForm_dropBox' %>
    <%= f.submit "Create Project" %>
    </p>
    <% end %>
    <ol>
    <% @projects.each do |t| %>
        <li>
        <%= t.name %> - Owner: <%=  t.user.name %> - 
        <%= link_to "Show", t, :action => "show",  :remote => true %>
         |  
        <%= link_to "delete", t, :method => :delete, :confirm => "You sure?", :title => "Delete" %>
        </li>
    <% end %>
    </ol>
</div>

projectscontroller (very ugly and will clean)

class ProjectsController < ApplicationController

    def index
        @title = "Requirements" 
        @users = User.order("created_at").all
        @project = Project.new
        @projects = Project.all
    end

    def show
        @users = User.order("created_at").all
        @project = Project.find(params[:id])
        @projects = Project.all
    end

    def create
        @project = Project.new(params[:project])
        if (defined?(@project_e)).nil?
            @project_e = Project.new(params[:project])
        end

        if @project.save
            flash[:success] = "Successfully created a Project."
            redirect_to projects_path
        else
            flash[:error] = "You made some errors"
            redirect_to projects_path
        end
    end


    def update
    @project = Project.find(params[:id])
        if @user.update_attributes(params[:user])
            flash[:success] = "Project updated."
            redirect_to projects_path
        else
            flash[:error] = "You made some errors"
            redirect_to projects_path
        end
    end

    def destroy
        Project.find(params[:id]).destroy
        flash[:success] = "Project destroyed."
        redirect_to projects_path
    end

end

If you need any other code such as routes let me know but i assumed my routes and everything else is fine since this does works when "show" link is first clicked.

  • 写回答

1条回答 默认 最新

  • 三生石@ 2011-08-11 15:51
    关注

    You should use $('#showreq').html() over replaceWith, as what's happening is the #showreq id is being overwritten because it exists outside the partial

    评论

报告相同问题?

悬赏问题

  • ¥35 平滑拟合曲线该如何生成
  • ¥100 c语言,请帮蒟蒻写一个题的范例作参考
  • ¥15 名为“Product”的列已属于此 DataTable
  • ¥15 安卓adb backup备份应用数据失败
  • ¥15 eclipse运行项目时遇到的问题
  • ¥15 关于#c##的问题:最近需要用CAT工具Trados进行一些开发
  • ¥15 南大pa1 小游戏没有界面,并且报了如下错误,尝试过换显卡驱动,但是好像不行
  • ¥15 自己瞎改改,结果现在又运行不了了
  • ¥15 链式存储应该如何解决
  • ¥15 没有证书,nginx怎么反向代理到只能接受https的公网网站