我有一个表单试图读取JSON文件以解析/ actions/etc. 我在控制器中读取它时遇到问题.
视图:
<%= form_tag({:controller => :admins, :action => :upload_json}, {:multipart => true, :method => :post}) do |f| %>
    <%= file_field_tag 'datafile' %>
<%= submit_tag "Upload" %>
Run Code Online (Sandbox Code Playgroud)
控制器:
def upload_json
  file_data = params[:datafile]
  File.read(file_data) do |file|
     file.each do |line|
       ## does stuff here....
     end
  end
end
Run Code Online (Sandbox Code Playgroud)
seed.rb当我播种数据时,类似的功能在我的文件中起作用- 只是无法在上传的文件中读取它.
我得到的错误是:can't convert ActionDispatch::Http::UploadedFile into String.
在此先感谢您的帮助!
我有一个应用程序,用户的会员资格到期.
我正在设置一个before_filter在我的applications.rb文件中,以确保他们的成员资格在让他们进入网站之前是有效的.
在我的application.rb文件中:
before_filter :check_account
def check_account
  if user_signed_in?
     if current_user.account.expired
       flash[:error] = "Your account is expired. Please contact Navanti for renewal."
       redirect_to destroy_user_session_path
     end
  end
end
Run Code Online (Sandbox Code Playgroud)
我不断收到重定向循环错误.我猜这是因为正在调用的注销页面也正在执行before_filter,但如果我把except => [:users => :sign_out]它仍然抛出循环错误.
谢谢您的帮助.
要求的设计方法:
# DELETE /resource/sign_out
def destroy
  redirect_path = after_sign_out_path_for(resource_name)
  signed_out = (Devise.sign_out_all_scopes ? sign_out : sign_out(resource_name))
  set_flash_message :notice, :signed_out if signed_out && is_navigational_format?  
  # We actually need to hardcode this as Rails default responder doesn't
  # support returning empty …Run Code Online (Sandbox Code Playgroud) 我正在尝试让 YAJL 在我的应用程序中工作 - 一直在通过 Twitter API 和 Digg API 对其进行测试,但我似乎无法让它工作。
我不确定我哪里出错了。使用以下代码访问 Twitter 流 API:(删除了用户名/密码)
max_allowed_errors = 1200
consecutive_errors = 0
while consecutive_errors < max_allowed_errors do
  url = URI.parse("https://[username]:[password]!@stream.twitter.com/1/statuses/sample.json")
  begin
    Yajl::HttpStream.get(url) do |status|
      consecutive_errors = 0
     # puts status.inspect
    end
  rescue Yajl::HttpStream::InvalidContentType
    consecutive_errors += 1
  end
  sleep(0.25*consecutive_errors)
end
Run Code Online (Sandbox Code Playgroud)
我收到一个错误:
Yajl::HttpStream::HttpError in DashboardsController#streamit
Code 200 expected got 0
Run Code Online (Sandbox Code Playgroud)
错误是指这一行:
Yajl::HttpStream.get(url) do |status|
Run Code Online (Sandbox Code Playgroud)
我想避免使用特定于 API 的 gem,这样我就可以重用代码并在未来使用其他 API(即 TweetStream)。
在此先感谢您的帮助!如果您有任何问题,或者我是否可以澄清这一点,请告诉我。
哦!如果您有建议,我会注意到我愿意接受其他 gems 来管理流媒体。
我有一个单行JSON文件,我试图对其进行迭代,并寻找遍历它的最佳方法。
爆炸后,看起来像:
{
 "system": "Test",
 "Continents": [
    {
        "Continent": {
            "name": "Europe",
            "location": "North"
         },
        "Continent": {
            "name": "Australia",
            "location": "South"
         },
        "Continent": {
            "name": "Asia",
            "location": "North"
         }
     }  
  ]
}
Run Code Online (Sandbox Code Playgroud)
首先,我将使用以下内容加载JSON,这将成功加载完整的JSON。
File.open(json_file, 'r') do |file|
    file.each do |line|            
      JSON.parse(line).each do |item|
        ## CODE TO ITERATE HERE
      end        
    end
 end
Run Code Online (Sandbox Code Playgroud)
我不确定该怎么做才能遍历该Continents部分并提取关联的记录。目标是循环浏览并在列表/表/显示中输出信息。
感谢您的帮助-让我知道是否可以澄清。
我正在努力上传多个文件,但我没有在表单和控制器之间正确传递参数 - 不太确定我缺少什么.
表格很简单:
<%= form_tag({:controller => :admins, :action => :upload_json}, {:multipart => true, :method => :post, :html => { :class => "form-horizontal"}}) do %>
  <fieldset>
    <legend>Data</legend>
    <ol class="field-list">
      <li>
        <label for="data-file">JSON File</label>
        <%= file_field_tag 'jsonfileupload', multiple: true %>
      </li>
    </ol>
    <div class="form-actions">
      <%= submit_tag "Upload" %>
    </div>
  </fieldset>
<% end %>
Run Code Online (Sandbox Code Playgroud)
在控制器中,我只是这样做,看看传递了什么:
@output = params[:jsonfileupload]
Run Code Online (Sandbox Code Playgroud)
在视图中,我只是debug查看返回的内容,它只会拉出任何选定文件集的最后一个文件.
#<ActionDispatch::Http::UploadedFile:0x007f95d0c21010 @original_filename="4987.json", @content_type="application/json", @headers="Content-Disposition: form-data; name=\"jsonfileupload\"; filename=\"4987.json\"\r\nContent-Type: application/json\r\n", @tempfile=#<File:/var/folders/0p/6lq88m950mgftng1qm1w63_8000194/T/RackMultipart20121114-389-t9l7vs>>
Run Code Online (Sandbox Code Playgroud)
不知道我缺少什么让它返回所有选定的文件.谢谢您的帮助!
我需要2个正则表达式,其中一个输入是:
LastName, FirstName 
Run Code Online (Sandbox Code Playgroud)
结果应该是
FirstName, LastName
Run Code Online (Sandbox Code Playgroud)
第二个正则表达式应该只给我FirstName
如果您认为我需要担心Jr,Sr,II等,请告诉我.如果是这样,可能需要更复杂的正则表达式.