Javascript Rails国家/地区\u选择ReactJs中的Gem

Javascript Rails国家/地区\u选择ReactJs中的Gem,javascript,ruby-on-rails,ruby-on-rails-4,reactjs,Javascript,Ruby On Rails,Ruby On Rails 4,Reactjs,今天早上我尽量不做傻事,但我发现自己喜欢使用HTML5data-*属性。我已经安装了rails gem,它在html.erb中运行良好,格式如下: <%= f.country_select :location, { priority_countries: ["GB", "US"], selected: "GB" } %> 我愚蠢的把这件事扯进Reactjs的方式是: html.erb: <div id="foo" data-countries="<%= f.coun

今天早上我尽量不做傻事,但我发现自己喜欢使用HTML5
data-*
属性。我已经安装了rails gem,它在
html.erb
中运行良好,格式如下:

<%= f.country_select :location, { priority_countries: ["GB", "US"], selected: "GB" } %>

我愚蠢的把这件事扯进Reactjs的方式是:

html.erb:

<div id="foo" data-countries="<%= f.country_select :location, { priority_countries: ["GB", "US"], selected: "GB" } %>"></div>
getInitialState: function() {
  return {
    location: this.props.data.location
  }
},

geo: function(event) {
  this.setState({location: event.target.value});
},

render: function(){
  var bar = document.getElementById('foo');  

  return (
      <div>
        <select
           value={this.state.location}
           onChange={this.geo}
         >
           <option value="" selected disabled>Please select</option>
           <option value="{bar.dataset.countries}">{bar.dataset.countries}</option>
         </select>
      </div>
    )
 }
def country
  country = ISO3166::Country
  c = country.all
  c.map do |a| a.name end
end
def method_name
 c = User.new
 # Calling the method from user.rb
 @country = c.country
end
render: function(){
  var country = (c) => (<option key={c} value={c}>{c}</option>) 

  return (
      <div>
        <select
           value={this.state.location}
           onChange={this.geo}
         >
           <option value="" selected disabled>Please select</option>
           {this.props.country.map(country)}
         </select>
      </div>
    )

对于以上内容,我通常对方法执行此操作

js.jsx:

<div id="foo" data-countries="<%= f.country_select :location, { priority_countries: ["GB", "US"], selected: "GB" } %>"></div>
getInitialState: function() {
  return {
    location: this.props.data.location
  }
},

geo: function(event) {
  this.setState({location: event.target.value});
},

render: function(){
  var bar = document.getElementById('foo');  

  return (
      <div>
        <select
           value={this.state.location}
           onChange={this.geo}
         >
           <option value="" selected disabled>Please select</option>
           <option value="{bar.dataset.countries}">{bar.dataset.countries}</option>
         </select>
      </div>
    )
 }
def country
  country = ISO3166::Country
  c = country.all
  c.map do |a| a.name end
end
def method_name
 c = User.new
 # Calling the method from user.rb
 @country = c.country
end
render: function(){
  var country = (c) => (<option key={c} value={c}>{c}</option>) 

  return (
      <div>
        <select
           value={this.state.location}
           onChange={this.geo}
         >
           <option value="" selected disabled>Please select</option>
           {this.props.country.map(country)}
         </select>
      </div>
    )
getInitialState:function(){
返回{
位置:this.props.data.location
}
},
地理位置:功能(活动){
this.setState({location:event.target.value});
},
render:function(){
var bar=document.getElementById('foo');
返回(
请选择
{bar.dataset.countries}
)
}

在我看来,所有国家都是由
html.erb
中的
数据国家
造成的。同样在渲染下拉菜单中,我看到“请选择”和
我找到了它!基本上,您需要:

在您的用户模型中:

<div id="foo" data-countries="<%= f.country_select :location, { priority_countries: ["GB", "US"], selected: "GB" } %>"></div>
getInitialState: function() {
  return {
    location: this.props.data.location
  }
},

geo: function(event) {
  this.setState({location: event.target.value});
},

render: function(){
  var bar = document.getElementById('foo');  

  return (
      <div>
        <select
           value={this.state.location}
           onChange={this.geo}
         >
           <option value="" selected disabled>Please select</option>
           <option value="{bar.dataset.countries}">{bar.dataset.countries}</option>
         </select>
      </div>
    )
 }
def country
  country = ISO3166::Country
  c = country.all
  c.map do |a| a.name end
end
def method_name
 c = User.new
 # Calling the method from user.rb
 @country = c.country
end
render: function(){
  var country = (c) => (<option key={c} value={c}>{c}</option>) 

  return (
      <div>
        <select
           value={this.state.location}
           onChange={this.geo}
         >
           <option value="" selected disabled>Please select</option>
           {this.props.country.map(country)}
         </select>
      </div>
    )
现在在适当的控制器中的某个位置:

<div id="foo" data-countries="<%= f.country_select :location, { priority_countries: ["GB", "US"], selected: "GB" } %>"></div>
getInitialState: function() {
  return {
    location: this.props.data.location
  }
},

geo: function(event) {
  this.setState({location: event.target.value});
},

render: function(){
  var bar = document.getElementById('foo');  

  return (
      <div>
        <select
           value={this.state.location}
           onChange={this.geo}
         >
           <option value="" selected disabled>Please select</option>
           <option value="{bar.dataset.countries}">{bar.dataset.countries}</option>
         </select>
      </div>
    )
 }
def country
  country = ISO3166::Country
  c = country.all
  c.map do |a| a.name end
end
def method_name
 c = User.new
 # Calling the method from user.rb
 @country = c.country
end
render: function(){
  var country = (c) => (<option key={c} value={c}>{c}</option>) 

  return (
      <div>
        <select
           value={this.state.location}
           onChange={this.geo}
         >
           <option value="" selected disabled>Please select</option>
           {this.props.country.map(country)}
         </select>
      </div>
    )
因为我在使用,在我看来:

<%= react_component('NewProject', { country: @country }) %>

js.jsx:

<div id="foo" data-countries="<%= f.country_select :location, { priority_countries: ["GB", "US"], selected: "GB" } %>"></div>
getInitialState: function() {
  return {
    location: this.props.data.location
  }
},

geo: function(event) {
  this.setState({location: event.target.value});
},

render: function(){
  var bar = document.getElementById('foo');  

  return (
      <div>
        <select
           value={this.state.location}
           onChange={this.geo}
         >
           <option value="" selected disabled>Please select</option>
           <option value="{bar.dataset.countries}">{bar.dataset.countries}</option>
         </select>
      </div>
    )
 }
def country
  country = ISO3166::Country
  c = country.all
  c.map do |a| a.name end
end
def method_name
 c = User.new
 # Calling the method from user.rb
 @country = c.country
end
render: function(){
  var country = (c) => (<option key={c} value={c}>{c}</option>) 

  return (
      <div>
        <select
           value={this.state.location}
           onChange={this.geo}
         >
           <option value="" selected disabled>Please select</option>
           {this.props.country.map(country)}
         </select>
      </div>
    )
render:function(){
变量国家=(c)=>({c})
返回(
请选择
{this.props.country.map(国家)}
)
现在我得到了我的下拉列表。希望这对其他人有用