Rails 新列表创建表单返回时带有 304 http 代码而不是 200
Rails new listing creation form gets returned with a 304 http code instead of 200
我有一个 rails 表格 new.html.erb。
当我手动转到此 link 时,我在其获取请求中得到 200 OK。
但是当我从另一个页面来到这个页面时,比方说我的根页面,我得到了 304-not modified http 状态代码,因此我认为我的一些 document.ready 代码没有得到正确执行.
这是我的 new.html.erb 文件
<div class="ui padded page grid">
<div class="ui eight wide column">
<%=form_for @listing, :html=> { class:'ui form centered teal segment'},remote: true do |f|%>
<%= hidden_field_tag :authenticity_token, form_authenticity_token -%>
<a class="ui teal ribbon label">
New Listing</a>
<h2 class="ui header">
<i class="settings icon"></i>
<div class="content">
Create New Listing
<div class="sub header">Manage your preferences</div>
</div>
</h2>
<div class="required field">
<label>Title</label>
<%=f.text_field :title,placeholder:'Title of the listing'%>
</div>
<div class="two fields">
<div class="required field">
<label>Pet</label>
<%= f.select(:pet_type,options_for_select([['Dog',1,{class:'item'}],['Cat',2,{class:'item'}],['Bird',3,{class:'item'}]]),{prompt:'Pet'},class:'ui dropdown pet_type')%>
</div>
<div class="field">
<label>Breed</label>
<%= f.select(:breed_type,options_for_select(Breed.all.collect{|x| [x.name,x.id,class:'item']}),{prompt:'Breed'},class:'ui dropdown disabled breed_type')%>
</div>
</div>
<div class="two fields">
<div class="required field">
<label>Gender</label>
<%= f.select(:gender,options_for_select([['Male',1,{class:'item'}],['Female',2,{class:'item'}]]),{prompt:'Gender'},class:'ui dropdown gender')%>
</div>
<div class="field">
<label>State</label>
<select class="ui search dropdown">
<option value="">State</option>
<option value="AL">Alabama</option>
<option value="AL">Alabama</option>
<option value="AK">Alaska</option>
<option value="AZ">Arizona</option>
<option value="AR">Arkansas</option>
<option value="CA">California</option>
<option value="CO">Colorado</option>
<option value="CT">Connecticut</option>
<option value="DE">Delaware</option>
<option value="DC">District Of Columbia</option>
<option value="FL">Florida</option>
<option value="GA">Georgia</option>
<option value="HI">Hawaii</option>
<option value="ID">Idaho</option>
<option value="IL">Illinois</option>
<option value="IN">Indiana</option>
<option value="IA">Iowa</option>
<option value="KS">Kansas</option>
<option value="KY">Kentucky</option>
<option value="LA">Louisiana</option>
<option value="ME">Maine</option>
<option value="MD">Maryland</option>
<option value="MA">Massachusetts</option>
<option value="MI">Michigan</option>
<option value="MN">Minnesota</option>
<option value="MS">Mississippi</option>
<option value="MO">Missouri</option>
<option value="MT">Montana</option>
<option value="NE">Nebraska</option>
<option value="NV">Nevada</option>
<option value="NH">New Hampshire</option>
<option value="NJ">New Jersey</option>
<option value="NM">New Mexico</option>
<option value="NY">New York</option>
<option value="NC">North Carolina</option>
<option value="ND">North Dakota</option>
<option value="OH">Ohio</option>
<option value="OK">Oklahoma</option>
<option value="OR">Oregon</option>
<option value="PA">Pennsylvania</option>
<option value="RI">Rhode Island</option>
<option value="SC">South Carolina</option>
<option value="SD">South Dakota</option>
<option value="TN">Tennessee</option>
<option value="TX">Texas</option>
<option value="UT">Utah</option>
<option value="VT">Vermont</option>
<option value="VA">Virginia</option>
<option value="WA">Washington</option>
<option value="WV">West Virginia</option>
<option value="WI">Wisconsin</option>
<option value="WY">Wyoming</option>
</select>
</div>
</div>
<div class="required field">
<label>Tell everyone in short about your pet</label>
<%=f.text_area :love_for_pets%>
</div>
<h4 class="ui dividing header">Additional Info</h4>
<div class="two fields">
<div class="required field">
<label>Price</label>
<div class="ui icon input">
<%=f.text_field :price,placeholder:"Price"%>
<i class="user icon"></i>
</div>
</div>
<div class="field">
<label>Phone Number</label>
<div class="ui icon input">
<%=f.text_field :phone_no,placeholder:"Phone_no"%>
<i class="call icon"></i>
</div>
</div>
</div>
<div class="ui hidden divider"></div>
<div class="field">
<div class="ui checkbox">
<input type="checkbox" name="hot-deals">
<label>I agree to the <a href="#">Terms of Service</a>.</label>
</div>
</div>
<div class="ui error message">
<div class="header">We noticed some issues</div>
</div>
<%=f.submit 'Register',class:'ui button'%>
<div class="ui error message"></div>
<%end%>
</div>
<div class="ui eight wide column">
<div class="ui teal segment dropzone" id="media-dropzone">
<!--p class="dz-default dz-message"><span>Drop files here to upload</span>
</p-->
<h2 class="ui center aligned icon header dz-message">
<i class="upload icon"></i>
<div class="content">
Upload File
<div class="sub header">Drop your images here</div>
</div>
</h2>
</div>
</div>
</div>
因此我的 dropzone 没有得到 initialized.Also 语义 ui 下拉列表无法显示。
我假设这背后的原因是 document.ready 没有被击中,因此 dom 元素初始化没有发生。
/listing.js.erb/
$(document).ready(function()
{
/*Dropzone.options.mediaDropzone=false;*/
Dropzone.autoDiscover = false;
mediaDropzone=new Dropzone('div#media-dropzone',
{
url:"/listings/media",
headers:
{
'X-CSRF-Token': $('meta[name="csrf-token"]').attr('content')
},
autoProcessQueue:false,
addRemoveLinks:true,
parallelUploads: 10
});
mediaDropzone.on("success",function(file,responseText)
{
console.log(responseText.file_name.url);
console.log(responseText);
});
});
/ListingController/
class ListingsController < ApplicationController
def new
@breeds=Breed.all
session[:listing].destroy
@listing=Listing.new
end
def create
@listing=Listing.new(listing_params)
if @listing.save
session[:listing]=@listing.id
respond_to do |format|
format.js
end
end
end
def media
@listing=Listing.find(session[:listing])
puts params
@photo=Photo.new(file_name:params[:file],listing:@listing)
if @photo.save!
respond_to do |format|
format.json{render :json=>@photo}
end
end
end
private
def listing_params
params.require(:listing).permit!
end
end
/routes.rb/
Rails.application.routes.draw do
root 'prime_petz#home'
get 'prime_petz/home'
get 'prime_petz/about'
get 'prime_petz/contact'
post 'listings/media'
resources :listings
resources :prime_petz
end
Turbolinks in Rails 在应用内导航时禁用 document.ready()
。您必须在 link 选项中禁用它:
<%= link_to('foo', @foo, 'data-no-turbolink' => true) %>
或者您必须安装 turbolinks jquery gem:
304 状态代码并不表示有错误。这意味着资产文件与浏览器的匹配。 In Rails 3.2.3 server I get error 304 Not Modified (5ms)
http://en.wikipedia.org/wiki/List_of_HTTP_status_codes#3xx_Redirection
这似乎是 turbolinks 的问题。
我有一个 rails 表格 new.html.erb。 当我手动转到此 link 时,我在其获取请求中得到 200 OK。
但是当我从另一个页面来到这个页面时,比方说我的根页面,我得到了 304-not modified http 状态代码,因此我认为我的一些 document.ready 代码没有得到正确执行.
这是我的 new.html.erb 文件
<div class="ui padded page grid">
<div class="ui eight wide column">
<%=form_for @listing, :html=> { class:'ui form centered teal segment'},remote: true do |f|%>
<%= hidden_field_tag :authenticity_token, form_authenticity_token -%>
<a class="ui teal ribbon label">
New Listing</a>
<h2 class="ui header">
<i class="settings icon"></i>
<div class="content">
Create New Listing
<div class="sub header">Manage your preferences</div>
</div>
</h2>
<div class="required field">
<label>Title</label>
<%=f.text_field :title,placeholder:'Title of the listing'%>
</div>
<div class="two fields">
<div class="required field">
<label>Pet</label>
<%= f.select(:pet_type,options_for_select([['Dog',1,{class:'item'}],['Cat',2,{class:'item'}],['Bird',3,{class:'item'}]]),{prompt:'Pet'},class:'ui dropdown pet_type')%>
</div>
<div class="field">
<label>Breed</label>
<%= f.select(:breed_type,options_for_select(Breed.all.collect{|x| [x.name,x.id,class:'item']}),{prompt:'Breed'},class:'ui dropdown disabled breed_type')%>
</div>
</div>
<div class="two fields">
<div class="required field">
<label>Gender</label>
<%= f.select(:gender,options_for_select([['Male',1,{class:'item'}],['Female',2,{class:'item'}]]),{prompt:'Gender'},class:'ui dropdown gender')%>
</div>
<div class="field">
<label>State</label>
<select class="ui search dropdown">
<option value="">State</option>
<option value="AL">Alabama</option>
<option value="AL">Alabama</option>
<option value="AK">Alaska</option>
<option value="AZ">Arizona</option>
<option value="AR">Arkansas</option>
<option value="CA">California</option>
<option value="CO">Colorado</option>
<option value="CT">Connecticut</option>
<option value="DE">Delaware</option>
<option value="DC">District Of Columbia</option>
<option value="FL">Florida</option>
<option value="GA">Georgia</option>
<option value="HI">Hawaii</option>
<option value="ID">Idaho</option>
<option value="IL">Illinois</option>
<option value="IN">Indiana</option>
<option value="IA">Iowa</option>
<option value="KS">Kansas</option>
<option value="KY">Kentucky</option>
<option value="LA">Louisiana</option>
<option value="ME">Maine</option>
<option value="MD">Maryland</option>
<option value="MA">Massachusetts</option>
<option value="MI">Michigan</option>
<option value="MN">Minnesota</option>
<option value="MS">Mississippi</option>
<option value="MO">Missouri</option>
<option value="MT">Montana</option>
<option value="NE">Nebraska</option>
<option value="NV">Nevada</option>
<option value="NH">New Hampshire</option>
<option value="NJ">New Jersey</option>
<option value="NM">New Mexico</option>
<option value="NY">New York</option>
<option value="NC">North Carolina</option>
<option value="ND">North Dakota</option>
<option value="OH">Ohio</option>
<option value="OK">Oklahoma</option>
<option value="OR">Oregon</option>
<option value="PA">Pennsylvania</option>
<option value="RI">Rhode Island</option>
<option value="SC">South Carolina</option>
<option value="SD">South Dakota</option>
<option value="TN">Tennessee</option>
<option value="TX">Texas</option>
<option value="UT">Utah</option>
<option value="VT">Vermont</option>
<option value="VA">Virginia</option>
<option value="WA">Washington</option>
<option value="WV">West Virginia</option>
<option value="WI">Wisconsin</option>
<option value="WY">Wyoming</option>
</select>
</div>
</div>
<div class="required field">
<label>Tell everyone in short about your pet</label>
<%=f.text_area :love_for_pets%>
</div>
<h4 class="ui dividing header">Additional Info</h4>
<div class="two fields">
<div class="required field">
<label>Price</label>
<div class="ui icon input">
<%=f.text_field :price,placeholder:"Price"%>
<i class="user icon"></i>
</div>
</div>
<div class="field">
<label>Phone Number</label>
<div class="ui icon input">
<%=f.text_field :phone_no,placeholder:"Phone_no"%>
<i class="call icon"></i>
</div>
</div>
</div>
<div class="ui hidden divider"></div>
<div class="field">
<div class="ui checkbox">
<input type="checkbox" name="hot-deals">
<label>I agree to the <a href="#">Terms of Service</a>.</label>
</div>
</div>
<div class="ui error message">
<div class="header">We noticed some issues</div>
</div>
<%=f.submit 'Register',class:'ui button'%>
<div class="ui error message"></div>
<%end%>
</div>
<div class="ui eight wide column">
<div class="ui teal segment dropzone" id="media-dropzone">
<!--p class="dz-default dz-message"><span>Drop files here to upload</span>
</p-->
<h2 class="ui center aligned icon header dz-message">
<i class="upload icon"></i>
<div class="content">
Upload File
<div class="sub header">Drop your images here</div>
</div>
</h2>
</div>
</div>
</div>
因此我的 dropzone 没有得到 initialized.Also 语义 ui 下拉列表无法显示。
我假设这背后的原因是 document.ready 没有被击中,因此 dom 元素初始化没有发生。
/listing.js.erb/
$(document).ready(function()
{
/*Dropzone.options.mediaDropzone=false;*/
Dropzone.autoDiscover = false;
mediaDropzone=new Dropzone('div#media-dropzone',
{
url:"/listings/media",
headers:
{
'X-CSRF-Token': $('meta[name="csrf-token"]').attr('content')
},
autoProcessQueue:false,
addRemoveLinks:true,
parallelUploads: 10
});
mediaDropzone.on("success",function(file,responseText)
{
console.log(responseText.file_name.url);
console.log(responseText);
});
});
/ListingController/
class ListingsController < ApplicationController
def new
@breeds=Breed.all
session[:listing].destroy
@listing=Listing.new
end
def create
@listing=Listing.new(listing_params)
if @listing.save
session[:listing]=@listing.id
respond_to do |format|
format.js
end
end
end
def media
@listing=Listing.find(session[:listing])
puts params
@photo=Photo.new(file_name:params[:file],listing:@listing)
if @photo.save!
respond_to do |format|
format.json{render :json=>@photo}
end
end
end
private
def listing_params
params.require(:listing).permit!
end
end
/routes.rb/
Rails.application.routes.draw do
root 'prime_petz#home'
get 'prime_petz/home'
get 'prime_petz/about'
get 'prime_petz/contact'
post 'listings/media'
resources :listings
resources :prime_petz
end
Turbolinks in Rails 在应用内导航时禁用 document.ready()
。您必须在 link 选项中禁用它:
<%= link_to('foo', @foo, 'data-no-turbolink' => true) %>
或者您必须安装 turbolinks jquery gem:
304 状态代码并不表示有错误。这意味着资产文件与浏览器的匹配。 In Rails 3.2.3 server I get error 304 Not Modified (5ms) http://en.wikipedia.org/wiki/List_of_HTTP_status_codes#3xx_Redirection
这似乎是 turbolinks 的问题。