【发布时间】:2016-04-02 07:39:55
【问题描述】:
故事:
用户单击“添加”,它将数据库中的值设置为true。点击“删除”,将数据库中的同一列更新为false:
def add
u = User.find(1)
u.foo = true
u.save
u.reload
head :ok
end
def remove
u = User.find(1)
u.foo = false
u.save
u.reload
head :ok
end
React 按钮组件:
...
_add(){
$.ajax({
url: ''url-to-the-controller-action-(add),
type: 'POST',
success: function (data) {
console.log("DB is set to: " this.props.uid)
},
error: function (data) {
console.log("not added")
}
});
},
_remove(){
$.ajax({
url: ''url-to-the-controller-action-(remove),
type: 'POST',
success: function (data) {
console.log("DB is set to: " this.props.uid)
},
error: function (data) {
console.log("not removed")
}
});
},
render(){
return(
<div>
<button onClick={this._add}>add</button>
<button onClick={this._remove}>remove</button>
</div>
)
}
...
我的按钮从另一个组件接收道具:
Foo 组件
getInitialState: function() {
return{
uid: this.props.data,
}
}
Foo 的渲染:
<Button uid={this.state.uid} />
我确信我可以在不需要flux/redux 的情况下实现这一点,因为这对于这个微小的组件来说太过分了。 I have looked into this 但不知道怎么用。
该应用可以运行,但我必须刷新页面才能看到更改。
或者,Rails 5 是我最好的动作电缆选择?
【问题讨论】:
标签: javascript ruby-on-rails ajax ruby-on-rails-4 reactjs