Rails named route and parameters - ruby-on-rails

I want to send to my controller some data, and then in variables:
params[:oem_number]
params[:id]
and see there some data, but how can i send to method data? How to write this route?
Now i have such route:
match '/articles/by_oem/:id&:oem_number' => 'articles#articles_by_oem', :as => :articles_by_oem
And try to create link:
= link_to "аналоги", :articles_by_oem(:id => no.article_nr, :oem_number => no.oem)
But i get SyntaxError errors...
So how to solve my problem? Please don't send me to rails doc's...
Just how to create link, that will send this two params, and also before edit route...

first, you don't need that list of parameters on a route.
You can leave just this in your routers.rb:
/articles/by_oem/:id
And, then, what are you trying to do here?
:articles_by_oem(:id => no.article_nr, :oem_number => no.oem)
:articles_by_oem is a symbol, not a function. Use articles_by_oem_path method instead:
= link_to "аналоги", articles_by_oem_path(:id => no.article_nr, :oem_number => no.oem)

= link_to "name", articles_by_oem_path(no.article_nr, no.oem)

Related

Rails Routing having a URL in the query string

So I need to hit a url like mydomain.com/proxies/www.msn.com in order to fulfill some ajax requests for an API
In my route I have
get "/proxies/:url" => "proxies#get"
and I in the controller I have
url_contents = Net::HTTP.get(URI.parse(params[:url]))
which if i put /proxies/www i get
undefined method `request_uri' for #<URI::Generic:0x3f89508 URL:www>
if i put /proxies/www.msn.com
I get
No route matches [GET] "/proxies/www.msn.com"
You have two separate problems here:
You're trying to treat a URL without a scheme as an HTTP URL.
Your /proxies route won't match :urls with dots and slashes the way you're expecting it to.
For the first one, you'll have to add the schema manually:
url = 'http://' + params[:url]
content = Net::HTTP.get(URI.parse(url))
For the second one, you can use a splat-route to deal with embedded slashes and :format => false to keep Rails from trying to treat .com, for example, as a format:
get '/proxies/*url' => 'proxies#get', :format => false
If you use :url, Rails will see embedded slashes as component separators and that's probably not what you want. Without the the :format => false, Rails will try to interpret .X (for any X) as a format (just like .html, .json, ...) rather than as part of the URL.
This is applicable for rails-2
The problem is with the dot(.) I guess. Ive tried something like below and it worked.
#routes.rb
map.get_proxy 'proxies/:url', :controller => "Proxies", :action => "get", :requirements => { :url => /.*/ }
#view file
#Here dot(.) are needed to replace with its encoded string %2E
<%= link_to 'Get The Site', get_proxy_url('www.msncom') %>
#Proxies Controller
#After receiving the url the %2E need to conovert to dot(.) again.
url_contents = Net::HTTP.get(URI.parse('http://'+params[:url]))
modified as stated by #mu.
match '/proxies/:url' => 'proxies#get', :as => :proxies_get

ActionMailer doesn't recognise route

I have a route called "settings_redirect", which I've defined as follows:
routes.rb
match "/settings/redirect" => "settings#redirect", :via => "get"
I want to link to this route in an email template:
mymail.html.erb
<%= link_to "Manage Settings", settings_redirect_url %>
Yet, when I get ActionMailer to send the email, I get the error
{undefined local variable or method `settings_redirect_url' for #<#:0x007ffa1153de38>
The same link works completely fine in any regular view, just not when I try to send it in an email. All other links in the same template don't cause any trouble either.
Any ideas as to what could cause the error?
You can use this form:
get "settings/redirect" => "settings#redirect", :as => :settings_redirect
match "/settings_redirect" => "settings#redirect", :via => "get"
Check out this documentation, http://guides.rubyonrails.org/action_mailer_basics.html#generating-urls-in-action-mailer-views

Ruby On Rails REST: Customize URL pattern for POST request

