我试图重定向到显示操作(所以我可以看到新的帖子)如果保存在创建操作成功,但得到错误? [英] I am trying to redirect to the show action(so I can see the new post) if a save is successful in the create action, but getting error?

查看:239
本文介绍了我试图重定向到显示操作(所以我可以看到新的帖子)如果保存在创建操作成功,但得到错误?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图重定向到显示操作成功的创建操作后,为了看到用户刚刚创建的帖子(帖子控制器)。我收到此错误消息:

I am trying to redirect to the show action after a successful create action in order to see the post the user just made (Post controller). I am getting this error message:

语法错误,意外的keyword_ensure,期望输入结束

syntax error, unexpected keyword_ensure, expecting end-of-input

更多调试信息:
unless source.valid_encoding?
raise WrongEncodingError.new(@source,Encoding.default_internal)
end

More debugging info: unless source.valid_encoding? raise WrongEncodingError.new(@source, Encoding.default_internal) end

begin

mod.module_eval(source, identifier, 0)

ObjectSpace.define_finalizer(self, Finalizer[method_name, mod])

rescue => e # errors from template code

if logger = (view && view.logger)

logger.debug "ERROR: compiling #{method_name} RAISED #{e}"

logger.debug "Function body: #{source}"

这是Post控制器

    class PostsController < ApplicationController

    def index
       @posts = Post.all
    end


    def show
       @post = Post.find(params[:id])
    end

    def new
       @post = Post.new(params[:id])
    end

    def create
       @post = Post.new(params[:id])

       if @post.save
         redirect_to @post
       else
         render :new
       end
    end
end

显示视图:

<h1>Posts</h1> 


Title: <%= post.title %><br>
Body: <%= post.body %><br>
url: <%= post.url %><br>
<% end %>

索引视图:

<% @posts.each do |post| %>

感谢您的帮助。

推荐答案

SHOW 视图必须为:

< h1>帖子< / h1>
标题:<%= @ post.title%>< br>
身体:<%= @ post.body%>< br>

>视图必须为:

INDEX view must be:

< h1>帖子< / h1>
<%@ posts.each do | post | %>
标题:<%= post.title%>< br>
正文:<%= post.body%>< br>
<%end%>

def create
  @post = Post.new(post_params)
  if @post.save
    redirect_to @post
  else
    render :new
  end
end

并添加到控制器(最后结束)之前:

And add to controller (before last end):

private
def post_params
  params.require(:post).permit(:title, :body, :url) #here you put all attributes you want be able to assign when creating the post
end

这篇关于我试图重定向到显示操作(所以我可以看到新的帖子)如果保存在创建操作成功,但得到错误?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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