Ruby on Rails在URL中传递参数 [英] Ruby on Rails Passing Parameter In URL

查看:254
本文介绍了Ruby on Rails在URL中传递参数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这可能是一个非常简单的修复程序,但是我还找不到答案。

This is probably a very simple fix but I've been unable to find an answer just yet.

我的应用程序有命令和任务。订单有很多任务。当用户在显示订单视图中单击新任务时,它将传递订单。

My application has orders and tasks. Orders have many tasks. When a user clicks new task in the show order view, it passes the order.id:

<%= link_to "New Task", new_task_path(:order_id=> @order.id) %>

网址显示:

/tasks/new?order_id=1

我只是不知道如何提取它并以我的形式使用它?我试过了:

I just don't know how to extract this and use it in my form? I have tried:

<%= f.text_field :order_id, :value => @order_id %>

但是它不起作用。

推荐答案

可以做到

<%= f.text_field :order_id, :value => params[:order_id] %>

或者,获取值(使用 params 在控制器中并将其分配给 @order_id 那里。

Alternately, capture the value (with params) in the controller and assign it to @order_id there.

这篇关于Ruby on Rails在URL中传递参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