【问题标题】:Rails 4: Access all CRUD from index.html.erbRails 4:从 index.html.erb 访问所有 CRUD
【发布时间】:2014-10-11 23:38:58
【问题描述】:

我正在尝试仅创建一个 1 页索引网站。 我想在我的 1 索引中处理创建、删除、编辑所有内容。 (非常基础)

但是,我跟随初学者入门: http://guides.rubyonrails.org/getting_started.html

我的文章控制器示例:

def index
 @articles = Article.all #Create Articles and then add the for loop inside the index.html
 @article = Article.new
end

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

def new
 @article = Article.new
end 
...

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

我使用@articles 填充所有已添加的文章。然后@article 用于 CRUD。除了我无法从 index.html 页面完全控制 CRUD。

<form role="form" class="form-inline">
    <div class="form-group">
      <%= form_for @article do |f| %>

          <div class="form-group">
            <label for="textUserInputDescription"><%= f.label :title %><br/><%= f.text_field :title %>
            </label>
            <label for="textUserInputDescription"><%= f.label :text %><br/><%= f.text_area :text %> </label>
            <button type="submit" class="btn btn-default"><%= f.submit %></button>
          </div>
    <% end %>
  </div>

当我单击提交时,它不会添加到我的数据库中。

我的问题是:如何从 index.html 中访问 CRUD?

感谢您的宝贵时间,我仍在学习 Rails/CRUD/ActiveRecord 和教程。

【问题讨论】:

  • 我假设article_params 应该是params[:article] ?
  • 进行了编辑,这是一个私有方法 def article_params(来自教程)
  • 我正在取得一些进展。尝试使用 post 'value1', :text => 'value2'), method: :post %> 的 link_to,但现在我被退回:缺少参数或值为空:article For params.require(:article).permit(:title, :text) 不知道为什么我的参数为空?

标签: html ruby-on-rails ruby ruby-on-rails-4 crud


【解决方案1】:

我把 f.submit 改成:

<%= button_to "Create", new_article_path(@article[:title], @article[:text]), method: :post %>

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-08-21
    • 1970-01-01
    • 2015-08-16
    • 1970-01-01
    • 1970-01-01
    • 2016-04-24
    相关资源
    最近更新 更多