在控制器中
params.max = Math.min(params?.max?.toInteger() ?: 10, 20)
params.offset = params?.offset?.toInteger() ?: 0
Run Code Online (Sandbox Code Playgroud)
如果您输入以下网址
/books?offset=10&max= //error
/books?offset=10&max=sdf //error
/books?offset=&max=10 //works
/books?offset=adsfa&max=10 //error
java.lang.NumberFormatException: For input string: "asdf"
at java.lang.NumberFormatException.forInputString(NumberFormatException.java:48)
at java.lang.Integer.parseInt(Integer.java:449)
at java.lang.Integer.valueOf(Integer.java:554)
Run Code Online (Sandbox Code Playgroud)
是否有一行常规答案来检查url参数中的null / string字符?
我有一个链接
<%= link_to 'Add link', new_link_path(:link => {:item1_id => @item.id}) %>
Run Code Online (Sandbox Code Playgroud)
传递给
def new
@link = Link.new(params[:link])
@items = Item.all
Run Code Online (Sandbox Code Playgroud)
形式是
<%= form_for(@link) do |f| %>
<%= f.hidden_field :item1_id %>
<%= f.collection_select :item2_id , Item.all , :id , :name %>
Run Code Online (Sandbox Code Playgroud)
所以我想访问链接到item1_id项目的图像.我该如何访问它?我试过了
@item1 = Item.find_by_id(params[:link])
Run Code Online (Sandbox Code Playgroud)
和
@item1 = Item.find_by_id(params[:item_id])
Run Code Online (Sandbox Code Playgroud)
但我不知道什么是对的.
我有一个路由器test/view,我想通过一些类似的test/view/id/123.
我不知道如何在zf2路由器中添加这些参数.
'router' => array(
'routes' => array(
'test' => array(
'type' => 'Literal',
'options' => array(
'route' => '/test',
'defaults' => array(
'__NAMESPACE__' => 'test\Controller',
'controller' => 'Index',
'action' => 'index',
),
),
'may_terminate' => true,
'child_routes' => array(
'view' => array(
'type' => 'Literal',
'options' => array(
'route' => '/view',
'defaults' => array(
'controller' => 'Index',
'action' => 'view',
),
),
),
),
),
),
),
Run Code Online (Sandbox Code Playgroud)
我设置view为儿童路线,但不知道在哪里添加这些参数.
我试过'route' => '/view/:id/:value'和 …
在CakePHP上,我们可以使用ALL POST/GET
$this->params
Run Code Online (Sandbox Code Playgroud)
在Yii,我不确定它是否具有等价物.只看到我想得到一个字段值,无论是(POST/GET),但我需要指定它.
Yii::app()->request->getParam('email')
Run Code Online (Sandbox Code Playgroud) 我是Rails的新手,我有一种感觉,我正在从错误的角度接近这个但是这里......我有一个显示车辆的列表页面,我正在尝试添加过滤功能,用户可以过滤结果通过vehicle_size,制造商和/或payment_options.
使用三个选择表单字段,用户可以设置以下值:vehicle_size,:manufacturer和/或:payment_options参数,并将这些值提交给我正在使用的控制器
@vehicles = Vehicle.order("vehicles.id ASC").where(:visible => true, :vehicle_size => params[:vehicle_size] )
Run Code Online (Sandbox Code Playgroud)
一种查询.这适用于单个参数(上面返回的结果是正确的车辆尺寸)但我希望能够传递所有3个参数而不会得到任何结果,如果其中一个参数留空.
有没有办法在不经过编写过程的情况下执行此操作if语句定义不同的where语句,具体取决于params的设置?如果我添加更多过滤器选项,这可能会变得非常繁琐.如果has_key解决方案的效果可能是某种内联:
@vehicles = Vehicle.order("vehicles.id ASC").where(:visible => true, if(params.has_key?(:vehicle_size):vehicle_size => params[:vehicle_size], end if(params.has_key?(:manufacturer):manufacturer => params[:manufacturer] end )
Run Code Online (Sandbox Code Playgroud)
对N00b问题表示歉意,并提前致谢.
我有一个具有嵌套资源链接的Opportunity模型.在我的views/opportunities/show页面中,当我点击其中一个链接的"DestroY"时,我收到错误:
param丢失或值为空:link
它抱怨的代码片段是:
def link_params
params.require(:link).permit(:description, :link_url)
end
Run Code Online (Sandbox Code Playgroud)
这是我的破坏代码:
def destroy
@opportunity = Opportunity.find(params[:opportunity_id])
@link = @opportunity.links.find(link_params)
@link.destroy
respond_to do |format|
format.html { redirect_to links_url, notice: 'Link was successfully destroyed.' }
format.json { head :no_content }
end
Run Code Online (Sandbox Code Playgroud) 我正在尝试为项目编写一个方法,该方法将任意数量的列表作为参数,并返回一个包含所有这些列表共享的术语的新列表.我有功能代码,但我更喜欢使用params关键字,而不是必须创建一个列表列表,其中包含我想要比较的所有列表.
static List<T> Shared<T>(List<T> first, List<T> second)
{
List<T> result = new List<T>();
foreach (T item in first)
if (second.Contains(item) && !result.Contains(item)) result.Add(item);
return result;
}
static List<T> Shared<T>(List<List<T>> lists)
{
List<T> result = lists.First();
foreach (List<T> list in lists.Skip(1))
{
result = Shared<T>(result, list);
}
return result;
}
Run Code Online (Sandbox Code Playgroud)
是我当前的代码,它可以很好地比较两个列表,但为了比较两个以上的列表,我必须创建一个新的列表,如:
List<int> nums1 = new List<int> { 1, 2, 3, 4, 5, 6 };
List<int> nums2 = new List<int> { 1, 2, 3 };
List<int> nums3 = new List<int> { 6, …Run Code Online (Sandbox Code Playgroud) 我有一个来自以下javascript文件的POST请求:
this.submitQuoteButton = $("<button />")
.text("Download PDF")
.addClass("submitQuoteButton button-success pure-button")
.click(function() {
$.ajax({
type: "POST",
url: "../quotes/create",
data: {
name : "John ",
email: "john@john.com",
json: "data",
uid: "uid",
},
dataType:'text',
success: function(data,status,xhr){
console.log(status);
alert("SUCCESS!");
},
error: function(xhr,status,error){
console.log(status,error);
alert("ERROR!");
}
});
})
Run Code Online (Sandbox Code Playgroud)
这个POST调用我的quotes_controller创建方法,我有这个
def create
@quote = Quote.new(quote_params)
if @quote.save
redirect_to root_url
else
redirect_to blog_path
end
end
private
def quote_params
params.require(:quotes).permit(:uid, :name, :email, :json)
end
Run Code Online (Sandbox Code Playgroud)
目的是获取POST请求中传递的数据,并使用create方法将其保存到我的数据库中.我这样做了吗?我得到一个:
param is missing or the value is empty for: quotes
Run Code Online (Sandbox Code Playgroud)
这是否意味着我的数据库设置或创建方法存在问题?
在Airport模型我primary_key用作机场国际代码,但Rails仍然:id在请求中使用.
我已经开始了 airport.rb
self.primary_key = 'code'
Run Code Online (Sandbox Code Playgroud)
并在 airports_controller.rb
def show
@airport = Airport.find(params[:code])
end
Run Code Online (Sandbox Code Playgroud)
但是当我试图获得时domain.com/airports/led,我会收到:
找不到没有身份证的机场
在请求参数我有
{"action"=>"show", "controller"=>"airports", "id"=>"led"}
Run Code Online (Sandbox Code Playgroud)
如果我将控制器更改params[:code]为params[:id]可行,但我不想以这种方式使用它,因为我id在DB中没有列.
当我们将params关键字与多维数组一起使用时,为什么会出现编译时错误?
using System;
namespace Testing_Params_Keyword
{
class Program
{
static void Main(string[] args)
{
//Calculate in invoked
Calculate(25.4, 26.2, 27.8, 28.9);
}
//Declearing Calculate method
public static void Calculate(params float [ , ] Money)//----** Here is error **
{
//Divide values of column1 by column2
float row1 = Money[0, 0] / Money[0, 1];
float row2 = Money[1, 0] / Money[1, 1];
Console.WriteLine(row1 + row2);
}//end of method Calculate
}
}
Run Code Online (Sandbox Code Playgroud)
给我错误
params参数必须是单维数组
为什么它必须是单维数组?