2013-10-10 86 views
2

我遇到一个错误:Laravel - 路由更新错误

Some mandatory parameters are missing ("users") to generate a URL for route "users.update". 

我有这一套对我的看法:

{{ Form::open(array('action' => array('[email protected]'))) }} 

     <div> {{ Form::label('username', 'Username:') }} 
      {{ Form::text('username', $user->username , array('class' => 'form-control')) }}</div> 

      <div> {{ Form::label('email', 'Email Address:') }} 
      {{ Form::text('email', $user->email , array('class' => 'form-control')) }}</div> 

      <div> {{ Form::label('new_password', 'New Password:') }} 
      {{ Form::text('new_password', '', array('class' => 'form-control')) }} </div> 

      <div> {{ Form::label('old_password', 'Old Password:') }} 
      {{ Form::text('password', '', array('class' => 'form-control')) }} </div> 

      {{ Form::submit() }} 


{{ Form::close() }} 

我也有一个功能,在我的控制器连接到更新:

public function update() { 
     return 'This is an update'; 
    } 

最后,当我检查Artisan命令中可用的所有路线时,我发现该更新的路线为:users/{users}

我的代码有什么问题?我试图更新一个用户,它会引发这个错误。

+1

您在表单的action属性中缺少'{user}'参数。 __in simple terms__,你缺少'{{Form :: open(array('action'=> array('UsersController @ update')))}''中的第二个参数。它应该像'{{Form :: open(array('action'=> array('UsersController @ update',$ id)))}}'其中id是用户的id。 – itachi

+0

是的。我刚刚发现,但仍然不能解决问题,因为它是为了更新。无需为更新,存储和删除等操作设置这些变量。 – Bajongskie

+0

然后在routes.php – itachi

回答

0

您的路线以这样的方式来期待一个变量$用户传递定义。因为:{users}

相反,你应该把它定义喜欢:

Route::post('users/update', '[email protected]'); 

,然后在功能更新()通过得到这个职位变量:

$users_data = Input::get(); 

OR

如果要保留参数,请通过传递附加参数重新定义表格:

{{ Form::open(array('action' => array('[email protected]', $id))) }} 
+0

中更改'users'的路线,'users/{users}'有什么问题? – itachi

+1

谢谢。这解决了我的问题,但它是一个更新,所以我使用:Route :: put()来代替。 – Bajongskie

0

您打开FORM的方式,它需要一个路由参数。如果你不希望传递的参数,只需要使用以下内容:

{{ Form::open(array('action' => '[email protected]')) }} 

相反的:

{{ Form::open(array('action' => array('[email protected]'))) }} 
0

即使您正在设置操作,您仍然可能需要一个操作路线。我强烈建议您始终使用CLEARED DEFINED路线到您的控制器。看看Resource Controllers是否可以帮助你,以防万一你不想定义每一条上帝的路线(我不)。

而且,最后回答你的问题:我觉得

{{ Form::open(array('action' => '[email protected]')) }} 

...可以解决你的问题。希望能帮助到你。对不起,我的英语不好! :D