How to pass parameter in rendering 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 need to pass id to a new method. Here, if the data is stored correctly, it goes to the index method. But if not, then it should go to the new one, but with the id parameter. How can I pass parameters using the render action? Here I want to do, but param id is not passed to the new method. I highlighted this part by mistake. If i do

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

Then it does not report errors ...

I need to pass the 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 get error form prints to a new one, but here I have to send the id, which is in @emppede.ad . How can i do this? Since id must be passed to enter a new method

 redirect_to :action => :new, :id => @id 
+6
source share
2 answers

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

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

Your published code is a little cryptic, but the rendering will not introduce the new method, but only display new.html.erb in the current context. Therefore, if you declared @id in your create action and visualized a new one, you will get it.

When redirecting to: an action, you need to pass @id as a parameter.

+7
source

Render will look for "new.html", it will not go into the new method.

+7
source

All Articles