On Aug 12, 2014, at 3:16 AM, Serguei Cambour <[email protected]> wrote:

> If you isolated you engine like that (engine_app/lib/engine.rbĂ )
> 
> isolate_namespace YourEngineName
> 
> Routes inside an engine are isolated from the application by default. This is 
> done by the isolate_namespace call inside the Engine class. This essentially 
> means that the application and its engines can have identically named routes 
> and they will not clash.
> 
> Routes inside an engine are drawn on the Engine class within 
> config/routes.rb, like this:
> 
> 
> YourEngineName::Engine.routes.draw do
> 
>   resources :posts
> 
> end
> 
> 
> 
> For instance, the following example would go to the application's posts_path 
> if that template was rendered from the application, or the engine's 
> posts_path if it was rendered from the engine:
> 
> 
> 
> <%= link_to "Blog posts", posts_path %>
> 
> To make this route always use the engine's posts_path routing helper method, 
> we must call the method on the routing proxy method that shares the same name 
> as the engine.
> 
> 
> <%= link_to "Blog posts", blorgh.posts_path %>
> 
> If you wish to reference the application inside the engine in a similar way, 
> use the main_app helper:
> 
> 
> <%= link_to "Home", main_app.root_path %>
> 
> 
> 
> 
> Hope this helps
> 
> 
That is the interesting thing. That is what I was using in my test before 
defining a method that had Rails.application.class.routes.url_helpers in it. 
The main_app helper wasn't available to me out of the box. I thought it was 
very strange since I expected things to work just as you explained it but for 
me that just wasn't the case.

I am glad you explained this the way you did. I wasn't aware that the templates 
would infer the route based on what route called the template.

> On 12 August 2014 05:02, vell <[email protected]> wrote:
> 
> On Aug 7, 2014, at 3:30 AM, Javix <[email protected]> wrote:
> 
>> 
>> 
>> On Tuesday, August 5, 2014 3:49:20 PM UTC+2, Vell wrote:
>> Hello all,
>> 
>> I am creating a rails 4.1 engine and am having trouble figuring out how to 
>> test the controllers redirect action back to the main_app.root_path. I have 
>> been struggling with this for a while and am hoping I can get a little 
>> guidance on this.
>> 
>> Basically if I have a create action such as:
>> 
>> def create
>>   @user = User.new(user_params)
>>   if @user.save
>>     flash[:success] = "User Saved!"
>>     redirect_to main_app.root_path
>>   else
>>     flash[:error] = "User Not Saved! Check Errors"
>>     render :new
>>   end
>> end
>> 
>> I get an error that says main_app is nil. I expect this since my spec 
>> directory is in the root of my engine. How would I go about testing creating 
>> a controller test where I would be able to test redirecting back to routes 
>> in the main_app? This should hopefully help me with the next step of 
>> integration testing as well.
>> 
>> Any advice is greatly appreciated.
>> 
>> Thanks
>> 
>> To test a redirect you should so smth like that(to be adapted to your 
>> fixtures, or FactiryGirl or whatever you use):
>> 
>> it "redirects to the home page upon save" do
>>   post :create, contact: FactoryGirl.attributes_for(:contact)
>>   expect(response).to redirect_to root_url #or some other url
>> end
>> 
>> 
>> Sure, in your code, main_app is an undefined variable, that's why you're 
>> getting Nil.
>> 
> 
> I was wondring how to make it so that main_app was not nil. The error that 
> was given to me by rspec made it clear that main_app was nil. I figured there 
> had to be a way to access the main_app's routes that I just wasn't able to 
> figure out.
> 
> This answer was given to me yesterday (since I was not able to figure this 
> out myself). For those that may be interested in how this issue was solved, I 
> created a macro with the following in it:
> 
> def main_app
>   Rails.application.class.routes.url_helpers
> end
> 
> and that allows me to get to the main_app routes for testing redirects and so 
> on.
> 
> 
>> -- 
>> You received this message because you are subscribed to the Google Groups 
>> "rspec" 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/rspec/1d9fa996-1f2b-4a0e-883f-7c7a596c6f46%40googlegroups.com.
>> For more options, visit https://groups.google.com/d/optout.
> 
> 
> -- 
> You received this message because you are subscribed to a topic in the Google 
> Groups "rspec" group.
> To unsubscribe from this topic, visit 
> https://groups.google.com/d/topic/rspec/yvNa2nWwR-s/unsubscribe.
> To unsubscribe from this group and all its topics, 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/rspec/3E5EE1C7-8D1B-4624-A1E0-A99D9F55EF17%40gmail.com.
> 
> For more options, visit https://groups.google.com/d/optout.
> 
> 
> -- 
> You received this message because you are subscribed to the Google Groups 
> "rspec" 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/rspec/CAJGQ%3DvZYW4OncHFjXBjzckHqGNwZP6fCbjEPhyKbR%3DyzaZwHDA%40mail.gmail.com.
> For more options, visit https://groups.google.com/d/optout.

-- 
You received this message because you are subscribed to the Google Groups 
"rspec" 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/rspec/B09E5A00-82AA-4957-9D0E-4FE4AA0E5089%40gmail.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to