Rails redirect_to shows incorrect URL in browser - ruby-on-rails-3

I have 2 controllers: Games and Players.
Games creates my game and the first player, and redirects to players 'edit' action.
User submits updates to the player, and the Players controller redirects back to games 'show' action.
The user flow works well (the player is updated and the user is shown the game show page), except the URL in the browser upon the final submit still shows 'www.server.com/players/:id'. If I hit refresh on the browser, it will go to the player's show action (as the URL suggests), instead of refreshing the games/show content that is currently on the screen.
What is the preferred approach for fixing this behavior?
Games Controller:
def create
#game = Game.new(params[:game])
if #game.save
redirect_to edit_player_path (#game.players.first)
else
render 'new'
end
end
Players Controller:
def update
#game = current_game
#player = #game.players.find(params[:id])
if #player.update_attributes(params[:player])
redirect_to #game
else
render 'edit'
end
end
ANSWERS to follow up Questions:
current_game is a helper method to retrieve a game (same design as Rails Tutorial http://ruby.railstutorial.org/chapters/sign-in-sign-out )
def current_game
#current_game ||= Game.find_by_remember_token(cookies[:remember_token])
end
routes.rb:
root to: 'static_pages#home'
get "static_pages/home"
resources :games
resources :players
resources :sessions, only: [:new, :create, :destroy]
UPDATE:
I think I have narrowed down the problem. It has to do with jquery_mobile_rails.
I was able to reproduce the behavior by creating a new project with 2 scaffolds and set up routing in the controllers as above. The URL is updated properly without JQM. When I added jquery_mobile_rails gem, and performed a POST, the URL no longer updates.
There is further discussion on the web about this (see links below), but so far I was not able to find a solution:
Rails jquery mobile Routing/Rendering Issue
http://forum.jquery.com/topic/restful-resources-rails-3-and-jquery-mobile

Related

Configure routes with post method in rails

I am creating a web service in rails application and I need to configure routes with respect to "books/CreateBookItem" with post method.
But since I will not have a view page for this to create a book item, for testing purpose I am typing the url in the browser as
localhost:3000/books/CreateBookItem
But now when I type the above url in browser it is taking as GET request.
So how do I configure the routes for the above.
Add this createbookitem action to books resources in routes.rb:
resources books
collection do
post :createbookitem
end
end
Don't want to have any view for this action then create createbookitem action with disabled render.
class BooksController < ApplicationController
def createbookitem
#do your stuffs
render nothing: true
end
end

What happens to a form from the time it's submitted in the view to before the data returns back to the controller?

This question has come up multiple times during interviews and I'm having trouble finding the answer on google. I think I'm using the wrong terminology when searching.
For statrters this is the MVC Architecture. Probably have seen it already, but basically as a user of the application. A good example of data being submitted in the view back to the controller is when a user attempts to create a new user. As first you may have a <%= link_to 'Create New User', new_user_path %> this will send a request back to the controller calling the new action in the user_controller upon this it will then render app/users/new.html.erb which will contain a form_for block that will render the partial which in most cases will be :render :partial => 'form'. From rendering the form and entering the relevant information when the user submits the form the form and the data along with the form is submitted and a POST request is made which would most probably be /users in this case. This is mapped to the create action in the UsersController as Rails uses the principle of convention over configuration.
def create
#title = 'Create a user'
#user = User.new(params[:user])
if #user.save
UserMailer.registration_confirmation(#user).deliver
redirect_to usermanagement_path
flash[:success] = 'Created successfully.'
else
#title = 'Create a user'
render 'new'
end
end
In the above example what happens is that when the new action is called and the form is submitted it will pass in the new user into the params, upon this the it will try and save the user. I added a mailer so that when a particular user is created they receive a confirmation email. If the form doe not submit it will continue to render the 'new' action.
Hope this clears things up

Redirecting to show User Profile rather than default home page when clicking to go "home"

This question stems from the example web app created with Michael Hartl's Rails tutorial, though it is not one of the exercises suggested by the author.
On many social sites, when clicking to go "home" (such as on the site logos), users are redirected to their profile rather than the default home page. I was wondering how I may be able to accomplish this as well in the tutorial app.
In the tutorial app, the "home" view is considered "static", and rendered by a static_page_controller that just shows:
def home
end
When I tried to add the following to it:
def home
if signed_in?
redirect_to #user
end
end
I get the error "cannot redirect to nil". I had thought #user was shared across all the controllers (since signed_in was... In the user_controller: #user = User.find(params[:id]) ). How would I modify the controller so that when it detects the user is signed in, the profile would be displayed rather than the home page?
Thanks for your help!
This signed_in? method do not use current_user instead of #user?
If so you can just change the line
redirect_to #user
to
redirect_to current_user

Rails 3 - Routing to a user profile

Greetings all, newbie to Rails here. I'm currently having issues routing /profile to the current user's profile.
My route is as follows:
match "profile" => "users#show"
However, this is hitting me with the "Couldn't find User without an ID" error. I know it has to do with my show method in the Users Controller. That code is simply:
def show
#user = User.find(params[:id])
end
Now, I could add another method in my Users controller with "#user = current_user" and it works fine. However, it seems a bit redundant and would also require a copy of the show view page. From what I've gathered with Rails, it's all about keeping things neat and tidy.
I would appreciate it if someone could point me in the right direction. Thank you.
RailsGuides states:
Because you might want to use the same controller for a singular route (/account) and a plural route (/accounts/45), singular resources map to plural controllers.
So I think you want to change your code to be the following
def show
#user = !params[:id].nil? ? User.find(params[:id]) : current_user
end

Rails app doesn't see my views

I've on a while on rails now and here's the problem I've been having on and on:
When I create a controller through:
"rails generate controller ControllerName ViewName"
I get everything working as I want but if for some reason I create the controller through:
"rails generate controller ControllerName"
and then just add ViewName.html.erb to the folder inside views that has the same name as my controller things would go wrong.
So the concrete case is me writing:
rails generate controller Subjects list show.
Which creates for me:
1.controllers>subjects_controller.rb
2.views>subjects>list.html.erb
3.views>subjects>show.html.erb
So this whole thing works fine.But as I already said if I need another view; let's say "new" I just add "new.html.erb" next to the other *.html.erb files and an action:
def new
end
to my subjects_controller.rb then it won't work.
The two previous views would keep working but any other "*html.erb" created outside the command line wouldn't.
Is there anywhere else where info about views is being stored?.
I'm a Windows 7 user (32 bit).Rails version=3.0.3. WebServer=WEBrick.
Text editor = E-TextEditor
This is most likely caused by your routes not being correctly configured. So it would be helpful to see the content of your routes.rb
In your case I think the best way to configure the routes is to use the resources mapping:
resources :subjects
This will by default create routing for the standard RESTful actions :index, :show, :edit, :update, :new, :create and :destroy.
For more detailed information about the routing, I would recommend Rails Routing from the Outside In