0

我正在尝试从我视图中的表单提交信息,该表单将提交的信息 :hashtag 传递到模型中,然后使用该信息运行模型。但似乎这就是我的模型只是使用“hashtag”而不是表单信息来运行。我相信它很接近。我只是不知道下一步该去哪里。

主页.html.erb

  <div class="row">
      <div class="span6 offset2">

                <%= form_for :hashtag do |f| %>
                <div class="input-prepend input-append">
                <span class="add-on swag">#</span>

                <%= f.text_field :hashtag , class: "span3 inverse", id:"appendedPrependedInput" %>

                <%= f.submit "Swag!", class: "btn btn-inverse" %>
                <% end %>

        </div>
      </div>
      <div class="span4">
        <div id="hashtags">
            <% @random_hashtags.each do |hashtag| %>
            <blockquote><%= hashtag.content %></blockquote>
            <div class="from">&mdash; @<%= hashtag.screen_name %></div>
        <% end %>
        </div>
    </div>
</div>

标签.rb

class Hashtag < ActiveRecord::Base
  attr_accessible :content, :profile_image, :screen_name, :tweet_date, :tweet_id

  def self.pull_hashtag
  Twitter.search("%#{hashtag}").results.map do |tweet|
    unless exists?(tweet_id: tweet.id)
        create!(
            tweet_id: tweet.id,
            content: tweet.text,
            profile_image: tweet.profile_image_url,
            screen_name: tweet.from_user,
            tweet_date: tweet.created_at
            )   
        end     
    end
  end
end

hashtags_controller

    class HashtagsController < ApplicationController

    def home 
        @random_hashtags = Hashtag.order("RANDOM()").limit(4)
    end

    def create
        @hashtag = Hashtag.pull_hashtag(params[:search])
    end         
end

更新了我现在正在使用的代码,因为我没有向模型发布任何内容

虽然它正在提交,但从那里似乎什么都没有。

更新 2,

我正在尝试通过从表单中获取信息、在其上运行 Twitter.search 并在我的数据库中创建结果来将信息发布到数据库。

4

1 回答 1

0

你能试着用这个代替吗?

form_for @hashtag, :url => :action => 'home'

我的猜测是需要指定操作。

于 2012-12-01T01:02:26.367 回答