user3131148 user3131148 - 7 months ago 38
Ruby Question

How do you create a rails app without using all of resource in create,update,delete?

In the rails guides tutorial creating a blog app after we create the rails app and create a resources in the routes then we start working on a form_for for creating a posts title and text in the guide it tells me that we need to add this line

<%= form_for :post, url: posts_path do |f| %>



the posts_path helper is passed to the :url option. What Rails will do with this is that it will point the form to the create action of the current controller, the PostsController, and will send a POST request to that route.


so what am trying to understand is the passing to 'create action' you see I have a simple app where i want is when a text is entered in the title field and the submit button is entered I want it to pass to the create action where I just out put the text in the create action view or another view, the rails guide goes through teaching the 'CRUD' but I just want to understand How to build an app that doesn't use 'CRUD' for instance an app that takes an input and outputs it in another view?

my form:

<h1>Here Lets create a simple post</h1>

<%= form_for :post, url: posts_path do|f| %>
<p>
<%= f.label :title %>
<%= f.text_field :title %>
</p>
<p>
<%= f.submit %>
</p>

<% end %>


posts controller:

class PostsController < ApplicationController

def new
@post = Post.new
end

def create
@post = Post.new(post_params)

end

def post_params
params_require(:post).permit(:title)
end
end


create view:

<h1>THis is the post create action</h1>

<%= @post.title %>


routes:

Learnnobase::Application.routes.draw do
resources :posts
root "welcome#home"
end


Right now am getting an error stating
uninitialized constant PostsController::Post
highlighting my create method? I've done so many rails app tutorials using 'CRUD' I really wanna learn building a simple app without using 'CRUD', I was trying to experiment with this app even though I do use the create action of "CRUD".

Answer

We generally use Rails to build database-backed applications, but for learning purposes, you can do it this way.

The problem you are facing here is: You are tyring to create an object of the Post class, that will be the model in the example you are referring to. The error comes up since you have not created the Post model.

To meet your requirement you can make your create action be:

def create
  @post = post_params #this will be a hash
end

Then change your view to:

<h1>THis is the post create action</h1>

<%= @post[:title] %>
Comments