thank you all for your help in advanced.
I'm an absolute beginner in rails and I'm trying to work through a tutorial.
The task is as follows. I have a post model built from a scaffold with only the content:string field.
Then a category model, not a scaffold etc. The idea is a category has_many :posts and the post belongs_to :category. A category has the fields name and description. This is fine and I understand this, I've added these to the model.
I've also run the migration
rails generate migration AddCategoryToPost category:references
How do I now enable the user to add a category when they make their post.
So the order of events is a user creates a post, where they can add a category, as the post is created. The category has name, and description that the user will need to define.
def new
@post = Post.new
end
def create
@category = Category.new(caregory_params)
@post = Post.new(post_params)
respond_to do |format|
if @post.save
format.html { redirect_to @post, notice: 'Post was successfully created.' }
format.json { render :show, status: :created, location: @post }
else
format.html { render :new }
format.json { render json: @post.errors, status: :unprocessable_entity }
end
end
end
How to I alter the new,create, and update methods of the post controller to achieve this, and thus what should the form contain to create a new post (and category).
Thank you very much for your help in advanced, I'm just not understanding how you would go about it as a category need is an 'object' and needs to be added to a post object (that needs to be added to the database).