AJAX Ruby on Rails |开发人员指南部分不重新渲染

时间:2019-01-06 00:49:49

标签: ruby-on-rails ruby ruby-on-rails-5 rails-ujs

嗨,我是Rails的新手,正在尝试在Rails应用程序中实现AJAX。

问题在于,当我提交表单时,似乎肯定调用了create动作,因为当我刷新页面时,会出现新元素,但表不会自动刷新。

topics / show.html.erb相关代码:

<div class="row" id="toberefreshed">
  <%= render('show_partial', :locals => {:topic => @topic}) %>
</div>

topics / _show_partial.html.erb相关代码:

<table class = "opinionlist" align="center">
  <% opinions = @topic.opinions %>
  <% cons = opinions.select{|opinion| opinion.type_of == 'con'} %>
  <% pros = opinions.select{|opinion| opinion.type_of == 'pro'} %>
  <tr>
    <% pros.each do |pro|%>
      <td>
        <div class="article-body">
          <%= pro.content %>
        </div>
        <div class="article-meta-details">
          <small>
            Created by: <%= pro.user.username %>
            <%= time_ago_in_words(pro.created_at) %> ago,
          </small>
        </div>
      </td>
    <% end %>
    <% if pros.length < cons.length %>
      <% difference = cons.length - pros.length%>
      <% difference.times do %>
        <%= content_tag(:td) %>
      <% end %>
    <% end %>

    <td><%= render 'opinions/form', opinion: Opinion.new, typeOf: "pro", :topic => @topic %></td>
  </tr>
  <tr>
    <% cons.each do |con|%>
      <td>
        <div class="article-body">
          <%= con.content %>
        </div>
        <div class="article-meta-details">
          <small>
            Created by: <%= con.user.username %>
            <%= time_ago_in_words(con.created_at) %> ago,
          </small>
        </div>
      </td>
    <% end %>
    <% if pros.length > cons.length %>
      <% difference = pros.length - cons.length %>
      <%  puts "DIFFERENCE: " + difference.to_s  %>
      <% difference.times do %>
        <%= content_tag(:td) %>
      <% end %>
    <% end %>
    <td>
      <%= render 'opinions/form', opinion: Opinion.new, typeOf: "con", :topic => @topic %>
    </td>
  </tr>
</table>

topics / updated_view.js.erb:

$("toberefreshed").html("<%= escape_javascript(render("topics/show_partial", :locals => {:topic => @opinion.topic})) %>");

opinions_controller.rb

class OpinionsController < ApplicationController
  def create
    @opinion = Opinion.new(opinion_params)
    @opinion.user = current_user
    @opinion.topic = Topic.find(params[:to_find_id])
    @opinion.type_of = params[:type_of]
    if @opinion.save
      flash[:success] = 'Opinion Added'
    else
      puts @opinion.errors.full_messages
      flash[:danger] = 'Opinion not Added'
    end
    respond_to do |format|
      format.js
    end
  end
  private
  def opinion_params
    params.require(:opinion).permit(:content)
  end
end

编辑:Opinions / _form.html.erb

<div class="row">
  <div class="col-xs-12">
    <%= form_for(opinion, :html=> {class:"form-horizontal", role:"form"}, remote: true) do |f| %>
      <div class="form-group">
        <div class="col-sm-12">
          <%= f.text_area :content, rows:4, class: "form-control", placeholder: "Opinion" %>
        </div>
      </div>
      <%= hidden_field_tag 'type_of', typeOf %>
      <%= hidden_field_tag :to_find_id, @topic.id %>
      <% puts "ID: " + @topic.id.to_s %>
      <div class="form-group">
        <div class="col-sm-12">
          <%= f.submit %>
        </div>
      </div>
    <% end %>
  </div>
</div>

非常感谢您的帮助, 拉伊希尔

1 个答案:

答案 0 :(得分:0)

if (!empty($options[ 'callbacks' ])) { foreach ($options[ 'callbacks' ] as $callback => $variables) { $this->$callback($variables); } }

替换

topics/_show_partial.html.erb在第二行

<% opinions = @topic.opinions %>

您正在通过局部变量将<% opinions = topic.opinions %>变量传递给局部变量,但是您正在调用topic,而调用create方法时该变量不存在

还要更改以下内容:

instance variable @topic

收件人

<%= render 'opinions/form', opinion: Opinion.new, typeOf: "pro", :topic => @topic %>


并更改

<%= render 'opinions/form', opinion: Opinion.new, typeOf: "pro", :topic => topic %>

收件人

<%= render 'opinions/form', opinion: Opinion.new, typeOf: "con", :topic => @topic %>


因此,您在<%= render 'opinions/form', opinion: Opinion.new, typeOf: "con", :topic => topic %>中的内容应如下所示:

topics/_show_partial.html.erb

您可以推荐的最后一件事:

您在视图内部建立了很多逻辑,例如<table class = "opinionlist" align="center"> <% opinions = topic.opinions %> <% cons = opinions.select{|opinion| opinion.type_of == 'con'} %> <% pros = opinions.select{|opinion| opinion.type_of == 'pro'} %> <tr> <% pros.each do |pro|%> <td> <div class="article-body"> <%= pro.content %> </div> <div class="article-meta-details"> <small> Created by: <%= pro.user.username %> <%= time_ago_in_words(pro.created_at) %> ago, </small> </div> </td> <% end %> <% if pros.length < cons.length %> <% difference = cons.length - pros.length%> <% difference.times do %> <%= content_tag(:td) %> <% end %> <% end %> <td><%= render 'opinions/form', opinion: Opinion.new, typeOf: "pro", :topic => topic %></td> </tr> <tr> <% cons.each do |con|%> <td> <div class="article-body"> <%= con.content %> </div> <div class="article-meta-details"> <small> Created by: <%= con.user.username %> <%= time_ago_in_words(con.created_at) %> ago, </small> </div> </td> <% end %> <% if pros.length > cons.length %> <% difference = pros.length - cons.length %> <% puts "DIFFERENCE: " + difference.to_s %> <% difference.times do %> <%= content_tag(:td) %> <% end %> <% end %> <td> <%= render 'opinions/form', opinion: Opinion.new, typeOf: "con", :topic => topic %> </td> </tr> </table> ,您在视图内部创建了许多局部变量。我建议您使用另一种方法,例如在像topics/_show_partial.html.erb这样的视图之前的所有层之间构建所有变量,请查看有关presenters的讨论

What is the Rails Presenters folder for?

rails presenters应该保持简单,并尽可能避免出现逻辑问题