I am new to Ruby On Rails and need some help implementing REST protocol.
Whenever you do a POST on REST you get a URL back e.g. http://my-site.com/id/1
I need a customized response in URL format which I have given in example above.
Lets say I am doing a post on parameter <main-id>123</main-id>
The customized response I am looking for is http://my-site.com/123/id/1
What I want to implement is, whatever parameter ID I passed during a post I want that as a part of the response URL output.
Thanks for any help in advance.
You can specify any URL in your controller, e.g.:
def create
... # create record here
redirect_to "/#{params[:main_id]}/id/#{#record.id}"
end
Of course, you'll probably want to use the url helper based on your defined route:
def create
... # create record here
redirect_to my_oddball_path(#record, :main_id => 123)
end
Provided you are using Rails 3, you could just add this route with the proper controller/action
match ':main_id/id/:id', :controller => 'foo', :action => "bar", :via => :post, :as => main_id
Then you can just with the helper main_id_path(main_id, #record)

how can I add a new action to a controller?

I used the following in routes to add a new action to my Email controller:
map.resources :emails, :member => { :newfwd => :put}
The expected result was that newfwd_email_path(:id => 1) would generate the following urL: emails/1/newfwd
It does. But I get an error, it treats '1' as an action and 'newfwd' as an id. I want '1' to be interpreted as the id for emails, upon which the newfwd action acts.
I'm not sure what I'm doing wrong. (Note: I am using Rails 2.3.8)
Try
link_to newfwd_email_path(1), :method => :put
:id => 1 is as good as 1 ;)
you do not need to pass a hash to the newfwd_email_path method. Try
newfwd_email_path(1)
EDIT: you also need to use :method => :put to ensure that the PUT verb is used when request is received on the server and routing comes into effect.

Generating a form that passes multiple parameters

I have the following in my routes.rb
map.diff 'posts/:id/diff/:from/:to', :controller => "posts",
:action => "diff", :conditions => { :method => :get }
And I have the following in my view file.
- form_tag(diff_path(), :method => :get) do
= text_field_tag(:from, "")
= text_field_tag(:to, "")
= hidden_field_tag(:id, #post.id)
= submit_tag("Submit")
I would like to generate a form that submits something like "http://example.com/posts/3/diff/13/18", but it fails. How can I make such a form?
I need to pass parameters for diff_path(), but I don't know how to do that. I don't even know if this is possible with form_tag.
The error message:
diff_url failed to generate from {:action=>"diff", :controller=>"posts"} - you may have ambiguous routes, or you may need to supply additional parameters for this route. content_url has the following required parameters: ["posts", :id, "diff", :from, :to] - are they all satisfied?
To my knowledge, what you're trying to accomplish can't be done with just an HTML form. The reason being that the form will only know how to submit traditionally via GET and POST. It has no knowledge of the structure of the URL.
You get that error message because the :id, :from and :to parameters are required to form the both you want, so when you call diff_path() it freaks out.
Personally, I would advise you not to use the URL structure you're planning on - however I'm not totally clear on what this page is going to display. Regardless, if the :from and :to parameters are algorithmic input and not resource identifiers, I would avoid this structure.
That said, if you do want to implement this, you would either have to implement a redirect from rails or javascript.
Rails method
#some_controller.rb
redirect_to diff_path(:from => params[:from], :to => params[:to])
Javascript (jQuery) method
$(function() {
$("form#your_form_id_here").submit(function() {
window.location = "posts/" + this.id + "/diff/" + this.from + "/" + this.to;
return false;
});
});
I don't think that will work by specifying the url with that format in the form. Rails tries to create the URL to submit to when you render the page, not when you submit the form. So it is not seeing those parameters when you render the page. I would recommend creating another route
map.diff_form 'posts/:id/diff', :controller => :post, :action => :diff, :conditions => {:method => :post}
You could use the two routes side by side if you need to keep the current url format.
Why are you trying to do this in the first place? I really can't think of a good reason why this would be necessary.
Your "from" and "to" variables should probably just be normal URL parameters - i.e. /posts/:id/diff?from=X&to=Y, so that you can then retrieve them in your controller with params[:from] and params[:to]
There may be a way to make Rails work this way, but you're going to have issues with it, since Rails is emphatically not meant to work this way.
I think you can use like this
diff_path(#id,#from, #to)
where #id, #from, #to are instance variables. If dont, you can specify a hash also like
diff_path(:id=>#id,:from=>#from, :to=>#to)

Resources