Rails 4:从index.html.erb访问所有CRUD

时间:2014-10-11 23:38:59

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

我试图只创建一个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以及以下教程。

1 个答案:

答案 0 :(得分:0)

我将f.submit更改为:

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