如何在ruby on rails中通过从new和create动作中获取数据来在show page上显示数据?

nwsw7zdq  于 12个月前  发布在  Ruby
关注(0)|答案(2)|浏览(74)

嗨,我使用rails并试图在点击提交按钮后获取数据,但我的数据在按下提交按钮后不保存,请告诉我如何在保存后在show.html.erb上显示数据:下面是代码:
articles_controller.erb:

class ArticlesController < ApplicationController


  def show
    @article = Article.find(params[:id])
  end


  def index
    @articles = Article.all
  end

  def new
    @article=Article.new
  end


  def create
    @article = Article.new(article_params)
    @article.save
    redirect_to @article
  end

  private

  def article_params
    params.require(:article).permit(:title, :description)
  end
end

new.html.erb:

<h1>Create new File</h1>

<%= form_with scope: :article, url: articles_path , local: true do |f| %>
  <p>
    <%= f.label :title, "Title" %><br/>
    <%= f.text_field :title %>
  </p>
  <p>
    <%= f.label :description, "Description" %><br/>
    <%= f.text_area :description %>
  </p>
  <p><%= f.submit %> </p>
<% end %>

routes.rb:
Rails.应用程序.路由.绘制

# Define your application routes per the DSL in https://guides.rubyonrails.org/routing.html

  # Defines the root path route ("/")
  root "articles#index"
  resources :articles, only: [:edit, :update, :show, :destroy, :index, :new]

end

show.html.erb:

<h1>show</h1>
<h3><strong>Title: </strong><%= @article.title %></h3>
<h3><strong>Description: </strong><%= @article.description %></h3>
2lpgd968

2lpgd9681#

无论你遵循什么教程-我不认为它很好。
不要只是假设创建或更新记录会成功(当你假设时,你做了一个...)。用户将输入无效的输入,你需要处理:

class ArticlesController < ApplicationController

  # ...

  # POST /articles
  def create
    @article = Article.new(article_params)
    if @article.save
      redirect_to @article
    else
      render :new # render the form again but with error messages
    end
  end

  private

  def article_params
    params.require(:article).permit(:title, :description)
  end
end

您还需要让resources为create操作生成POST /articles路由:

# there is no point in using "only" when you're generating everything
resources :articles

您还希望以面向资源的方式使用表单帮助程序:

<%= form_with model: @article do |f| %>
  <% if @article.errors.any? %>
    <div id="error_explanation">
      <h2><%= pluralize(@article.errors.count, "error") %> prohibited this article from being saved:</h2>
      <ul>
        <% @article.errors.each do |error| %>
          <li><%= error.full_message %></li>
        <% end %>
      </ul>
    </div>
  <% end %>

  <p>
    <%= f.label :title, "Title" %><br/>
    <%= f.text_field :title %>
  </p>
  <p>
    <%= f.label :description, "Description" %><br/>
    <%= f.text_area :description %>
  </p>
  <p><%= f.submit %></p>
<% end %>

这将把输入绑定到模型,这样当验证失败时,表单就不会被清空,这会让用户非常不高兴。如果任何验证失败,我们也会在表单中显示错误消息。
Rails是一种约定,而不是配置驱动。你不需要或不想配置URI、作用域或其他任何东西,除非你有很强的理由这样做。
Rails将通过查看模型来确定表单操作使用哪个路径(/articles/articles/:id)以及正确的HTTP方法。
Rails中的基本CRUD操作在官方指南中有很好的介绍。

zengzsys

zengzsys2#

更新我多花了一分钟阅读你的代码。请务必阅读对您的问题的评论,乡亲们给予很好的见解。
什么是不起作用的你是如何得出这个结论的?我假设某些页面呈现时显示错误。

浏览你的代码给人的印象是,它应该大致做你所期望的,* 但是 *:你没有检查@article.save是否在创建操作中工作(感谢分享所有代码btw,这使得回答更容易)。save(和updatecreate)将返回truefalse,以告诉您保存到数据库是否有效。
为了得到一些有用的东西,作为基础:如果使用 *rails generators来搭建资源 rails g scaffold MyThing name:string),您应该看到它在最简单的默认情况下是如何工作的。您可以稍后手动删除scaffold代码或调用rails d scaffold MyThing)。
希望能帮上忙。也许还有其他潜在的问题(
更新,有 *),但 * 总是 * 确保检查create, update, save的返回值。此外,您可以使用@article = Article.create(article_params)(或者,用于调试... cle.create!(...),而不是使用new + save
你可以 * 调用@article.save!(使用“bang”:!),如果它不工作,将抛出异常(调试时很好)。其他的“砰”也一样!“-variants(create!, update!, save!),则抛出异常。然而,使用异常来控制流是有争议的/不鼓励的。特别是对于初学者来说,总是检查和处理返回代码可能是一个很好的做法(感谢max的评论)。

相关问题