Friday 15 January 2010

ruby on rails - Why does form_for :symbol return to the same page? -



ruby on rails - Why does form_for :symbol return to the same page? -

i have form used both new , edit templates model called product. in controller, create , update actions redirects @product , renders "show" upon success. works fine when form declared `form_for @product |f|, if utilize symbol instead of instance variable, tries post same page. i.e. if on page products/4/edit , pressed submit on form, give me routing error trying post products/4/edit, has route in resources.

now if set in url alternative "url: products_path" redirects correctly products/4 if used form_for @product. mean using symbol form_for isn't going controller actions? why trying post itself?

here's form

<%= form_for @product |f| %> <-- changing :product gives routing error <% if @product.errors.any? %> <div id="error_explanation"> <h2><%= pluralize(@product.errors.count, "error") %> prohibited product beingness saved:</h2> <ul> <% @product.errors.full_messages.each |message| %> <li><%= message %></li> <% end %> </ul> </div> <% end %> <div class="field"> <%= f.label :title %><br> <%= f.text_field :title %> </div> <div class="field"> <%= f.label :description %><br> <%= f.text_area :description, rows: 6 %> </div> <div class="actions"> <%= f.submit %> </div> <% end %>

and here's controller actions update , create:

def create @product = product.new(product_params) respond_to |format| if @product.save format.html { redirect_to @product, notice: 'product created.' } format.json { render :show, status: :created, location: @product } else format.html { render :new } format.json { render json: @product.errors, status: :unprocessable_entity } end end end def update respond_to |format| if @product.update(product_params) format.html { redirect_to @product, notice: 'product updated.' } format.json { render :show, status: :ok, location: @product } else format.html { render :edit } format.json { render json: @product.errors, status: :unprocessable_entity } end end end

when utilize symbol in form tells form_builder kind of object creating form for. if there happens instance variable set illustration @product form smart plenty pick values when rendering input variables. in order proper url path determined via rails resourceful routing need pass resource.

@product not same :product. instance variable reflects resource in scheme resourceful route can generated it. not case when using symbol why need set url param explicitly.

when using :product url of form action set url of current page why submit going edit action.

ruby-on-rails

No comments:

Post a Comment