简体   繁体   English

更新请求后在Rails中更新Div而不更改视图

[英]Updating Div in Rails without changing view after an update request

I'm new to rails. 我是新手。 I'm using form_for to update a 'collab' database entry. 我正在使用form_for更新'collab'数据库条目。 I've modified my update function to create a new instance variable called "@interpreted_code" and assigned it to a string. 我已经修改了更新功能,以创建一个名为“ @interpreted_code”的新实例变量,并将其分配给一个字符串。 When this variable is created/changed in my update function I'd like the view to display it without a reload. 在更新功能中创建/更改此变量后,我希望视图在不重新加载的情况下显示它。 Here is my view, collab.html.erb 这是我的观点,collab.html.erb

<%= form_for @collab, :remote=>true do |f| %>

  <p>
    Enter Ruby Code<br>
    <%= f.text_area :text %>
  </p>

  <p>
    <%= f.submit %>
  </p>

<% end %>

<div id='interpreted' > 
  <%= @interpreted_code.inspect %> #this doesn't work
</div>

And here is my update function in my controller.rb 这是我controller.rb中的更新功能

def update
    @collab = Collab.find(params[:id])

    if @collab.update(collab_params)
      File.open('rubyfile.rb', 'w') { |file| file.write(@collab.text) }
      @interpreted_code = "BLAH BLAH (change later)"
    end

    render :nothing =>true
  end

Try this- 尝试这个-

First create a partial like this _interpreted_code.html.erb 首先创建一个这样的_interpreted_code.html.erb部分

<%= @interpreted_code.inspect %>

Change your view now like this- 现在就这样更改您的视图-

<%= form_for @collab, :remote=>true do |f| %>
  <p>
   Enter Ruby Code<br>
   <%= f.text_area :text %>
  </p>

  <p>
    <%= f.submit %>
  </p>
<% end %>

<div id='interpreted' > 
  <%= render partial: "interpreted_code" %> 
</div>

Create file update.js.erb 创建文件update.js.erb

$("#interpreted").html("<%= raw escape_javascript(render(:partial => 'interpreted_code')) %>")

Change your controller like this- 像这样更改您的控制器-

def update
 @collab = Collab.find(params[:id])

 if @collab.update(collab_params)
   File.open('rubyfile.rb', 'w') { |file| file.write(@collab.text) }
   @interpreted_code = "BLAH BLAH (change later)"
 end

 respond_to do |format|
   format.js
 end
end

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM