【问题标题】:create action in rails在 Rails 中创建动作
【发布时间】:2013-06-12 08:39:53
【问题描述】:

当我在 rails 中使用脚手架时,控制器会创建各种方法,例如

新建、创建、显示、索引等

但在这里我无法理解新动作到创建动作的过渡

例如。当我点击新帖子时,它会查找新操作,现在它呈现_form,但是在提交时如何将数据输入到该特定表中,控制器的创建操作在哪里调用以及如何调用?

我的posts_controller

def new
@post = Post.new
@post.user_id = current_user.id
@post.save
respond_to do |format|
  format.html # new.html.erb
  format.json { render json: @post }
end
end

# GET /posts/1/edit
def edit
@post = Post.find(params[:id])
authorize! :manage, @post
end

# POST /posts
# POST /posts.json
def create
@post = Post.new(params[:post])

respond_to do |format|
  if @post.save
    format.html { redirect_to @post, notice: 'Post was successfully created.' }
    format.json { render json: @post, status: :created, location: @post }
  else
    format.html { render action: "new" }
    format.json { render json: @post.errors, status: :unprocessable_entity }
  end
end
end

【问题讨论】:

    标签: ruby-on-rails ruby ruby-on-rails-3


    【解决方案1】:

    这都是关于 HTTP 动词和路由的。

    您的表单将向 /posts 路由发出 POST 请求。如果您使用rake routes 列出您的路由,您会看到对该特定路由的所有 POST 请求都被定向到PostsController 中的create 操作,简称为posts#create

    【讨论】:

      【解决方案2】:

      当您将浏览器指向/posts/new 时,它会呈现new 操作,该操作会为您提供要填写的表单(在app/views/posts/new.html.erbapp/views/posts/_form.html.erb 中定义。当您单击提交 按钮,它将您的数据发布到 create 操作,它实际上在数据库中创建记录。

      看看你的 PostsController 代码,你可能想要这条线

      @post.save
      

      在您的 new 操作中,因为这会将空白记录保存到数据库中 - 无论用户是否填写表单。而且,你可能想搬家

      @post.user_id = current_user.id
      

      到您的create 操作,因为这是您实际将帖子保存到数据库的位置。

      【讨论】:

      • 好的,这意味着 create 是在提交按钮时自动调用的方法!
      【解决方案3】:

      默认在表单上搭建脚手架 (read here)

      当用户单击此表单上的创建帖子按钮时,浏览器 将信息发送回控制器的创建操作 (Rails 知道调用 create 操作,因为发送的表单是 HTTP POST 请求;这是当时的约定之一 前面提到过):

      def create
        @post = Post.new(params[:post])
      
        respond_to do |format|
          if @post.save
            format.html  { redirect_to(@post,
                          :notice => 'Post was successfully created.') }
            format.json  { render :json => @post,
                          :status => :created, :location => @post }
          else
            format.html  { render :action => "new" }
            format.json  { render :json => @post.errors,
                          :status => :unprocessable_entity }
          end
        end
      end
      

      如果你想在新的表单脚手架上自定义一个动作,你应该在你的表单上添加:url => {:action => "YourActionName"}

      例子:

      #form
      form_for @post, :url => {:action => "YourActionName"}
      
      #controller
      def YourActionName
        @post = Post.new(params[:post])
      
        respond_to do |format|
          if @post.save
            format.html  { redirect_to(@post,
                          :notice => 'Post was successfully created.') }
            format.json  { render :json => @post,
                          :status => :created, :location => @post }
          else
            format.html  { render :action => "new" }
            format.json  { render :json => @post.errors,
                          :status => :unprocessable_entity }
          end
        end
      end
      
      #route
      match '/posts/YourActionName`, 'controllers#YourActionName', :via => :post
      

      【讨论】:

        猜你喜欢
        • 2014-08-05
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2013-10-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多