【问题标题】:pass value from form to controller variable将值从表单传递给控制器​​变量
【发布时间】:2015-04-12 15:19:47
【问题描述】:

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

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

一切正常。但是,我希望“@blog”是动态的。因此,在视图中,我需要一个用户可以输入 tumblr 博客的表单。我只是不知道如何将两者联系起来。有人有想法吗?

谢谢!


编辑 控制器:

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

index.html.erb:

<%= form_tag( method: "get", url: "blog" ) do %>
  <%= text_field_tag(:blogname) %>
  <%= submit_tag %>
<% end %>

show.html.erb:

<section class="section section_grid has_no-pad">
  <h1 class="is_white is_bold is_uppercase"><%= p @posts.first["blog_name"] %></h1>

  <ul class="s-grid-2 has_isotope">
    <% @posts.each do |post| %>

      <% @type = post["type"] # the type of post %>
      <% @url = post["post_url"] # the url for the post %>

      <% if @type == "photo" %>

        <% @pictures = post["photos"] %>

        <% @pictures.each do |pic| %>
          <% @pic_url = pic["original_size"]["url"] %>

          <li>
            <%= image_tag(@pic_url) %>
          </li>
        <% end %>

      <% end %>
    <% end %>
  </ul>
</section>

路线:

Prefix     Verb URI Pattern           Controller#Action
blog_index GET  /blog/index(.:format) blog#index
      root GET  /                     blog#index

【问题讨论】:

    标签: ruby api tumblr


    【解决方案1】:

    阅读the guide and it's chapter about forms 可能会很好。

    在您的控制器中,您将从 params 哈希中获取提交的值:

    # controller
    @blog = "#{params[:blogname]}.tumblr.com"
    

    在您的模板中,您将实现一个表单以将值提交给 rails 应用程序:

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

    以上代码来自我的头脑,完全未经测试。

    【讨论】:

    • 我设法拿到了表格。我将@blog 放在“索引”方法中,其余放在“显示”方法中。我不知道如何将它们链接在一起。顺便说一句,没有数据库。你有什么办法可以告诉我如何把它们放在一起吗?谢谢!
    • 如果我理解正确,您在 show 操作中有 form-tag 并希望提交它以在 index 操作中显示博客?这将要求您将正确的 url 添加到表单中。 path-helpers 应该让它变得简单,具体取决于你的控制器如何被称为form_tag method: :get, url: controller_name_path。请参阅rake routes 以找出正确的路径名称。
    • 实际上恰恰相反。我的索引中有一个 form_tag,并且想对 show 中的结果值做一些事情。我在上面编辑了我的问题,向您展示我现在的位置。
    • 在我看来问题出在路线上。当我在路由文件中添加“资源:博客名称”或其他内容时,所有 url 都需要一个 id,而我没有,因为我没有使用数据库。
    • 只需使用博客名称作为您的 id。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多