将.js.erb转换为js.haml

时间:2016-10-01 11:41:22

标签: javascript jquery ruby-on-rails ruby ajax

我有一个删除页面帖子的ajax代码:

$("#post_delete").on("ajax:success", function(data){
  $("#<%= j @post.id.to_s %>").fadeOut();
});

它仅适用于erb格式,但当我尝试使用haml时,我收到错误 - “非法嵌套”

$("#post_delete").on("ajax:success", function(data){
  $("#" + "#{j @post.id.to_s}").fadeOut();
});

2 个答案:

答案 0 :(得分:0)

在haml中,你必须像这样编写你的js代码:

class App extends React.Component {
    constructor(props) {
        super(props);
        this.state = {
            data: '',
            loading: true
        }
    }
    componentDidMount () {
        const newsfeedURL = 'https://s3-eu-west-1.amazonaws.com/streetlife-coding-challenge/newsfeed.json';
        $.get(newsfeedURL, function(result) {
            this.setState({
                data: JSON.parse(result),
                loading: false
            });
            console.log(typeof this.state.data.messages);
        }.bind(this));
    }
    render () {
      let content;
      if (this.state.loading === false && this.state.data.messages) {
        content = Object.keys(this.state.data.messages).map(key => {
         console.log(this.state.data.messages[key])
         return <div key={key}><b>Key: {key},</b> Value: {this.state.data.messages[key].body}</div>;
        })
      } else { 
        content = ''; // whatever you want it to be while waiting for data
      }
      return (
        <div>
          {content}
        </div>
      )
    }
}


ReactDOM.render(
  <App />,
  document.getElementById('app')
);

答案 1 :(得分:0)

$("#post_delete").on("ajax:success", function(data){
$("##{j @post.id.to_s}").fadeOut();
});