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).
You PostsController#create method probably looks like this now:
def create
@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
and post_params is something like:
def post_params
params.require(:post).permit(:title, :body)
end
I also assume you've already defined relationship between Category and Post and migrated database accordingly:
class Post < ActiveRecord::Base
belongs_to :category
end
class Category < ActiveRecord::Base
has_many :posts
end
What you require is to add ability to select Post's category on create and update. You need to make changes only in two places.
The first place is a form view/posts/_form.html.erb, where you add the following snippet inside form_for block:
<div class="field">
<%= f.label :category_id %>
<%= f.collection_select :category_id, Category.all, :id, :name %>
</div>
This will create a <select> tag with list of categories. Blogger can now select desired category when creating/updating his blog post.
The second place you need to make changes is post_params method in posts_controller:
def post_params
params.require(:post).permit(:title, :body, :category_id)
end
Here you just declared :category_id as a safe parameter.
You can check now. Your forms should be fully functional now.
Note You probably need to display category in posts list as well (views/posts/index.html.erb). You can add the following column to existing table:
<td><%= post.category && post.category.name %></td>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With