27.02.2013 Views

Rails%203%20In%20Action

Rails%203%20In%20Action

Rails%203%20In%20Action

SHOW MORE
SHOW LESS

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

Writing your first engine feature<br />

<br />

"forem/posts/form", :locals =><br />

➥{ :post => post} %><br />

<br />

<br />

<br />

Alright now, with the action, the view, and the form partial defined, you’re almost<br />

there. In this partial though, you reference another partial called forem/posts/<br />

form B, passing through the local variable of the post form builder object as f to it<br />

by using the :locals option. You’re using the long form here, as Rails cannot infer<br />

the name of it any other way.<br />

This new partial will provide the text field that you’ll use for posts. You’re placing it<br />

into a partial because you may use it later on if you ever create a form for creating new<br />

posts, like a reply feature for a topic.<br />

Let’s create the file for this partial now at app/views/forem/posts/_form.html.erb<br />

and put these lines in it:<br />

<br />

<br />

<br />

<br />

Even though this is an extremely short partial, it’s good to separate it out so that it can<br />

be shared across the topic and posts forms, and also in case you ever decide to add any<br />

additional information to a post.<br />

Your test should get a little further when you run bin/rspec spec/integration/<br />

topics_spec.rb again:<br />

AbstractController::ActionNotFound:<br />

The action 'create' could not be found for Forem::TopicsController<br />

Now you need to define the create action, the second to last action along this chain,<br />

with the show action being the last.<br />

17.5.6 The create action<br />

The create action will take the parameters passed from the form provided by the new<br />

action and create a new Topic object with a nested Post object. This action should set<br />

the flash[:notice] variable to inform the user that the topic could be created and<br />

then redirect to the show action.<br />

This action needs to be defined using the code shown in the following listing, placing<br />

it under the new action.<br />

Listing 17.7 app/controllers/forem/topics_controller.rb<br />

def create<br />

@topic = Forem::Topic.create(params[:topic])<br />

flash[:notice] = "Topic has been created!"<br />

redirect_to @topic<br />

end<br />

B<br />

Reference<br />

partial<br />

489

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!