4

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).

Leon92
  • 505
  • 1
  • 9
  • 15

1 Answers1

6

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>
dimakura
  • 7,575
  • 17
  • 36
  • The requirement is that "Modify the form for post creation that allows a blogger to add a category to posts when they are created. To do this you will have to modify the new,create, and update methods in the posts_controller.rb file." I think it's not the usual choose one from a predefined category. I guess, it should be viewed as any other type of attribute, it's just an attribute that happens to be an object. – Leon92 Sep 05 '15 at 08:03
  • Can blogger create new categories? Or categories are created by administrator? – dimakura Sep 05 '15 at 08:11
  • The blogger creates a category, yes. – Leon92 Sep 05 '15 at 08:12
  • I reread your requirements and it does not says you need to allow blogger create categories on the same page as post. It's possible too, but it will require some Javascript. What it basically asks you (as I get it), add a combo box where blogger can select from pre-existing categories. Do you agree? If not, can you share your full requirements spec or at least share with us what are you doing? School project or is it customer's requirement (they do have strange ones)? – dimakura Sep 05 '15 at 08:41
  • 1
    It's a university tutorial. It specifies to generate a category model. With the following fields "Name, and description". Our final task is to add a category relation to our post, so we can identify each post to a category. To start with, we must add the relations to the appropriate model classes. To the category model, add: has_many :posts, and to the post model add: belongs_to :category. This establishes the relations in the model class, but we also need to add these to the database files. Here is the first for the post class: rails generate migration AddCategoryToPost category:references – Leon92 Sep 05 '15 at 08:55
  • Once you have generated both, make sure your database migrations are correct and run rake db:migrate. We now have relations between categories and posts. Modify the form for post creation that allows a blogger to add a category to posts when they are created. To do this you will have to modify the new, create, and update methods in the posts_controller.rb file. Once you have added this relation so that you can add a category to a past, be sure that you add the ability to display the category of a post in the full index page. – Leon92 Sep 05 '15 at 08:59
  • It's just a basic learn rails exercise, but I'm trying to understand what's going on. So, if we do not need the user to define the categories, only select from a predefined list. How should I go about doing that? – Leon92 Sep 05 '15 at 09:02
  • I've read the updates. Thank you, but how do I go about actually defining some categories? – Leon92 Sep 05 '15 at 10:09
  • Don't you have categories controller? You can also add them from rails console. – dimakura Sep 05 '15 at 10:16
  • Nope, I was sure we're not allowed one. But I might just add one anyway. How do I add it from the rails console? – Leon92 Sep 05 '15 at 10:20
  • `rails c` and then `Category.create(name: 'My Category, description: 'Some description')` – dimakura Sep 05 '15 at 10:21