ruby on rails - URL parameter not preserved after validation fails and 'new' action is rendered -


when call new function variables load correctly. params[:code] url param defined in routes. however, when validation fails on create , new rendered, @m variable not loaded (this causes nomethoderror when attribute of @m called in 'new' template). so, :code parameter not obtained after rendering new. can :code parameter preserved after validation fails?

     class affiliatescontroller < applicationcontroller          def new           @m = merchant.find_by_code(params[:code])           @affiliate = affiliate.new         end           def create           = affiliate.new(params[:affiliate])           if a.save              redirect_to 'http://' + params[:ref]           else              render 'new'           end        end     end 

another way preserve params[:code], aside using session, add hidden field in form.

<%= form_for @affiliate |f| %>   <%= hidden_field_tag :code, @m.code %> 

then change create action to

def create   @affiliate = affiliate.new(params[:affiliate])    if @affiliate.save     redirect_to 'http://' + params[:ref]   else     @m = merchant.find_by_code(params[:code])     render :new   end end 

Comments

Popular posts from this blog

Why does Ruby on Rails generate add a blank line to the end of a file? -

keyboard - Smiles and long press feature in Android -

node.js - Bad Request - node js ajax post -