On Friday, February 10, 2017 at 2:58:48 PM UTC+1, Colin Law wrote:
>
> On 10 February 2017 at 13:46, 'krfg' via Ruby on Rails: Talk 
> <[email protected] <javascript:>> wrote: 
> >  ... 
> > The home page is controlled by static_pages_controller.rb 
> > However the part of the home page concerning microposts creation and 
> > deletion is controlled by the microposts controller. 
> > Below is an extract of microposts_controller.rb 
> > 
> > class MicropostsController < ApplicationController 
> >   before_action :logged_in_user, only: [:create, :destroy] 
> >   before_action :correct_user,   only: :destroy 
> > 
> >   def create 
> >     @micropost = current_user.microposts.build(micropost_params) 
> >     if @micropost.save 
> >       flash[:success] = "Micropost created!" 
> >       redirect_to root_url 
> >     else 
> >       @feed_items = [] 
> >       render 'static_pages/home' 
>
> The render line does not invoke the static_pages controller, it just 
> invokes the view files for static_pages/home, so it will not call the 
> static_pages controller to setup your variables. I don't know how your 
> debug got to that code, I suspect it was from a different part of the 
> test.  Perhaps you meant redirect_to which will invoke the controller 
> to perform the action.  You may find this useful - 
> http://tosbourn.com/difference-between-redirect-render-rails/ 
>
> Colin 
>


Then the problem was just rendering the home page after invalid submission 
instead of redirecting to the home page.
The original code was render 'static_pages/home', you can check it here 
<https://www.railstutorial.org/book/user_microposts#code-microposts_create_action_with_feed>
.
This original code would show a flash error message that the test would 
catch with:

assert_select 'div#error_explanation'

If I remove or comment this line in the test, the test would pass as long 
as I use a redirect in the create action of the microposts controller.
No flash errors are showed after redirection, though 

-- 
You received this message because you are subscribed to the Google Groups "Ruby 
on Rails: Talk" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
To post to this group, send email to [email protected].
To view this discussion on the web visit 
https://groups.google.com/d/msgid/rubyonrails-talk/d07f7ad3-5560-47a1-9eff-3527ea43f489%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to