Ruby, How to Add a Param to an Url That You Don't Know If It Has Any Other Param Already

Ruby, How to add a param to an URL that you don't know if it has any other param already

require 'uri'

uri = URI("http://url.com?p1=v1&p2=2")
ar = URI.decode_www_form(uri.query) << ["param","value"]
uri.query = URI.encode_www_form(ar)
p uri #=> #<URI::HTTP:0xa0c44c8 URL:http://url.com?p1=v1&p2=2¶m=value>

uri = URI("http://url.com")
uri.query = "param=value" if uri.query.nil?
p uri #=> #<URI::HTTP:0xa0eaee8 URL:http://url.com?param=value>

EDIT:(by fguillen, to merge all the good propositions and also to make it compatible with his question test suite.)

require 'uri'

def add_param(url, param_name, param_value)
uri = URI(url)
params = URI.decode_www_form(uri.query || "") << [param_name, param_value]
uri.query = URI.encode_www_form(params)
uri.to_s
end

Appending empty query string param to the URL in Rails

Edited due to the changed question on the comment below:

Ah I see.

You could easily use the same URL for all devices and just separate by checking the request environment for HTTP_USER_AGENT.

But if this distinction is not enough (I think it is enough for more than 80% of all cases) you could do the following:

in config/routes.rb:

map.with_options :prefix => '/m', :format => 'mobile' do |mobile|
mobile.resources :apples
mobile.resource :user
mobile.connect #...
end

# generates
# /m/apples
# /m/apples/new
# ...
# /m/user
# ...

I have not tested this, but maybe you will have to add a MIME type mapping for :format => 'mobile'.

How to add parameters to current URL in rails

If you only need one cgi param and want to stay on the same page, this is very simple to achieve:

<%= link_to "lowest prices", :sort => "price_lowest" %>

However, if you have more than one, you need some logic to keep old ones. It'd probably be best extracted to a helper, but essentially you could do something like this to keep the other params..

<%= link_to "lowest prices", :sort => "price_lowest", :other_param => params[:other] %>

Named routes will only really help you here if you need to go to another page.

Rails: Specifing params without value to link_to

If you don't add a value to the parameters you will not be respecting the W3C standard, which mandates that the params section has the form field=value.

I recommend that you add a new :most_popular action to your articles controller instead.

On your routes.rb:

map.resources :articles, :collection => {:most_popular=>:get}

On your controller:

class ArticlesController < ApplicationController
...
def most_popular
@articles = ...
end

On your views:

link_to most_popular_articles_path() # /articles/most_popular

This will be HTML-compliant, your urls will look practically the same (changing one ? by one /) and your controller will be simplified (you will have the most_popular action separated from the index).

Regards!

Update (2017): It appears that the W3C standard doesn't mandate the field=value syntax (or doesn't mandate it any more). However some servers are documented to "choke" on queries not complying with this syntax. See Is a url query parameter valid if it has no value? for details.

How to pass parameters with link_to tag?

You're already doing it correctly in the controller, you just need to include the :id in the link_to url options:

<%= link_to(:controller => "posts", :action => "up vote", :id => @post) do %>
<span>Like</span>
<span>(<%= @post.get_upvotes.size %>)</span>
<% end %>

Note: you don't need to call id on the post, rails will automatically figure this out for you.

A better solution, would be to use resource routes for you Post object. then add upvote/downvote actions:

# in config/routes.rb
resources :posts do
member do
match "upvote", :as => :upvote, :via => [:get, :put]
end
end

Rails will automatically create url helpers that you can use in your views

# in show html.erb
<%= link_to(upvote_post_url(@post)) do %>
<span>Like</span>
<span>(<%= @post.get_upvotes.size %>)</span>
<% end %>

the advantage to this is simpler code, and the ability change your routes with having to update template code.

Rails has already done all the heavy lifting of how to make and link to urls, to be faster at plumbing rails views and controllers together, you should definitely read the official guides http://guides.rubyonrails.org/routing.html, then may look daunting at first, but they will quickly become second knowledge.



Related Topics



Leave a reply



Submit