[英]rails Insert meta data from external url into table
I have a table for storing links. 我有一个用于存储链接的表。 I'm trying to use the pismo gem for grabbing meta data from a url, and saving that to the table.
我正在尝试使用pismo gem从URL抓取元数据,并将其保存到表中。
In my link controller I have a before filter : 在我的链接控制器中,我有一个前置过滤器:
def pismo_grab_meta_data
doc = Pismo::Document.new("http://google.com/")
@link_title = doc.title
end
Which gets called in the new and create action. 在new和create动作中调用哪个。 In my view I pass the instance variable in a hidden field.
在我看来,我在一个隐藏字段中传递了实例变量。
<%= f.hidden_field :name, :value => @link_title %>
This works when hard code the url as a string (like the above google example), but fails when I pass it the url params, like so: 当将URL硬编码为字符串时(例如上述google示例),此方法有效,但是当我将url参数传递给URL时失败,如下所示:
doc = Pismo::Document.new(params[:url])
Error: undefined method `gsub!' 错误: 未定义的方法“ gsub!” for nil:NilClass
对于nil:NilClass
Pretty new to rails, so I assume I'm doing something stupid here. Rails刚起步,所以我认为我在这里做一些愚蠢的事情。 Any help is very welcome!
任何帮助都非常欢迎!
<%= form_for(@link, :html => { "data-behavior" => "submit-form"}) do |f| %>
<% if @link.errors.any? %>
<div id="error_explanation">
<h2><%= pluralize(@link.errors.count, "error") %>
prohibited this link from being saved:
</h2>
<ul>
<% @link.errors.full_messages.each do |msg| %>
<li><%= msg %></li>
<% end %>
</ul>
</div>
<% end %>
<%= f.hidden_field :name, :value => @link_title %>
<div class="field urlsaver">
<%= f.label :url %><br />
<% if params[:link].present? %>
<%= f.text_field :url, :value => params[:link] %>
<% else %>
<%= f.text_field :url %>
<% end %>
</div>
<div class="actions">
<%= f.submit 'Save', :class => 'btn' %>
</div>
<% end %>
The value you are looking for should be in params[:link][:url]. 您要查找的值应该在params [:link] [:url]中。 Change the line to :
将行更改为:
doc = Pismo::Document.new(params[:link][:url])
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.