rails 4 iframe路由错误
rails 4 iframe routing error
我关注这个 tutorial 只是为了了解更多关于实现 coffeescript 的信息。
我的模特是
class Url < ActiveRecord::Base
validates :url, presence: true
end
控制器
class UrlsController < ApplicationController
def new
@shortened_url = Url.new
end
def create
@shortened_url = Url.new(url_params)
if @shortened_url.save
flash[:shortened_id] = @shortened_url.id
redirect_to new_url_url
else
render :action => "new"
end
end
def show
@shortened_url = Url.find(params[:id])
redirect_to @shortened_url.url
end
private
def url_params
params.require(:url).permit(:url)
end
end
结束
咖啡脚本
$(document).ready ->
preview = $("#preview-url")
$('#url_url').keyup ->
current_value = $.trim @value
if current_value is ''
preview.hide().attr 'src', ''
else
preview.show().attr 'src', current_value
app/views/layouts/application.rb
<!DOCTYPE html>
<html>
<head>
<title>Shorty</title>
<%= stylesheet_link_tag 'application', media: 'all', 'data-turbolinks-track' => true %>
<%= javascript_include_tag 'application', 'data-turbolinks-track' => true %>
<%= csrf_meta_tags %>
</head>
<body>
<% if flash[:shortened_id].present? %>
<p class='shortened-link'>
The shortened url is available <%= link_to 'here',
url_url(flash[:shortened_id]) %>.
(Right click and copy link to share it).
</p>
<% end %>
<%= yield %>
</body>
</html>
app/views/urls/new.html.erb
<h1>Add a new URL</h1>
<%= form_for @shortened_url do |form| %>
<p>
<%= form.label :url, "Your URL:" %>
<%= form.text_field :url %>
</p>
<% if @shortened_url.errors[:url].any? %>
<p class='error-messages'>
The given url <%= @shortened_url.errors[:url].to_sentence %>.
</p>
<% end %>
<p class='buttons'>
<%= form.submit "Shorten my URL" %>
</p>
<% end %>
<iframe id='preview-url' style='width: 600px; height: 400px; display: none'></iframe>
config/routes.rb
Rails.application.routes.draw do
resources :urls, :only => [:new, :show, :create]
# get '/', to: 'urls#create', as: :new_url_url
root :to => redirect('/urls/new')
end
错误是
没有路由匹配 [GET] "/urls/www.rte.ie"
我假设正在执行 coffeescript,因为上面的地址是我在文本框中输入的 url 并且 iframe 在提供 url 之前是不可见的
您似乎需要将协议添加到 url
,否则当您在 show
操作中执行 redirect_to @shortened_url.url
时重定向将不起作用。你可以在你的模型中使用 before_save
像这样:
class Url < ActiveRecord::Base
before_save :ensure_protocol
private
def ensure_protocol
self.url = "http://#{url}" unless url_protocol_present?
end
def url_protocol_present?
u = URI.parse(url)
u.scheme
end
end
此外,我认为您的 new
操作有错别字:redirect_to
语句中的 new_url
是什么?
我关注这个 tutorial 只是为了了解更多关于实现 coffeescript 的信息。
我的模特是
class Url < ActiveRecord::Base
validates :url, presence: true
end
控制器
class UrlsController < ApplicationController
def new
@shortened_url = Url.new
end
def create
@shortened_url = Url.new(url_params)
if @shortened_url.save
flash[:shortened_id] = @shortened_url.id
redirect_to new_url_url
else
render :action => "new"
end
end
def show
@shortened_url = Url.find(params[:id])
redirect_to @shortened_url.url
end
private
def url_params
params.require(:url).permit(:url)
end
end
结束
咖啡脚本
$(document).ready ->
preview = $("#preview-url")
$('#url_url').keyup ->
current_value = $.trim @value
if current_value is ''
preview.hide().attr 'src', ''
else
preview.show().attr 'src', current_value
app/views/layouts/application.rb
<!DOCTYPE html>
<html>
<head>
<title>Shorty</title>
<%= stylesheet_link_tag 'application', media: 'all', 'data-turbolinks-track' => true %>
<%= javascript_include_tag 'application', 'data-turbolinks-track' => true %>
<%= csrf_meta_tags %>
</head>
<body>
<% if flash[:shortened_id].present? %>
<p class='shortened-link'>
The shortened url is available <%= link_to 'here',
url_url(flash[:shortened_id]) %>.
(Right click and copy link to share it).
</p>
<% end %>
<%= yield %>
</body>
</html>
app/views/urls/new.html.erb
<h1>Add a new URL</h1>
<%= form_for @shortened_url do |form| %>
<p>
<%= form.label :url, "Your URL:" %>
<%= form.text_field :url %>
</p>
<% if @shortened_url.errors[:url].any? %>
<p class='error-messages'>
The given url <%= @shortened_url.errors[:url].to_sentence %>.
</p>
<% end %>
<p class='buttons'>
<%= form.submit "Shorten my URL" %>
</p>
<% end %>
<iframe id='preview-url' style='width: 600px; height: 400px; display: none'></iframe>
config/routes.rb
Rails.application.routes.draw do
resources :urls, :only => [:new, :show, :create]
# get '/', to: 'urls#create', as: :new_url_url
root :to => redirect('/urls/new')
end
错误是
没有路由匹配 [GET] "/urls/www.rte.ie"
我假设正在执行 coffeescript,因为上面的地址是我在文本框中输入的 url 并且 iframe 在提供 url 之前是不可见的
您似乎需要将协议添加到 url
,否则当您在 show
操作中执行 redirect_to @shortened_url.url
时重定向将不起作用。你可以在你的模型中使用 before_save
像这样:
class Url < ActiveRecord::Base
before_save :ensure_protocol
private
def ensure_protocol
self.url = "http://#{url}" unless url_protocol_present?
end
def url_protocol_present?
u = URI.parse(url)
u.scheme
end
end
此外,我认为您的 new
操作有错别字:redirect_to
语句中的 new_url
是什么?