0

In my Business model, when I try to create a business and an error occurs, I noticed after the POST it gets rid of my shared/page_header partial:

def page_title
  "#{@page_title}"
end

<% unless @page_title.blank? %>
<div class="row">
   <div class="page-header span12">
      <h1><%= @page_title %></h1>
   </div>
</div>
<% end %>

def new
  @business = Business.new
  @page_title = "Add Business"
end

def create
  @business = Business.new(params[:business])
  if @business.save
    redirect_to :back, :notice => "This Business was successfully added."
  else
    render :new, :notice => "It seems there was an error. Please try again."
  end
end

Now I notice I start on /businesses/new but after the POST it goes to /businesses. Someone told me this was normal, but I never seen this type of behavior until now. If it helps, the new and create actions are the only ones in my Business resource. What can I do to get this working?

1 Answer 1

2

Just add @page_title = "Add Business" to your create method. "render :new" renders the new template but not executes the new method.

Using render with :action is a frequent source of confusion for Rails newcomers. The specified action is used to determine which view to render, but Rails does not run any of the code for that action in the controller. Any instance variables that you require in the view must be set up in the current action before calling render. from http://guides.rubyonrails.org/layouts_and_rendering.html#using-render

Sign up to request clarification or add additional context in comments.

1 Comment

Woah, Simple enough. I didn't know actions like create could be used in this way. I'll keep this in mind. Thank you.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.