nao
nao

Reputation: 1188

Form creates object, but params not passed in

I'm trying to create a simple form submission to create posts for a blog-type site. I can't seem to get the form to submit the params. If I remove the validations in the model, I can create objects with unique IDs, but they're completely blank as far as the params go.

posts_controller.rb

 class PostsController < ApplicationController

def index
    @posts = Post.all
end

def show
    @post = Post.find(params[:id])
end

def new
    @post = Post.new
end

def create
    @post = Post.new(post_params)
    if @post.save
        redirect_to post_path(@post), :notice => "Post was created successfully."
    else
        render :new
    end
end

private
def post_params
    params.require(:post).permit(:id)
end

new.html.erb

  <%= form_for(@post) do |f| %>
  <% if @post.errors.any? %>
  <div id="error_explanation">
   <ul>
     <% @post.errors.full_messages.each do |msg| %>
     <div><%= msg %></div>
     <% end %>
    </ul>
</div>
<% end %>
 <% end %>

<%= form_for(@post) do |f| %>
  <ol class="formdivst">
  <%= f.label :title %><br />
  <%= f.text_field :title %>
  <%= f.label :content_md %><br />
  <%= f.text_field :content_md %>
<div>
  <%= f.file_field :image %>
  <div class="actions">
    <%= f.submit "Submit" %>
  </div>
</ol>
<% end %>

posts.rb (model)

class Post < ActiveRecord::Base
  # Use friendly_id
  extend FriendlyId
  friendly_id :title, use: :slugged

  # Markdown
  #before_save { MarkdownWriter.update_html(self) }

  # Validations
  validates :title, presence: true, length: { maximum: 100 }, uniqueness: true
  validates :content_md, presence: true

  # Pagination
  paginates_per 30

  # Relations
  belongs_to :user

  # Scopes
  scope :published, lambda {
    where(draft: false)
    .order("updated_at DESC")
  }

  scope :drafted, lambda {
    where(draft: true)
    .order("updated_at DESC")
  }

  has_attached_file :image, styles: { small: "64x64", med: "100x100", large: "200x200" }

end

routes.rb

app::Application.routes.draw do
  root "pages#home"
  resources :posts
  get "home", to: "pages#home", as: "home"
  get "inside", to: "pages#inside", as: "inside"
  get "/contact", to: "pages#contact", as: "contact"
  post "/emailconfirmation", to: "pages#email", as: "email_confirmation"

  devise_for :users

  namespace :admin do
    resources :posts
    root "base#index"
    resources :users
    get "posts/drafts", to: "posts#drafts", as: "posts_drafts"
    get "posts/dashboard", to: "posts#dashboard", as: "posts_dashboard"
  end

end

Upvotes: 1

Views: 67

Answers (1)

MrYoshiji
MrYoshiji

Reputation: 54882

The issue here is that you don't define the post_params as you should do (if using Rails 4 OR using the StrongParameters gem):

# posts_controller
def post_params
  params.require(:post).permit([:list_of_allowed_attributes_here])
end

I see you are using FriendlyId. In order to find back the record with it's friendly ID, you have to find it like this:

@post = Post.friendly.find(params[:id])

Because params[:id] is not a regular integer as Rails expects but a FriendlyId (string containing keywords and stuff).

Upvotes: 1

Related Questions