Ruby 将值从窗体传递到控制器变量

Ruby 将值从窗体传递到控制器变量,ruby,api,tumblr,Ruby,Api,Tumblr,我正在使用TumblrAPI来布局博客的所有帖子。这是我在控制器中的索引方法: def index # Keys given from Tumblr API @key = '[my key]' @secret = '[my secret]' @oauth_token = '[my oauth token]' @oauth_token_secret = '[my oauth token secret]' # Sets the client that

我正在使用TumblrAPI来布局博客的所有帖子。这是我在控制器中的索引方法:

def index
    # Keys given from Tumblr API
    @key = '[my key]'
    @secret = '[my secret]'
    @oauth_token = '[my oauth token]'
    @oauth_token_secret = '[my oauth token secret]'

    # Sets the client that allows interfacing with Tumblr
    @client = Tumblr::Client.new(
      :consumer_key => @key,
      :consumer_secret => @secret,
      :oauth_token => @oauth_token,
      :oauth_token_secret => @oauth_token_secret
    )

    # Make the request
    @blog = "[blogname].tumblr.com"
    @posts = @client.posts(@blog, :type => "photo")["posts"] #gets a posts array
    # @posts = Kaminari.paginate_array(@posts["posts"]).page(params[:page]).per(10)

    # # Photography posts only (other types follow the same pattern)
    # @photoPosts = @myClient.posts("YOURTUMBLR.tumblr.com", 
    #                               :limit => 5, 
    #                               :type => "photo")
    # @photoPosts = @photoPosts["posts"]

    respond_to do |format|
      format.html # index.html.erb
      format.json { render json: @posts }
    end
  end
class BlogController < ApplicationController
  def index
    @blog = "#{params[:blogname]}.tumblr.com"
  end

  def show
    # Keys given from Tumblr API
    @key = '[my key]'
    @secret = '[my secret]'
    @oauth_token = '[my oauth token]'
    @oauth_token_secret = '[my oauth token secret]'

    # Sets the client that allows interfacing with Tumblr
    @client = Tumblr::Client.new(
      :consumer_key => @key,
      :consumer_secret => @secret,
      :oauth_token => @oauth_token,
      :oauth_token_secret => @oauth_token_secret
    )

    # Make the request
    @posts = @client.posts(@blog, :type => "photo")["posts"]

    respond_to do |format|
      format.html # index.html.erb
      format.json { render json: @posts }
    end
  end
end
一切都很顺利。不过,我希望“@blog”是动态的。因此,在视图中,我需要一个表单,用户可以在其中输入tumblr博客。我只是不知道如何把两者联系起来。有人有主意吗

谢谢


编辑 控制器:

def index
    # Keys given from Tumblr API
    @key = '[my key]'
    @secret = '[my secret]'
    @oauth_token = '[my oauth token]'
    @oauth_token_secret = '[my oauth token secret]'

    # Sets the client that allows interfacing with Tumblr
    @client = Tumblr::Client.new(
      :consumer_key => @key,
      :consumer_secret => @secret,
      :oauth_token => @oauth_token,
      :oauth_token_secret => @oauth_token_secret
    )

    # Make the request
    @blog = "[blogname].tumblr.com"
    @posts = @client.posts(@blog, :type => "photo")["posts"] #gets a posts array
    # @posts = Kaminari.paginate_array(@posts["posts"]).page(params[:page]).per(10)

    # # Photography posts only (other types follow the same pattern)
    # @photoPosts = @myClient.posts("YOURTUMBLR.tumblr.com", 
    #                               :limit => 5, 
    #                               :type => "photo")
    # @photoPosts = @photoPosts["posts"]

    respond_to do |format|
      format.html # index.html.erb
      format.json { render json: @posts }
    end
  end
class BlogController < ApplicationController
  def index
    @blog = "#{params[:blogname]}.tumblr.com"
  end

  def show
    # Keys given from Tumblr API
    @key = '[my key]'
    @secret = '[my secret]'
    @oauth_token = '[my oauth token]'
    @oauth_token_secret = '[my oauth token secret]'

    # Sets the client that allows interfacing with Tumblr
    @client = Tumblr::Client.new(
      :consumer_key => @key,
      :consumer_secret => @secret,
      :oauth_token => @oauth_token,
      :oauth_token_secret => @oauth_token_secret
    )

    # Make the request
    @posts = @client.posts(@blog, :type => "photo")["posts"]

    respond_to do |format|
      format.html # index.html.erb
      format.json { render json: @posts }
    end
  end
end

读一读可能会很好

在控制器中,您可以从
params
散列中获取提交的值:

# controller
@blog = "#{params[:blogname]}.tumblr.com"
在模板中,您将实现一个表单,将值提交到rails应用程序:

# index.html.erb
<%= form_tag method: :get do %>
  <%= input_tag :blogname %>
  <%= submit_tag %>
<% end %>
#index.html.erb

上面的代码来自我的头脑,完全没有经过测试。

我设法得到了表单。我把@blog放在“index”方法中,其余的放在“show”方法中。我不知道如何把它们联系起来。顺便说一下,这里没有分贝。你能告诉我怎么把这一切放在一起吗?谢谢如果我理解正确,您在show操作中有表单标记,并希望在index操作中提交它以显示博客?这将要求您向表单中添加正确的url。路径帮助程序应该使它变得简单,这取决于如何调用控制器
form\u tag method::get,url:controller\u name\u path
。请参见
rake routes
,以找到路径的正确名称。实际上,这是另一种方法。我的索引中有一个form_标记,我想对show中的结果值做些什么。我编辑了上面的问题,告诉你我现在在哪里。在我看来,问题在于路线。当我在routes文件中添加“resources:blognames”或其他内容时,所有URL都需要一个id,我没有这个id,因为我没有使用数据库。只需使用blogname作为id即可。
# index.html.erb
<%= form_tag method: :get do %>
  <%= input_tag :blogname %>
  <%= submit_tag %>
<% end %>