Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- Create rails project and your database, skip test.
- cd to your project.
- bundle your gems
- Create your database.
- Test the server.
- Generate the controller.
- Configure your URL path and root the index.
- Generate your rails (app) controller {welcome index}.
- Go to or create an index.html.erb file in your views/welcome folder.
- Configure routes.rb and include resources :articles (this sets up your REST[Get, Post, Put/Patch, Delete])
- Root "welcome#index" to your site.
- Generate your rails controller [article](s)
- Define a (new) action in your just created [articles]controller.
- Make a new.html.erb file in /app/views/articles. Add an h1 title to display in browser.
- Build a form_for :article, url: articles_path do |f| in new.html.erb and check on server.
- Define a create action in articles_controller.
- Generate your rails model Article title:string text:text
- Run the migration.
- In articles_controller.rb add to the create action, 1) Set @article = Article.new(article_params) 2) @article.save 3)redirect_to @article
- Define a private article_params method, 1) params.require(:article).permit(:title, :text)
- Add the show action, 1) @article = Article.find(params[:id])
- Make a new file views/articles/show.html.erb add <p><strong>Title:</strong><%=@article.title %></p><p><strong>Text:</strong><%=@article.text%></p>
- In articles_controller.rb define index action, 1) @articles = Article.all
- Then add the view for that controller action in app/views/articles/index.html.erb <h1>Index of Articles</h1><table><tr><th>Title</th><th>Text</th></tr><% @articles.each do |article| %><tr><td><%= article.title %></td><td><%= article.text %></td></tr><% end %></table>
- In views/welcome/index.html.erb add <h1>Hello, Rails!</h1><%= link_to 'My Blog', controller: 'articles' %>
- In views/articles/index.html.erb above table tag add <%= link_to 'New Article', new_article_path %> &&&& below table tag add <%= link_to 'Back', articles_path %>
- In views/articles/show.html.erb add <%= link_to 'Back', articles_path %>
- In models/article.rb add vaidates :title, presence: true, length: { minimum: 5 }
- In controllers/articles_controller.rb add to the "new" action @article = Article.new &&&& add if @article.save redirect_to @article else render 'new' end
- In articles/new.html.erb add <% if @article.errors.any? %><div id="error_explanation"><h2><%= pluralize(@article.errors.count, "error") %> Prohibited this article from being saved: </h2><ul><% @article.errors.full_messages.each do |msg| %><li><%= msg %></li><% end %></ul></div><% end %> <%= link_to 'Back', articles_path %>
- In articles_controller.rb add below "new" action def edit @article = Article.find(params[:id]) end
- Make a new file articles/edit.html.erb & add <h1>Editing article</h1><%= form_for :article, url: article_path(@article), method: :patch 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.full_messages.each do |msg| %><li><%= msg %></li><% end %></ul></div><% end %><p><%= f.label :title %><br><%= f.text_field :title %></p><p><%= f.label :text %><br><%= f.text_area :text %></p><p><%= f.submit %></p><% end %><%= link_to 'Back', articles_path %>
- In articles_controller.rb add below "update" action def update @article = Article.find(params[:id]) if @article.update(article_params) redirect_to @article else render 'edit' end
- Add links to articles/index.html.erb below <%= article.text%> <td><%= link_to 'Show', article_path(article) %></td><td><%= link_to 'Edit', edit_article_path(article) %></td>
- Add link to articles/show.html.erb before "back" <%= link_to 'Edit', edit_article_path(@article) %> |
- Make a new file /articles/_form.html.erb & copy code from articles/new.html.erb minus the "url: articles_path" ////// add <%= form_for :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.full_messages.each do |msg| %><li><%= msg %></li><% end %></ul></div><% end %><p><%= f.label :title %><br><%= f.text_field :title %></p><p><%= f.label :text %><br><%= f.text_area :text %></p><p><%= f.submit %></p><% end %>
- Change new.html.erb & edit.html.erb to <h1>...</h1><%= render 'form' %><%= link_to 'Back', articles_path %>
- D\\ Define destroy action in articles_controller.rb & add @article = Article.find(params[:id]) @article.destroy redirect_to articles_path end
- Add a destroy link to articles/index.html.erb as <td><%= link_to 'Destroy', article_path(article), method: :delete, data: { confirm: 'Are you sure?' } %></td>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement