【问题标题】:form_for remote true is not working in ruby on railsform_for remote true 在 ruby​​ on rails 中不起作用
【发布时间】:2013-02-14 07:09:47
【问题描述】:

我想通过使用 ajax 和加载器来创建任务。 这是我的看法 -

<%= form_for(@task, :remote => "true") do |f| %>
<center><h3>Organize Your Tasks</h3></center>
<table class="table table-bordered table-condensed table-striped">
<tr>
<td> 
     <%= f.select :taskcategory, options_for_select(["Money/Career", "Relationship", "Safety", "Health Care", "God", "Hobbies", "Society"]), {}, {:multiple => false} %>
    </td>
    <td style="width:120px;">
   <%= f.text_field :taskname, :placeholder => "Task Name" %>
</td>
<td>
  <%= f.select :importance, options_for_select(["Highly Important", "Important", "Less Important"]), {}, {:multiple => false} %></center>           
    </td>   
<td  style="width:120px;">
       <%= f.text_field :startdate , :id => "from", :placeholder => "Start Date", :value => "#{Date.today.strftime("%d/%m/%Y") }" %>   
    </td>   
<td  style="width:120px;">
      <%= f.text_field :targetdate , :id => "to", :placeholder => "End Date", :disabled => "ture"%>   
     </td>
    <td>                     
     <%= f.submit "Create", class: "btn"%> 
    </td>                
   </tr>     
</table>  
<% end %>

这是我的控制器 -

def create
  @task= current_user.tasks.build(params[:task])   
  respond_to do |format|
    if @task.save 
      flash[:success] = "Task created!"
      format.html {redirect_to root_url }
      format.js { redirect_to}
    else         
      format.html { render 'static_pages/index'}
    end
  end
end

这里我写了一个代码来显示所有的任务

<div class="tabbable" style="margin-left:-50px" >
   <ul class="nav nav-tabs span10" id="myTab" style="margin-left:50px">
     <li class="active"><a href="#tab1">All Tasks(<%=   current_user.tasks.where(:status => 'active').count %>)</a></li>
     <li ><a href="#tab2">Dreams(<%= current_user.tasks.where(:importance => 'Highly Important', :status => 'active').count %>)</a></li>
     <li ><a href="#tab3" >Today's Tasks(<%= current_user.tasks.where(:targetdate => Date.today.to_s, :status => 'active').count %>)</a></li>
     <li ><a href="#tab4" >Imp Tasks(<%= current_user.tasks.where(:importance => 'Important', :status => 'active').count %>)</a></li>
     <li id="fat-menu" class="dropdown aa">
     <a href="#" class="dropdown-toggle" data-toggle="dropdown">Others</a>
     <ul class="dropdown-menu">
    <li><a href="#tab5" >Postpone(<%= current_user.tasks.where(:status => 'postpone').count %>)</a></li>
    <li><a href="#tab6" >Satisfied(<%= current_user.tasks.where(:status => 'satisfied').count%>)</a></li>
    <li><a href="#tab7" >Unsatisfied(<%= current_user.tasks.where(:status => 'unsatisfied').count%>)</a></li>
    <li><a href="#tab8" >Closed(<%= current_user.tasks.where(:status => 'closed').count%>)</a></li>
     </ul>
  </li>
</ul>
 <div class="tab-content span10" >
  <div class="tab-pane active" id="tab1">    
<%= render 'shared/feed' %> 
  </div>
  <div class="tab-pane" id="tab2">
   <%= render 'shared/feed_dream' %> 
  </div>
  <div class="tab-pane" id="tab3">
     <%= render 'shared/feed_urgent' %> 
  </div>
  <div class="tab-pane" id="tab4">
     <%= render 'shared/feed_important' %> 
  </div>
  <div class="tab-pane" id="tab5">
     <%= render 'shared/feed_postpone' %> 
  </div>
  <div class="tab-pane" id="tab6">
    <%= render 'shared/feed_satisfied' %> 
  </div>
  <div class="tab-pane" id="tab7">
     <%= render 'shared/feed_unsatisfied' %> 
  </div>
  <div class="tab-pane" id="tab8">
    <%= render 'shared/feed_closed' %> 
  </div>
</div>

这是我的 create.js.ejb ---

page.replace_html('index' , redirect_to root_url)

当我提交我的任务时,它会在数据库中创建,但页面没有更新。当我再次刷新页面时,就会显示任务。我知道 create.js.ejb 文件中的问题。如何在我的表格中显示更新任务?

【问题讨论】:

  • page.replace_html 不再适用于 rails 3

标签: ruby-on-rails ruby ajax ruby-on-rails-3 erb


【解决方案1】:

首先确保您已将 jquery 和 jquery_ujs 包含到您的 application.js 文件中。

从视图重定向是一个坏习惯。让它format.js { redirect_to root_url}

并使用*.js.erb 模板而不是rjs 模板。

create.js.erb 你可能会做这样的事情

$("#some_div").append("some HTML content that is related to created record")

希望对你有帮助

【讨论】:

    【解决方案2】:
    page.replace_html('index' , redirect_to root_url) // this is invalid
    

    “page.replace_html”不再适用于 rails 3

    在 create.js.erb 中

    alert("record created");
    // or update div with id some_div_id
    $("#some_div_id").html("<%= escape_javascript(render :partial => 'some_partial') %>");
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2023-04-05
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-01-06
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多