2016-05-17 23 views
0

私はレールを使い慣れていません。私は 'コラボレーション'データベースエントリを更新するためにform_forを使用しています。 "@interpreted_code"という名前の新しいインスタンス変数を作成するために更新関数を変更し、それを文字列に割り当てました。この変数が私の更新機能で作成/変更されたとき、私は再ロードせずに表示するようにしたいと思います。ここで が私の見解で、collab.html.erb更新要求後にビューを変更せずにDivを更新する

<%= 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> 

そして、ここでは私の更新機能は、私の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 

答えて

2

あるthis-

まずこの_interpreted_codeような部分を作成して試してみてください.html.erb

<%= @interpreted_code.inspect %> 

あなたの意見は次のように変更されました:

<%= 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> 

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 
のようなあなたのコントローラを変更し、ファイルupdate.js.erb

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

を作成します。

関連する問題