如何在rails中的render动作中传递参数

时间:2023-02-08 13:19:59
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

我必须在新方法中传递id。如果数据被正确保存,那么它将转到索引方法。但如果不是那么它应该是新的,但与params id。我怎么能通过渲染动作传递参数?这里我想做,但param id没有传递给新方法。我突然强调了这一点。如果我做

 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.

我必须将用户ID传递给新方法,以便我可以通过表单传递并保存。

<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

但是当表单获得错误时它会呈现给new,但是我必须发送@ emppede.ad中的id。我怎样才能做到这一点?因为为了输入新方法,应该传递id

redirect_to :action => :new, :id => @id

2 个解决方案

#1


7  

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 %>.

您在控制器操作中定义的所有实例变量都存在于视图中。因此,如果在控制器中定义@id = 11,则可以使用<%= @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.

您发布的代码有点神秘,但渲染不会进入新方法,只会在当前上下文中呈现new.html.erb。因此,如果您在创建操作中声明@id并呈现新的,则会让它存在。

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

重定向到:action时,您必须将@id作为参数传递。

#2


7  

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

渲染将查找“new.html”,它不会进入新方法。

#1


7  

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 %>.

您在控制器操作中定义的所有实例变量都存在于视图中。因此,如果在控制器中定义@id = 11,则可以使用<%= @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.

您发布的代码有点神秘,但渲染不会进入新方法,只会在当前上下文中呈现new.html.erb。因此,如果您在创建操作中声明@id并呈现新的,则会让它存在。

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

重定向到:action时,您必须将@id作为参数传递。

#2


7  

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

渲染将查找“new.html”,它不会进入新方法。