赞
踩
我正在使用ajax创建属于特定主题的帖子,我在帖子索引页面中呈现表单.每个帖子都有很多标签,我也使用设计认证和CanCanCan授权.
我需要捕获帖子提交的错误消息并通过此JavaScript模板Create.js.erb在浏览器中显示自定义错误消息,而不是在帖子表单中.
以下是代码:
后控制器
class PostsController < ApplicationController load_and_authorize_resource before_action :set_post, only: [:show, :edit, :update, :destroy, :update_status] skip_before_action :verify_authenticity_token # GET /posts # GET /posts.json def index if params[:topic_id].present? @topic = Topic.find(params[:topic_id]) @posts = @topic.posts.paginate(page: params[:page], per_page: 10) @post = @topic.posts.new else @posts = Post.eager_load(:topic, :user).paginate(page: params[:page], per_page: 10) end @tags =Tag.all end # GET /posts/1 # GET /posts/1.json def show @tags = @posts.tags end def update_status current_user.posts<<(@posts) end # GET /posts/new def new @topic = Topic.find(params[:topic_id]) @posts = @topic.posts.new @tags =Tag.all end # GET /posts/1/edit def edit @tags = @posts.tags end # POST /posts # POST /posts.json def create @topic = Topic.find(params[:topic_id]) @posts = @topic.posts.create(post_params) respond_to do |format| @posts.user_id = current_user.id if @posts.save format.html { redirect_to topic_posts_path(@topic), notice: 'Post was successfully created.' } format.js format.json { render :show, status: :created, location: @posts } else format.html { render :new } format.json { render json: @posts.errors, status: :unprocessable_entity } end end end # PATCH/PUT /posts/1 # PATCH/PUT /posts/1.json def update @tags = @posts.tags respond_to do |format| if params[:rate].to_i>0 @posts.ratings.create(:star => params[:rate]) format.html { redirect_to post_path(@posts), notice: 'Rating was successfully updated.' } elsif @posts.update(post_params) format.html { redirect_to post_path(@posts), notice: 'Post was successfully updated.' } format.json { render :show, status: :ok, location: @posts } else format.html { render :edit } format.json { render json: @posts.errors, status: :unprocessable_entity } end end end # DELETE /posts/1 # DELETE /posts/1.json def destroy @posts.destroy respond_to do |format| format.html { redirect_to topic_posts_url(@posts.topic_id), notice: 'Post was successfully destroyed.' } format.json { head :no_content } end end private # Use callbacks to share common setup or constraints between actions. def set_post @posts = Post.find(params[:id]) end # Never trust parameters from the scary internet, only allow the white list through. def post_params params.require(:post).permit(:image, :name, :message, :topic_id, {tag_ids:[]}, :rate, :user_id) end protected def json_request? request.format.json? end end
发表表格
<%= form_for [@topic, @post], remote: true do |f| %> <% if @post.errors.any? %><%= pluralize(@post.errors.count, "error") %> prohibited this post from being saved:
<% @post.errors.full_messages.each do |message| %>
声明:本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:【wpsshop博客】- <%= message %>
<% end %>推荐阅读
如何解决《Sublime文本语法着色报价问题》经验,为你挑选了1个好方法。sublimetext3,sublime-text-plugin,react-native,javascript,DevBox,在线流程图,编程,编程问答,程序员,开... [详细]赞
踩
如何解决《在JavaScript中拳击强制?》经验,为你挑选了1个好方法。javascript,DevBox,在线流程图,编程,编程问答,程序员,开发者工具,开发工具,json解析,二维码生成,unix时间戳,在线开发工具,前端开发工具,开... [详细]赞
踩
如何解决《是否可以实现"继续作为{Facebook用户名}"按钮?》经验,为你挑选了0个好方法。javascript,iframe,facebook,http,facebook-graph-api,DevBox,在线流程... [详细]赞
踩
如何解决《如何在没有毫秒和Z的情况下在ISO8601中的javascript中输出日期》经验,为你挑选了4个好方法。javascript,date,iso8601,DevBox,在线流程图,编程,编程问答,程序员,开发者工具,开发工具,js... [详细]赞
踩
如何解决《如何使用createPeriodicWave而不是createScriptProcessor和getChannelData》经验,为你挑选了0个好方法。javascript,synthesizer,signal-processin... [详细]赞
踩
如何解决《Androidwebview加载网址不适用于Instagram网页》经验,为你挑选了1个好方法。android,webview,facebook,javascript,twitter,DevBox,在线流程图,编程,编程问答,程序... [详细]赞
踩
如何解决《覆盖动画填充模式:在JavaScript/CSS中转发》经验,为你挑选了0个好方法。javascript,css,jquery,DevBox,在线流程图,编程,编程问答,程序员,开发者工具,开发工具,json解析,二维码生成,un... [详细]赞
踩
如何解决《我怎么知道removeEventListener成功了?》经验,为你挑选了1个好方法。javascript,javascript-events,chrome,firefox,DevBox,在线流程图,编程,编程问答,程序员,开发者... [详细]赞
踩
如何解决《您如何使用已编译的Typescript使用IstanbulCodeCoverage?》经验,为你挑选了2个好方法。javascript,typescript,karma-runner,istanbul,gulp,DevBox,在线... [详细]赞
踩
在鼠标经过的时候不停的显示隐藏html元素,正确的写法应该是下面这样的,需要的朋友可以参考下鼠标经过,滑动,jquery,html,javascript,css,DevBox,在线流程图,编程,编程问答,程序员,开发者工具,开发工具,jso... [详细]赞
踩
如何解决《react-native-包含View的FitImage,而不是整个屏幕大小》经验,为你挑选了3个好方法。javascript,flexbox,reactjs,react-native,容器,https,DevBox,在线流程图,... [详细]赞
踩
如何解决《如何动态隐藏asciidoc元素》经验,为你挑选了1个好方法。asciidoc,asciidoctor,css,javascript,DevBox,在线流程图,编程,编程问答,程序员,开发者工具,开发工具,json解析,二维码生成... [详细]赞
踩
如何解决《使用jQuery将图片从一个div添加到另一个div》经验,为你挑选了1个好方法。html,javascript,css,jquery,dom,DevBox,在线流程图,编程,编程问答,程序员,开发者工具,开发工具,json解析,... [详细]赞
踩
如何解决《箭头功能中的默认参数值》经验,为你挑选了1个好方法。javascript,google-chrome,default-parameters,ecmascript-6,chrome,DevBox,在线流程图,编程,编程问答,程序员,... [详细]赞
踩
如何解决《Javascript数组包含/包含子数组》经验,为你挑选了1个好方法。javascript,arrays,contains,include,indexof,DevBox,在线流程图,编程,编程问答,程序员,开发者工具,开发工具,j... [详细]赞
踩
如何解决《匿名函数VSconst函数--javascript》经验,为你挑选了1个好方法。javascript,const,function,https,DevBox,在线流程图,编程,编程问答,程序员,开发者工具,开发工具,json解析,... [详细]赞
踩
如何解决《仅在$touch为true时显示的角度ng-messages》经验,为你挑选了1个好方法。javascript,angularjs,angular-material,ng-messages,json,DevBox,在线流程图,编程... [详细]赞
踩
如何解决《如何通过特殊字符串在javascript中拆分字符串》经验,为你挑选了1个好方法。javascript,arrays,string,split,windows,DevBox,在线流程图,编程,编程问答,程序员,开发者工具,开发工具... [详细]赞
踩
如何解决《没有(标准)播放器运行VAST?》经验,为你挑选了0个好方法。javascript,video,ads,html5-video,vast,DevBox,在线流程图,编程,编程问答,程序员,开发者工具,开发工具,json解析,二维码... [详细]赞
踩
如何解决《Aurelia有替代品吗?》经验,为你挑选了0个好方法。javascript,production,angularjs,aurelia,https,npm,DevBox,在线流程图,编程,编程问答,程序员,开发者工具,开发工具,j... [详细]赞
踩
相关标签
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。