我在发布数据以更新已成功更新的所有内容时遇到 302 重定向问题,除了我收到 302 并且我的页面被重定向的图像。
这是我的表单 Blade 代码:
{!! Form::model($post, ['method' => 'PATCH','route' => ['posts.update', $post->id],'data-parsley-validate' => '','class' => 'form-horizontal','files'=>true,'name'=>'updateabout']) !!}
<div class="col-md-8">
<input type="hidden" name="csrf_test_name" value="{!! csrf_token() !!}">
<div class="form-group">
<label class="col-md-2 control-label"> Title : </label>
<div class="col-md-10">
<input class="form-control " name="title" type="text" value="{{$post->title}}" id="title">
</div>
</div>
<div class="form-group">
<label class="col-md-2 control-label">Slug:</label>
<div class="col-md-10">
<input class="form-control" name="slug" type="text" value="{{$post->slug}}" id="slug">
</div>
</div>
<div class="form-group">
<label class="col-md-2 control-label">Category:</label>
<div class="col-md-10">
{{ Form::select('category_id', $categories, null, ['class' => 'form-control']) }}
</div>
</div>
<div class="form-group">
<label class="col-md-2 control-label">Tags:</label>
<div class="col-md-10">
{{ Form::select('tags[]', $tags, null, ['class' => 'form-control select2-multi', 'multiple' => 'multiple']) }}
</div>
</div>
<div class="form-group">
<label class="col-md-2 control-label">Short-Description:</label>
<div class="col-md-10">
<textarea name="short_description" class="form-control" maxlength="140" rows="7" required
id="shortdesc">{{$post->short_description}}</textarea>
<script src="{{ asset("/public/admin/ckeditor/ckeditor.js") }}"></script>
<script>
CKEDITOR.replace('shortdesc', {
height: 100,
toolbar: 'Custom', //makes all editors use this toolbar
toolbarStartupExpanded: false,
toolbarCanCollapse: false,
toolbar_Custom: [] //define an empty array or whatever buttons you want.
});
</script>
</div>
</div>
<div class="form-group">
<label class="col-md-2 control-label">Date:</label>
<div class="col-md-10">
{{ Form::text('date', null, ['class' => 'form-control','id'=>'date']) }}
</div>
<script type="text/javascript">
$(document).ready(function () {
$(function () {
$('#date').datepicker({format: 'HH:mm:ss'});
})
});
</script>
</div>
<div class="form-group">
<label class="col-md-2 control-label"> Image: </label>
<div class="col-md-10">
<img src="{{asset($post->image)}}" height="200" width="579">
<input type="hidden" name="imageold" value="{{$post->image}}">
</div>
</div>
<div class="form-group">
<label class="col-md-2 control-label">Change Image: </label>
<div class="col-md-10">
<input type="file" name="imagemain" id="file">
</div>
</div>
<div class="form-group">
<label class="col-md-2 control-label">Description:</label>
<div class="col-md-10">
{{ Form::textarea('description', null, ['class' => 'form-control','id'=>'long_description']) }}
<script src="//cdn.tinymce.com/4/tinymce.min.js"></script>
<script>
tinymce.init({
selector: '#long_description',
convert_urls: false,
statusbar: false,
height: '300',
plugins: 'image code print preview fullpage searchreplace autolink directionality visualblocks visualchars fullscreen image link table charmap hr pagebreak nonbreaking toc insertdatetime advlist lists textcolor wordcount imagetools contextmenu colorpicker textpattern media ',
toolbar: 'formatselect | bold italic strikethrough forecolor backcolor | link | alignleft aligncenter alignright alignjustify | numlist bullist outdent indent | removeformat |undo redo | image code| link fontsizeselect | ',
image_title: true,
automatic_uploads: true,
images_upload_url: '{{url("/admin/upload")}}',
file_picker_types: 'image',
file_picker_callback: function (cb, value, meta) {
var input = document.createElement('input');
input.setAttribute('type', 'file');
input.setAttribute('accept', 'image/*');
input.onchange = function () {
var file = this.files[0];
var reader = new FileReader();
reader.readAsDataURL(file);
reader.onload = function () {
var id = 'blobid' + (new Date()).getTime();
var blobCache = tinymce.activeEditor.editorUpload.blobCache;
var base64 = reader.result.split(',')[1];
var blobInfo = blobCache.create(id, file, base64);
blobCache.add(blobInfo);
cb(blobInfo.blobUri(), {title: file.name});
};
};
input.click();
}
});
</script>
</div>
</div>
<div class="form-group">
<label class="col-md-2 control-label">Meta-title:</label>
<div class="col-md-10">
{{ Form::textarea('meta_title', null, ['class' => 'form-control','id'=>'meta-title']) }}
<script>
CKEDITOR.replace('meta-title', {
height: 100,
toolbar: 'Custom', //makes all editors use this toolbar
toolbarStartupExpanded: false,
toolbarCanCollapse: false,
toolbar_Custom: [] //define an empty array or whatever buttons you want.
});
</script>
</div>
</div>
<div class="form-group">
<label class="col-md-2 control-label">Meta-Keywords:</label>
<div class="col-md-10">
{{ Form::textarea('meta_keywords', null, ['class' => 'form-control','id'=>'meta-keywords']) }}
<script src="{{ asset("/public/admin/ckeditor/ckeditor.js") }}"></script>
<script>
CKEDITOR.replace('meta-keywords', {
height: 100,
toolbar: 'Custom', //makes all editors use this toolbar
toolbarStartupExpanded: false,
toolbarCanCollapse: false,
toolbar_Custom: [] //define an empty array or whatever buttons you want.
});
</script>
</div>
</div>
<div class="form-group">
<label class="col-md-2 control-label">Posted By:</label>
<div class="col-md-10">
{{ Form::text('authorname', null, ['class' => 'form-control']) }}
</div>
</div>
<hr>
<br>
<div class="form-group row">
<div class="col-sm-offset-3 col-sm-6">
{{ Form::submit('Update Post', array('class' => 'btn btn-success')) }}
<a type="button" id="addslide" class="btn btn-danger">Cancel</a>
</div>
</div>
</div>
{!! Form::close() !!}
和我的 Controller :
public function update(Request $request, $id)
{
// Validate the data
$postdata = Post::find($id);
//print_r($post);
if ($request->input('slug') == $postdata->slug) {
//echo 'slug match';
//exit();
$this->validate($request, [
'title' => 'required|max:255',
'category_id' => 'required|integer',
'long_description' => 'required',
]);
} else {
//echo 'new slug matching';
//exit();
$this->validate($request, [
'title' => 'required|max:255',
'slug' => 'required|alpha_dash|min:5|max:255|unique:posts,slug',
'category_id' => 'required|integer',
'long_description' => 'required',
]);
}
if ($request->hasFile('imagemain')) {
//echo 'request has old file to unlink ';
// exit();
//unlink($request->get('image'));
// Get filename with the extension
$filenameWithExt = $request->file('imagemain')->getClientOriginalName();
// Get just filename
$filename = pathinfo($filenameWithExt, PATHINFO_FILENAME);
// Get just ext
$extension = $request->file('imagemain')->getClientOriginalExtension();
$fileNameToStore = $filename . '_' . time() . '.' . $extension;
// Upload Image
$path = $request->file('imagemain')->move('public/blogsimages', $fileNameToStore);
// Filename to store
echo $fileNameToStore = $path;
} else {
$fileNameToStore = $request->input('imageold');
// exit();
}
// Save the data to the database
$post = Post::find($id);
$post->title = $request->input('title');
$post->slug = $request->input('slug');
$post->category_id = $request->input('category_id');
$post->description = $request->input('description');
$post->short_description = $request->input('short_description');
$post->date = $request->input('date');
$post->image = $fileNameToStore;
$post->meta_title = $request->input('meta_title');
$post->meta_keywords = $request->input('meta_keywords');
$post->authorname = $request->input('authorname');
$post->save();
if (isset($request->tags)) {
$post->tags()->sync($request->tags);
} else {
$post->tags()->sync([]);
}
// set flash data with success message
Session::flash('success', 'This post was successfully saved.');
// redirect with flash data to posts.show
return redirect()->route('posts.show', $post->id);
}
当我发布数据时,除了显示 302 的图像和表格外,所有内容都根据要求进行了更新,这里是屏幕截图:
我的路线是:Route::resource('posts', 'PostController');
最佳答案
我认为验证脚本有错误 请尝试代码
public function update(Request $request, $id)
{
// Validate the data
$postdata = Post::find($id);
//WITHOUT VALIDATION
if ($request->hasFile('imagemain')) {
//echo 'request has old file to unlink ';
// exit();
//unlink($request->get('image'));
// Get filename with the extension
$filenameWithExt = $request->file('imagemain')->getClientOriginalName();
// Get just filename
$filename = pathinfo($filenameWithExt, PATHINFO_FILENAME);
// Get just ext
$extension = $request->file('imagemain')->getClientOriginalExtension();
$fileNameToStore = $filename . '_' . time() . '.' . $extension;
// Upload Image
$path = $request->file('imagemain')->move('public/blogsimages', $fileNameToStore);
// Filename to store
echo $fileNameToStore = $path;
} else {
$fileNameToStore = $request->input('imageold');
// exit();
}
// Save the data to the database
$post = Post::find($id);
$post->title = $request->input('title');
$post->slug = $request->input('slug');
$post->category_id = $request->input('category_id');
$post->description = $request->input('description');
$post->short_description = $request->input('short_description');
$post->date = $request->input('date');
$post->image = $fileNameToStore;
$post->meta_title = $request->input('meta_title');
$post->meta_keywords = $request->input('meta_keywords');
$post->authorname = $request->input('authorname');
$post->save();
if (isset($request->tags)) {
$post->tags()->sync($request->tags);
} else {
$post->tags()->sync([]);
}
// set flash data with success message
Session::flash('success', 'This post was successfully saved.');
// redirect with flash data to posts.show
return redirect()->route('posts.show', $post->id);
}
这将解决您的问题。
关于php - 在 laravel 的表单更新中得到 302,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50540253/
给定这段代码defcreate@upgrades=User.update_all(["role=?","upgraded"],:id=>params[:upgrade])redirect_toadmin_upgrades_path,:notice=>"Successfullyupgradeduser."end我如何在该操作中实际验证它们是否已保存或未重定向到适当的页面和消息? 最佳答案 在Rails3中,update_all不返回任何有意义的信息,除了已更新的记录数(这可能取决于您的DBMS是否返回该信息)。http://ar.ru
我得到了一个包含嵌套链接的表单。编辑时链接字段为空的问题。这是我的表格:Editingkategori{:action=>'update',:id=>@konkurrancer.id})do|f|%>'Trackingurl',:style=>'width:500;'%>'Editkonkurrence'%>|我的konkurrencer模型:has_one:link我的链接模型:classLink我的konkurrancer编辑操作:defedit@konkurrancer=Konkurrancer.find(params[:id])@konkurrancer.link_attrib
我将应用程序升级到Rails4,一切正常。我可以登录并转到我的编辑页面。也更新了观点。使用标准View时,用户会更新。但是当我添加例如字段:name时,它不会在表单中更新。使用devise3.1.1和gem'protected_attributes'我需要在设备或数据库上运行某种更新命令吗?我也搜索过这个地方,找到了许多不同的解决方案,但没有一个会更新我的用户字段。我没有添加任何自定义字段。 最佳答案 如果您想允许额外的参数,您可以在ApplicationController中使用beforefilter,因为Rails4将参数
我有一个服务模型/表及其注册表。在表单中,我几乎拥有服务的所有字段,但我想在验证服务对象之前自动设置其中一些值。示例:--服务Controller#创建Action:defcreate@service=Service.new@service_form=ServiceFormObject.new(@service)@service_form.validate(params[:service_form_object])and@service_form.saverespond_with(@service_form,location:admin_services_path)end在验证@ser
我正在尝试为我的iOS应用程序设置cocoapods但是当我执行命令时:sudogemupdate--system我收到错误消息:当前已安装最新版本。中止。当我进入cocoapods的下一步时:sudogeminstallcocoapods我在MacOS10.8.5上遇到错误:ERROR:Errorinstallingcocoapods:cocoapods-trunkrequiresRubyversion>=2.0.0.我在MacOS10.9.4上尝试了同样的操作,但出现错误:ERROR:Couldnotfindavalidgem'cocoapods'(>=0),hereiswhy:U
这太简单了,太荒谬了,我在任何地方都找不到关于它的任何信息,包括API文档和Rails源代码:我有一个:belongs_to关联,我开始理解当您没有关联时您在Controller中调用的正常模型方法与您有关联时调用的方法略有不同。例如,我的关联在创建Controller操作时运行良好:@user=current_user@building=Building.new(params[:building])respond_todo|format|if@user.buildings.create(params[:building])#etcetera但我找不到关于更新如何工作的文档:@user
升级到OSXYosemite后,我现有的pow.cx安装不起作用。升级到最新的pow.cx无效。通过事件监视器重新启动它也没有成功。 最佳答案 卸载(!)并重新安装解决了这个问题。curlget.pow.cx/uninstall.sh|shcurlget.pow.cx|sh 关于ruby-on-rails-OSXYosemite更新破坏了pow.cx,我们在StackOverflow上找到一个类似的问题: https://stackoverflow.com/q
我们在Ubuntu14.04和Gitlab9.3.7上运行,运行良好。我们正在尝试更新到Gitlabv9.3.8的最新安全补丁,但它给我们这个错误:Gem::Ext::BuildError:ERROR:Failedtobuildgemnativeextension.currentdirectory:/home/git/gitlab/vendor/bundle/ruby/2.3.0/gems/re2-1.0.0/ext/re2/usr/local/bin/ruby-r./siteconf20170720-19622-15i0edf.rbextconf.rbcheckingformain(
我遇到了以下问题。我有一个名为user的模型,它有一个名为activated的列。我试图通过激活的方法更新该值?但它给我错误:验证失败:密码不能为空,密码太短(最少6个字符)这对我来说没有意义,因为我没有接触密码字段!我只想更新激活的列。我把我认为相关的代码放在这里,但如果你认为你需要更多,请问:)非常感谢您!型号:attr_accessor:passwordattr_accessible:name,:email,:password,:password_confirmation,:activatedhas_many:sucu_votesemail_regex=/\A[\w+\-.]+@
当且仅当模型存在时,我才尝试更新模型的值。如果没有,我什么都不做。搜索似乎只返回更新或创建问题/答案,但我不想创建。我知道我可以用一个简单的方法来做到这一点:found=Model.find_by_id(id)iffoundupdatestuffend但是,我觉得有一种方法可以在一次调用中完成此操作,而无需分配任何临时本地值或执行if。如果记录不存在,我该如何编写一个Rails调用来更新记录而不出现嘈杂错误?最新的Rails3.x 最佳答案 您可以使用try在对find_by_id或where的结果调用update_attribut