Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to pass parameter in render action in rails

def create
    @emppede = Emppede.new(params[:emppede])

    respond_to do |format|
      if @emppede.save
        format.html { redirect_to :action => :index, :id => @emppede.ad }
        format.json { render json: @emppede, status: :created, location: @emppede }
      else

        format.html { render action: "new", :id => @emppede.ad } *(....error)*
        format.json { render json: @emppede.errors, status: :unprocessable_entity }
      end
    end
  end

I have to pass id in new method. Here if the data is save properly then it goes to index method. But if not then it should go to new but with the params id. How could i pass the params through render action? Here i want to do but param id is not passed to new method. I highlighted that part by error. If i do

 format.html { redirect_to :action => :new, :id => @emppede.ad }

Then it do not give errors message...

I have to pass user id to the new method so that i can pass it through the form and save.

<div id="employm" style="display:none">


    <%= f.text_field :ad, :value=> @id%>

            </div>

But when the form get error it render to new but here i have to send the id which is in @emppede.ad. How can i do this? Since in order to enter in new method there should be id passed

redirect_to :action => :new, :id => @id
like image 255
regmiprem Avatar asked Nov 19 '12 08:11

regmiprem


2 Answers

All instance variables you define in your controller action are present in the view. So if you define @id = 11 in your controller you can access it in the view using <%= @id %>.

If you want to do this over a redirect you can simply can access the parameters inside the view (or use them first in the controller and then use the instance method above.

Your posted code is a bit cryptic, but render will not enter the new method but only render the new.html.erb in the current context. So if you declared @id in your create action and render new you'll have it present.

When redirecting to :action you'll have to pass the @id as a parameter.

like image 151
Tigraine Avatar answered Nov 12 '22 17:11

Tigraine


Render will look for "new.html", it won't enter into the new method.

like image 7
ktcoder Avatar answered Nov 12 '22 18:11

ktcoder