Вопрос

I'm learning to use padrino with haml and I get this weird behaviour while working with remote forms. The partial used for creation of an element works like a charm but the one used after the update renders in plaintext. I'm sure its a rookie mistake but I can't seem to find it.

#user.rb
get :show, :with => :id do
    @user = User.get(params[:id].to_i) #REWRITE
    @posts = @user.posts
    session[:user_id] = @user.id
    render 'user/show'
end

#post.rb
  put :update, :with => :id, :provides => :js do
    @post = post.first(:id => params[:post][:id].to_i, :user_id => session[:user_id].to_i)
    @post.attributes(:name => params[:post][:name], :up => params[:post][:up], 
                      :down => params[:post][:down])
    if @post.save
      render 'post/update'
    end
  end

#show.haml
#show
  .user
    .title= link_to @user.login, url_for(:user, :show, :id => @user.id)
    .date= time_ago_in_words(@user.created || Time.now) + ' ago'
    .password= @user.password
  #posts= partial 'post/list', :locals => { :posts => @user.posts }

#_post.haml
.post{:id => "post#{post.id}"}
  .name= post.name
  .date= time_ago_in_words(post.created || Time.now) + ' ago'
  - if post.up
    .up + 
  - if post.down 
    .down -
  = link_to "(x)", url(:post, :destroy, :id => post.id, :format => :js, :method => 'delete'), :confirm => "Sure?", :remote => true
  = link_to "(e)", url(:post, :edit, :id => post.id, :format => :js), :remote => true

#_edit.haml
- form_for :post, url(:post, :update, :id => @post.id, :format => :js), :remote => true, :method => 'put', :id => 'post_edit_form' do |f|
  = partial 'post/form', :locals => {:f => f}
  = f.text_field :id, :hidden => true
  = f.submit "Edit", :class => 'button'

#update.js.haml
:plain
  alert("ok");

And after clicking the edit button I get a white page with: alert("ok"); why is the update.js.haml page not rendered as remote js?

WEBrick log:

DEBUG -  TEMPLATE (0.0003s) /habit/update.js
DEBUG -       PUT (0.0170s) /habit/update/1.js - 200 OK
Это было полезно?

Решение

Sorry for leaving issues like these unattended for so long.

The issue you're having there is that update.js.haml is instructing Haml to render it as plain text and not inside a script tag. That's why the browser would never run it; you should be using :javascript instead, as follows:

#update.js.haml
:javascript
  alert("ok");

In any way, most times you will want to reference external assets, i.e., real JS files. If you need to bootstrap those with some data at load time you can look at alternatives such as injecting a script tag containing a variable that will set the initial data and consume it in your application (there are different ways of doing this). Alternatively, you can load that data through AJAX or WS, bear in mind that this creates more connections to the server and may have the user waiting, so it's not a good approach if the data needs to be ready at load if, say, you're building an SPA.

Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top