Note that there are some explanatory texts on larger screens.

plurals
  1. POHow do I redirect signed-in Users attempt to access User.new and User.create in Ruby?
    primarykey
    data
    text
    <p>I'm up to the <a href="http://ruby.railstutorial.org/book/ruby-on-rails-tutorial#sec-updating_deleting_exercises" rel="nofollow">Chapter 9 Exercises</a> in Hartl's Rails Tutorial. I've been trying for ages but can't crack question 6:</p> <blockquote> <p>Signed-in users have no reason to access the <code>new</code> and <code>create</code> actions in the <code>Users</code> controller. Arrange for such users to be redirected to the root URL if they do try to hit those pages.</p> </blockquote> <p>First, where should I put these tests? At the moment I'm trying in <code>user_pages_spec.rb</code> but I'm unsure if that's right. And then where should I put the logic itself?</p> <p>Second, is this what my test should look like:</p> <pre><code> describe "after signing-in" do before { sign_in(user) } describe "creating a new user" do before { put new_user_path } specify { response.should redirect_to(root_url) } end end </code></pre> <p>At the moment when I do the above, RSpec tells me </p> <pre> Expected response to be a redirect to but was a redirect to http://www.example.com/signin. </pre> <p>But when I test it in my browser, visiting <code>http://localhost:3000/users/new</code> with a signed-in user, there is no redirect at all (which makes more sense to me because I haven't coded one).</p> <p>I've even downloaded Hartl's code from github but I can't see where / if he has included this functionality.</p> <p><strong>Edit:</strong> I've got both</p> <pre><code> describe "creating a new user" do before { visit new_user_path } it { should_not have_selector 'title', text: full_title('Sign Up') } end </code></pre> <p>And</p> <pre><code> describe "creating a new user" do before { get new_user_path } # note the use of GET here not PUT specify { response.should redirect_to(root_url) } end </code></pre> <p>to work properly. Thanks to @Peter de Ridder for help</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload