Ruby on rails 4 rails 4 iframe路由错误

Ruby on rails 4 rails 4 iframe路由错误,ruby-on-rails-4,iframe,coffeescript,Ruby On Rails 4,Iframe,Coffeescript,我遵循这一点只是为了了解有关实现coffeescript的更多信息 我的模型是 class Url < ActiveRecord::Base validates :url, presence: true end app/views/layouts/application.rb <!DOCTYPE html> <html> <head> <title>Shorty</title> <%= stylesheet

我遵循这一点只是为了了解有关实现coffeescript的更多信息

我的模型是

class Url < ActiveRecord::Base

  validates :url, presence: true

end
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>
错误是

没有与[GET]/url/www.rte.ie匹配的路由


我假设正在执行coffeescript,因为上面的地址是我在文本框中输入的url,而iframe在提供url之前是不可见的

您似乎需要将协议添加到url,否则在show action中将\u重定向到@shorted\u url.url时重定向将不起作用。在您的模型中,您可以通过以下方式进行保存:

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 

另外,我认为您的新操作中有一个输入错误:重定向到语句中的新url是什么?

谢谢,您的回答修复了url的保存,它让我了解了如何修复iframe问题,此修改后的咖啡脚本适用于$document.ready->preview=$preview url$'url\'url'.keyup->current\'value='http://'+$.trim@value(如果当前\'u值为preview.hide.attr'src',否则为preview.show.attr'src',current \'u值)
<!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>
<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>
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
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