Ruby on Rails redirect_to missing parameter - ruby-on-rails

I've been doing some the last part of my assingment in Ruby on Rails and I'm stuck on redirect_to for an hour.
I want to achieve this kind of structure on the URI.
http://localhost:3000/movies?ratings[G]&ratings[R]
My current code on this:
redirect_to :controller => 'movies', :ratings => session[:ratings_uri]
#session[:ratings_uri] = "ratings[G]&ratings[R]"
And this is what I got after the redirection.
http://localhost:3000/movies?ratings=%26ratings[G]%26ratings[R]
My question is, Is there any way to make my desired URL? Do I need to delete some paramaters on my code.

Related

Issue with Redirect not functioning in Rail 2

I have this redirection in the middle of my controller so if something isn't there, it will redirect you to a new area of the site if needed. Here is the problem. It is just ignoring the redirect in the code. It looks like this.
if conditions
redirect_to "/new/address"
end
I can't even figure out why it won't redirect, but I know it makes it into the conditional and literally just ignores the redirect. What am I missing here!?
I am using Rails 2 and Ruby 1.8
What are you seeing? Is it raising an error? Is there an error message in the console when running "script/server"? For this to work, there should be something to handle the path "new", typically a controller called "NewsController" (plural form) or some rule in the routes.rb file.
If you are seeking to create a new address, then you may be looking for something like
if conditions
redirect_to new_address_path
end
Did you try to use rails paths instead of a string path there?
Try something like
redirect_to :action => 'new'
if the method is inside this controller, or something like
redirect_to :controller => 'adress', :action => 'new'
To see if the result changes.
Don't forget that a redirect_to or render call in a Rails action do not terminate the method. Method execution continues to the end before the redirect/render is performed. So if you're looking to terminate execution of the method at that point add a return statement. The usual pattern is:
redirect_to(<my route>) and return

Rails '3.2.10' .js.erb template no route matches

In my public controller I have:
def index
#super = "test"
respond_to do |format|
format.html
format.js
end
end
I have created a index.js.erb for the index action in the views/public folder.
But when I visit http://localhost:3000/.js
I get No route matches [GET] "/.js"
This have worked before on older version of rails...
This, the very exact, question was asked here a year ago. The asker came to a conclusion, and I quote
Okay, so, I've decided that this was probably something that the Rails team did intentionally to discourage URLs like "/.atom" (because really, does that look like something that should happen?), so I'm now using a second route (get "latest", :action => :index) for the format URLs (so, like, "/latest.atom") instead.
From the question, it seems it's something happened between 3.0 and 3.1.
As a temporary solution, I still need to go deep, remove root from routes.rb and add this get "/(.:format)" instead, to: "public#index". This should work.
Edit
Yup, it could be related to this commit.
The API specifically says that root uses match to add / to the list of routes. root ends with a slash, so ... connect the dots. The commit says no format for routes end with /.
I did remove the addition (|| path.end_with?('/')) from actionpack/lib/action_dispatch/routing/mapper.rb and root can now have formats.
To make sure you don't break all the calls to the root_path helper, replace the route:
root :to => 'public#index'
With:
get '/(.:format)' => 'public#index', :as => :root

redirection and routes in Rails

I'm currently reading Beginning Rails 3. I have a question about redirection. The book states that "redirect_to can also take an object as a parameter" vs a path. So the example they give is that
redirect_to(#article)
is a shortcut equivalent to
redirect_to(article_path(:id => #article))
I'm not sure I understand this. What exactly is the line :id => #article saying?
thanks,
mike
redirect_to(#article) is a shortcut for
redirect_to(article_path(#article)). Rails can do this because it extracts the class name from the #article variable, something like send("#{#article.class.name.downcase}_path").
redirect_to(article_path(#article)) is a shortcut for redirect_to(article_path(:id => #article)), which is a shortcut for redirect_to(article_path(:id => #article.to_param)). Basically Rails says, #article is an ActiveRecord object, I'll ask it for it's magical to_param value. By default, to_param just returns the article's ID, but you can overwrite this to return like a slug or nicename, like 38-hello-world.
So redirect_to(#article) is really saying, OK, I have an Article, so I need the path to the articles, and I need the resource identifier, or to_param, of the article.
Then it constructs the URL!
:id => #article is a shortcut for :id => #article.to_param, which just specifies which article you're looking to view.
First of all, execute rake routes in your console. It will display the list of your app's routes.
There will be a route called article.
article_path is simply the link for the route article.
And by doing (:id => #article), you're passing it an id parameter that has the #article value.

Rails: redirect with params

What's the best way to pass some params along with a redirect?
I saw examples that said if you just add them to your redirect hash they would pass along with the request, but that doesn't seem to work anymore in Rails 3.
In my example I have an 'edit multiple' page that lets a user change the category on multiple items at once. Because they're browsing so many items this form is paginated.
If a user is on items page 3, makes some changes and presses sumbit, then the controller action receives a post request with the ids of the records that were changed, makes the changes, and redirects to the edit_many_items_path.
So, that redirect looks like this:
redirect_to edit_multiple_items_path, :notice => 'items updated'
... but what I'd like it to do is something like:
redirect_to edit_multiple_items_path, :notice => 'items updated', :page => ##
The above code doesn't work, so does anyone have an example of what would?
Try this:
redirect_to(edit_multiple_items_path(:page =>2), :notice => 'items updated')

How do I write a route that is SEO friendly in rails?

I am changing my site over from Google App Engine to rails and I would like to keep my place in google search. Currently my site uses a URL /page?pid=microsoft-interview-questions to access the Microsoft subsection of interview questions. How would I create a route that can send this to '/tags/:id' where :id would be microsoft in this case?
In addition to josh's answer I'll put this here for formatting:
# your controller
def show
#subject = Subject.find my_stripped_id
private
def my_stripped_id
params[:id].sub(/-interview-questions/, '')
end
something like this should work (in routes.rb):
map.connect '/page?pid=:number',
:controller => 'tags', :action =>
'show'
see routes reference

Resources