从标题中设置textfield上的值

时间:2012-08-03 07:40:12

标签: android ruby-on-rails ruby

在我的应用程序中,我希望动态设置来自标头的文本字段值。这是代码

在android项目中我设置了标题纬度和经度,我正在尝试在rails视图上获取它。

public static String getAddress(final String dir_atoken, final String data_atoken, final float lat, final float lng) throws Exception {

        final HttpGet get = new HttpGet("http://192.168.1.4:3000/address/lookup");

        get.setHeader(LAT,String.valueOf(lat));
        get.setHeader(LNG,String.valueOf(lng));


        return getResponse(get, 200, true);
    }

这是我的lookup_address控制器

def lookup_address
    @lat = request.headers['Lat']
    @lng = request.headers['Lng']
  end

这是我的lookup_address.html.erb

<% form_for @location do |f| %>
    <%=  f.error_messages %>

    <p>
        <%= f.label :latitude %><br />
        <%= f.text_field :latitude, :value => request.headers['Lat'] %>
    </p>
    <p>
        <%= f.label :longitude %><br />
        <%= f.text_field :longitude, :value => request.headers['Lng'] %>
    </p>
    <p>
        <%= f.submit 'Create' %>
    </p>
<% end %>

当我设置:value =>request.headers['Lat']时,它总是返回null。

我不应该为了获得空值而做什么?

1 个答案:

答案 0 :(得分:1)

构建@location的位置?猜猜在控制器中设置@location.latitude@location.longitude会更正确。

def lookup_address
  @location = Location.new #Not sure how you build the @location
  @location.latitude = request.headers['Lat']
  @location.longitude = request.headers['Lng']
end

lookup_address.html.erb

<% form_for @location do |f| %>
    <%=  f.error_messages %>

    <p>
        <%= f.label :latitude %><br />
        <%= f.text_field :latitude %>
    </p>
    <p>
        <%= f.label :longitude %><br />
        <%= f.text_field :longitude %>
    </p>
    <p>
        <%= f.submit 'Create' %>
    </p>
<% end %>