Advertisement
Guest User

Untitled

a guest
Oct 31st, 2015
81
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.59 KB | None | 0 0
  1. Create rails project and your database, skip test.
  2. cd to your project.
  3. bundle your gems
  4. Create your database.
  5. Test the server.
  6. Generate the controller.
  7. Configure your URL path and root the index.
  8. Generate your rails (app) controller {welcome index}.
  9. Go to or create an index.html.erb file in your views/welcome folder.
  10. Configure routes.rb and include resources :articles (this sets up your REST[Get, Post, Put/Patch, Delete])
  11. Root "welcome#index" to your site.
  12. Generate your rails controller [article](s)
  13. Define a (new) action in your just created [articles]controller.
  14. Make a new.html.erb file in /app/views/articles. Add an h1 title to display in browser.
  15. Build a form_for :article, url: articles_path do |f| in new.html.erb and check on server.
  16. Define a create action in articles_controller.
  17. Generate your rails model Article title:string text:text
  18. Run the migration.
  19. In articles_controller.rb add to the create action, 1) Set @article = Article.new(article_params) 2) @article.save 3)redirect_to @article
  20. Define a private article_params method, 1) params.require(:article).permit(:title, :text)
  21. Add the show action, 1) @article = Article.find(params[:id])
  22. Make a new file views/articles/show.html.erb add <p><strong>Title:</strong><%=@article.title %></p><p><strong>Text:</strong><%=@article.text%></p>
  23. In articles_controller.rb define index action, 1) @articles = Article.all
  24. 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>
  25. In views/welcome/index.html.erb add <h1>Hello, Rails!</h1><%= link_to 'My Blog', controller: 'articles' %>
  26. 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 %>
  27. In views/articles/show.html.erb add <%= link_to 'Back', articles_path %>
  28. In models/article.rb add vaidates :title, presence: true, length: { minimum: 5 }
  29. In controllers/articles_controller.rb add to the "new" action @article = Article.new &&&& add if @article.save redirect_to @article else render 'new' end
  30. 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 %>
  31. In articles_controller.rb add below "new" action def edit @article = Article.find(params[:id]) end
  32. 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 %>
  33. 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
  34. 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>
  35. Add link to articles/show.html.erb before "back" <%= link_to 'Edit', edit_article_path(@article) %> |
  36. 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 %>
  37. Change new.html.erb & edit.html.erb to <h1>...</h1><%= render 'form' %><%= link_to 'Back', articles_path %>
  38. D\\ Define destroy action in articles_controller.rb & add @article = Article.find(params[:id]) @article.destroy redirect_to articles_path end
  39. 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