我是 react.js 的新手,我试图在表中以 JSON 格式获取服务器端数据。所以我所做的是:
export default class TableUser extends React.Component {
constructor(props) {
super(props);
this.state = {
table: [],
}
}
componentDidMount(){
axios.get('http://www.reddit.com/r/reactjs.json')
.then((response)=>{
const table = response.data.map(obj => obj.data);
this.setState({ table });
})
.catch((err)=>{
})
}
Run Code Online (Sandbox Code Playgroud)
我把它渲染成<div>这样:
render(){
<div><p>{this.state.table.kind}</p></div>
}
Run Code Online (Sandbox Code Playgroud)
为什么我没有得到 的价值kind?请推荐!!
是否有Ruby(最好)或Rails方法来检查数组的第二个索引是否存在?
在我的Rails(4.2.6)应用程序中,我在视图中有以下代码,显示了一组照片的前两个缩略图:
<% if array.photos.any? %>
<%= image_tag array.photos.first.image.url(:thumb) %>
<%= image_tag array.photos[1].image.url(:thumb) %>
<% end %>
Run Code Online (Sandbox Code Playgroud)
但是,如果数组中没有第二项,则会出现错误
我已经尝试了以下if语句来使第二个缩略图的呈现有条件,但它们不起作用:
<% if array.photos.include?(1) %>
<% if array.photos.second? %>
<% if array.photos[1]? %>
<% if array.photos[1].any? %>
Run Code Online (Sandbox Code Playgroud)
我认为另一种获得我想要的方法是简单地检查数组的长度
我仍然想知道Ruby(或Rails)是否有方法或方法来检查数组中是否存在特定索引.提前致谢
编辑:澄清我只想显示数组中的前两个缩略图,如果有的话
我正在使用Wicked PDF,并且在单击按钮时在新选项卡中生成了pdf。但是我想通过单击按钮自动打印pdf 。我添加了这个print_media_type并将值设置为true,我希望我的打印机选项可以开始工作,但是什么也不会发生!有什么想法吗?
respond_to do |format|
format.html
format.pdf do
render pdf: "file_name", :template => 'officials/individual_receipt_export.html.erb', encoding: 'utf8',page_size: 'A4',:print_media_type => true
end
end
Run Code Online (Sandbox Code Playgroud) 这可能是一个noobish问题,但我有一个number_field只能接受3到5之间的值,只能接受整数(整数).
现在我已经设法用最大值和最小值来获得范围,但我仍然可以填写3,5例如.有没有办法设置它?
我一直在看文档,但找不到任何东西.另外,由于我使用的模板,我不想使用像simple_form这样的任何宝石.
我正在使用Apartment Gem构建一个Rails 4应用程序,我仍然坚持如何继续定义我的邮件程序主机..这是一个教程系列的一部分,在几集之后才停止..
现在最大的问题是这一切都在开发模式下工作,但是当我推送到Heroku,创建一个帐户时,会发送确认电子邮件,但是当我去确认时,确认会将我重定向回我的localhost(lvh.me) :3000)并且生产应用程序未获得确认更新.
我的应用程序控制器中有以下内容:
def set_mailer_host
subdomain = current_account ? "#{current_account.subdomain}." : ""
ActionMailer::Base.default_url_options[:host] = "#{subdomain}lvh.me:3000"
end
Run Code Online (Sandbox Code Playgroud)
我想添加一个if else这样的声明:
def set_mailer_host
if environment == development
subdomain = current_account ? "#{current_account.subdomain}." : ""
ActionMailer::Base.default_url_options[:host] = "#{subdomain}lvh.me:3000"
else
subdomain = current_account ? "#{current_account.subdomain}"
ActionMailer::Base.default_url_options[:host] = "#{subdomain}patrolvault.co"
end
Run Code Online (Sandbox Code Playgroud)
我只是想弄明白,或者我是否应该采取另一种方法来解决这个问题?