Ruby on Rails
Friday, February 10, 2017
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
<rubyonra...@googlegroups.com > 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.
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 rubyonrails-talk+unsubscribe@googlegroups.com.
To post to this group, send email to rubyonrails-talk@googlegroups.com.
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.
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment