So I'm working on a social web app using cloud datastore and ruby on rails. I've completed the crud operations for the posting aspect of the app and I'm trying to crud operations for my comment aspect. I'm running into an error when I try to render my comments form in the individual post page. I get an error and I think it has something to do with either one of my controllers or my erb files. I've been looking at for a long time now so hopefully someone can shed some light on it. I tried this solution I found, but it didn't work. link
For reference, I'm basically following the Google Datastore Bookshelf Tutorial App. BookShelf Tutorial App
Here is my posts controller
class PostsController < ApplicationController
PER_PAGE = 10
def index
#post, #cursor = Post.query limit: PER_PAGE, cursor: params[:cursor]
end
def new
#post = Post.new
end
def create
#post = Post.new post_params
if #post.save
flash[:success] = "Posted"
redirect_to posts_path(#post)
else
render :new
end
end
def edit
#post = Post.find(params[:id])
end
def update
#post = Post.find(params[:id])
if #post.update post_params
flash[:success] = "Updated Book"
redirect_to posts_path(#post)
else
render :edit
end
end
def show
#post = Post.find params[:id]
end
def destroy
#post = Post.find(params[:id])
#post.destroy
redirect_to posts_path
end
private
def post_params
params.require(:post).permit(:title, :body)
end
end
Here is my comments controller
class CommentsController < ApplicationController
def create
#post = Post.find(params[:post_id])
#comment = #post.comments.create(comment_params)
redirect_to post_path(#post)
end
def destroy
#post = Post.find(params[:post_id])
#comment = #post.comments.find(params[:id])
#comment.destroy
redirect_to post_path(#post)
end
private
def comment_params
params.require(:comment).permit(:body)
end
end
Comment model
require "google/cloud/datastore"
class Comment
include ActiveModel::Model
attr_accessor :id, :body
# Return a Google::Cloud::Datastore::Dataset for the configured dataset.
# The dataset is used to create, read, update, and delete entity objects.
def self.dataset
#dataset ||= Google::Cloud::Datastore.new(
project: Rails.application.config.
database_configuration[Rails.env]["dataset_id"]
)
end
def to_entity
post.parent = dataset.key "Post"
entity = Google::Cloud::Datastore::Entity.new
entity.key = post
entity["body"] = body
entity
end
# [START from_entity]
def self.from_entity entity
comment = Comment.new
comment.id = entity.key.id
entity.properties.to_hash.each do |name, value|
comment.send "#{name}=", value if comment.respond_to? "#{name}="
end
comment
end
# [END from_entity]
# Save the book to Datastore.
# #return true if valid and saved successfully, otherwise false.
def save
if valid?
entity = to_entity
Book.dataset.save entity
self.id = entity.key.id
true
else
false
end
end
def persisted?
id.present?
end
Post Model
require "google/cloud/datastore"
class Post
include ActiveModel::Model
attr_accessor :id, :title, :body
# Return a Google::Cloud::Datastore::Dataset for the configured dataset.
# The dataset is used to create, read, update, and delete entity objects.
def self.dataset
#dataset ||= Google::Cloud::Datastore.new(
project: Rails.application.config.
database_configuration[Rails.env]["dataset_id"]
)
end
# Query Book entities from Cloud Datastore.
#
# returns an array of Book query results and a cursor
# that can be used to query for additional results.
def self.query options = {}
query = Google::Cloud::Datastore::Query.new
query.kind "Post"
query.limit options[:limit] if options[:limit]
query.cursor options[:cursor] if options[:cursor]
results = dataset.run query
posts = results.map {|entity| Post.from_entity entity }
if options[:limit] && results.size == options[:limit]
next_cursor = results.cursor
end
return posts, next_cursor
end
def to_entity
entity = Google::Cloud::Datastore::Entity.new
entity.key = Google::Cloud::Datastore::Key.new "Post", id
entity["title"] = title
entity["body"] = body
entity
end
# [START from_entity]
def self.from_entity entity
post = Post.new
post.id = entity.key.id
entity.properties.to_hash.each do |name, value|
post.send "#{name}=", value if post.respond_to? "#{name}="
end
post
end
# [END from_entity]
# [START find]
# Lookup Post by ID. Returns Post or nil.
def self.find id
query = Google::Cloud::Datastore::Key.new "Post", id.to_i
entities = dataset.lookup query
from_entity entities.first if entities.any?
end
# [END find]
# [START validations]
# Add Active Model validation support to Book class.
include ActiveModel::Validations
validates :title, presence: true
# [END validations]
# [START update]
# Set attribute values from provided Hash and save to Datastore.
def update attributes
attributes.each do |name, value|
send "#{name}=", value if respond_to? "#{name}="
end
save
end
# [END update]
# [START destroy]
def destroy
Post.dataset.delete Google::Cloud::Datastore::Key.new "Post", id
end
# [END destroy]
# Save the book to Datastore.
# #return true if valid and saved successfully, otherwise false.
def save
if valid?
entity = to_entity
Post.dataset.save entity
self.id = entity.key.id
true
else
false
end
end
def persisted?
id.present?
end
end
My show.html.erb file
<h1><%= #post.title %></h1>
<p><%= #post.body %></p>
<hr>
<%= link_to 'Edit', edit_post_path(#post), :class => 'btn btn-default' %>
<%= link_to 'Delete', post_path(#post),
method: :delete,
data: {confirm: 'Are You Sure?'},
:class => 'btn btn-danger' %>
<%= render 'comments/form'%>
This is the comments html erb file
<h3>Comments</h3>
<%= #post.comment.each do |comment| %>
<div class="well">
</p><%= link_to '[X]', [comment.post, comment],
method: :delete,
data: {confirm: 'Are You Sure?'} %></p>
</div>
<% end %>
This is the error log
ActionView::Template::Error (undefined method `comment' for #<Post:0xbb05498>):
1: <h3>Add Comment</h3>
2: <%= form_for([#post, #post.comment.build]) do |f| %>
3: <div class="form-group">
4: <%= f.label:body %><br>
5: <%= f.text_area(:body, {:class => 'form-control'}) %>
app/views/comments/_form.html.erb:2:in `_app_views_comments__form_html_erb__676677109_98008740'
app/views/posts/show.html.erb:12:in `_app_views_posts_show_html_erb___922952118_98050956'
Related
So I've been building an app using rails and the google datastore. I keep encountering an error in my new.html.erb file where I get the NoMethodError.
I've scoured looking for a solution to my problem and it's most likely a simple typo somewhere in my code. I've been staring at the code for hours so maybe some fresh new eyes can help me figure it out
This is my posts_controller.rb
class PostsController < ApplicationController
PER_PAGE = 10
def index
#post, #cursor = Post.query limit: PER_PAGE, cursor: params[:cursor]
end
def new
#post = Post.new
end
def create
#post = Post.new post_params
if #post.save
flash[:success] = "Posted"
redirect_to posts_path(#post)
else
render :new
end
end
def edit
#post = Post.find(params[:id])
end
def update
#post = Post.find(params[:id])
if #post.update(post_params)
redirect_to #post
else
render 'edit'
end
end
def show
#post = Post.find(params[:id])
end
def destroy
#post = Post.find(params[:id])
#post.destroy
redirect_to posts_path
end
private
def post_params
params.require(:post).permit(:title, :body)
end
end
This is my new.html.erb
<h1>Create Post</h1>
<%= form_for #post do |f| %>
<% if #post.errors.any? %>
<% #post.errors.full_messages.each do |msg| %>
<div class="alert alert danger"><%= msg %></div>
<% end %>
<% end %>
<div class="form-group">
<%= f.label:title %><br>
<%= f.text_field(:title, {:class => 'form-control'}) %>
</div>
<div class="form-group">
<%= f.label:body %><br>
<%= f.text_area(:body, {:class => 'form-control'}) %>
</div>
<p>
<%= f.submit({:class => 'btn btn-primary'}) %>
</p>
<% end %>
This is my model post.rb
class Post
attr_accessor :title, :body
# Return a Google::Cloud::Datastore::Dataset for the configured dataset.
# The dataset is used to create, read, update, and delete entity objects.
def self.dataset
#dataset ||= Google::Cloud::Datastore.new(
project: Rails.application.config.
database_configuration[Rails.env]["dataset_id"]
)
end
# Query Book entities from Cloud Datastore.
#
# returns an array of Book query results and a cursor
# that can be used to query for additional results.
def self.query options = {}
query = Google::Cloud::Datastore::Query.new
query.kind "Post"
query.limit options[:limit] if options[:limit]
query.cursor options[:cursor] if options[:cursor]
results = dataset.run query
posts = results.map {|entity| Book.from_entity entity }
if options[:limit] && results.size == options[:limit]
next_cursor = results.cursor
end
return posts, next_cursor
end
# [START from_entity]
def self.from_entity entity
post = Post.new
post.id = entity.key.id
entity.properties.to_hash.each do |name, value|
post.send "#{name}=", value if post.respond_to? "#{name}="
end
post
end
# [END from_entity]
# [START find]
# Lookup Book by ID. Returns Book or nil.
def self.find id
query = Google::Cloud::Datastore::Key.new "Post", id.to_i
entities = dataset.lookup query
from_entity entities.first if entities.any?
end
# [END find]
# [START validations]
# Add Active Model validation support to Book class.
include ActiveModel::Validations
validates :title, presence: true
# [END validations]
# Save the book to Datastore.
# #return true if valid and saved successfully, otherwise false.
def save
if valid?
entity = to_entity
Post.dataset.save entity
self.id = entity.key.id
true
else
false
end
end
end
routes.rb
Rails.application.routes.draw do
get 'auth/:provider/callback', to: 'sessions#create'
get 'auth/failure', to: redirect('/')
get 'signout', to: 'sessions#destroy', as: 'signout'
resources :sessions, only: [:create, :destroy]
resource :main, only: [:show]
resources :posts
root to: 'posts#index', as: "home"
# For details on the DSL available within this file, see http://guides.rubyonrails.org/routing.html
end
EDIT: This is the error log that I'm getting
ActionView::Template::Error (undefined method `to_key' for #<Post:0x2ae2c68>
Did you mean? to_query):
1: <h1>Create Post</h1>
2: <%= form_for #post do |f| %>
3: <% if #post.errors.any? %>
4: <% #post.errors.full_messages.each do |msg| %>
5: <div class="alert alert danger"><%= msg %></div>
app/views/posts/new.html.erb:2:in `_app_views_posts_new_html_erb__241418705_22466964'
Your Post Model is a plain ruby object but you treat it like an ActiveModel/ActiveRecord object.
Try adding
include ActiveModel::Model inside the Post model like so:
class Post
include ActiveModel::Model
...
end
You have defined #posts in new method but you are using #post in the new.html.erb. Which is the reason for the error. Keep the same name either #posts or #post
So I'm working on a web app using rails and the google cloud datastore. I'm relatively new at coding web apps. Most of the errors I'm running into come from trying to integrate the datastore with the rails MVC model. For further context most of my code is based of the Google Datastore Bookshelf tutorial found on the datastore information page. https://cloud.google.com/ruby/getting-started/using-cloud-datastore
So recently I ran into a NameError in PostsController#create when I was trying to save a post to the datastore database. I can't seem to find the cause of this issue so hopefully someone can be my new set of eyes or provide some insight.
Also if you know any places where I can find helpful documentation using datastore and rails. That would be appreciated
So this is my post_controller.rb file
class PostsController < ApplicationController
PER_PAGE = 10
def index
#post, #cursor = Post.query limit: PER_PAGE, cursor: params[:cursor]
end
def new
#post = Post.new
end
def create
#post = Post.new post_params
if #post.save
flash[:success] = "Posted"
redirect_to posts_path(#post)
else
render :new
end
end
def edit
#post = Post.find(params[:id])
end
def update
#post = Post.find(params[:id])
if #post.update(post_params)
redirect_to #post
else
render 'edit'
end
end
def show
#post = Post.find(params[:id])
end
def destroy
#post = Post.find(params[:id])
#post.destroy
redirect_to posts_path
end
private
def post_params
params.require(:post).permit(:title, :body)
end
end
This is my model for the post post_rb
require "google/cloud/datastore"
class Post
include ActiveModel::Model
attr_accessor :title, :body
# Return a Google::Cloud::Datastore::Dataset for the configured dataset.
# The dataset is used to create, read, update, and delete entity objects.
def self.dataset
#dataset ||= Google::Cloud::Datastore.new(
project: Rails.application.config.
database_configuration[Rails.env]["dataset_id"]
)
end
# Query Book entities from Cloud Datastore.
#
# returns an array of Book query results and a cursor
# that can be used to query for additional results.
def self.query options = {}
query = Google::Cloud::Datastore::Query.new
query.kind "Post"
query.limit options[:limit] if options[:limit]
query.cursor options[:cursor] if options[:cursor]
results = dataset.run query
posts = results.map {|entity| Post.from_entity entity }
if options[:limit] && results.size == options[:limit]
next_cursor = results.cursor
end
return posts, next_cursor
end
# [START from_entity]
def self.from_entity entity
post = Post.new
post.id = entity.key.id
entity.properties.to_hash.each do |name, value|
post.send "#{name}=", value if post.respond_to? "#{name}="
end
post
end
# [END from_entity]
# [START find]
# Lookup Book by ID. Returns Book or nil.
def self.find id
query = Google::Cloud::Datastore::Key.new "Post", id.to_i
entities = dataset.lookup query
from_entity entities.first if entities.any?
end
# [END find]
# [START validations]
# Add Active Model validation support to Book class.
include ActiveModel::Validations
validates :title, presence: true
# [END validations]
# Save the book to Datastore.
# #return true if valid and saved successfully, otherwise false.
def save
if valid?
entity = to_entity
Post.dataset.save entity
self.id = entity.key.id
true
else
false
end
end
end
This is my new.html.erb file that i use to call the save method in datastore
<h1>Create Post</h1>
<%= form_for #post do |f| %>
<% if #post.errors.any? %>
<% #post.errors.full_messages.each do |msg| %>
<div class="alert alert danger"><%= msg %></div>
<% end %>
<% end %>
<div class="form-group">
<%= f.label:title %><br>
<%= f.text_field(:title, {:class => 'form-control'}) %>
</div>
<div class="form-group">
<%= f.label:body %><br>
<%= f.text_area(:body, {:class => 'form-control'}) %>
</div>
<p>
<%= f.submit({:class => 'btn btn-primary'}) %>
</p>
<% end %>
This is my routes.rb file
Rails.application.routes.draw do
get 'auth/:provider/callback', to: 'sessions#create'
get 'auth/failure', to: redirect('/')
get 'signout', to: 'sessions#destroy', as: 'signout'
resources :sessions, only: [:create, :destroy]
resource :main, only: [:show]
resources :posts
root to: 'posts#index', as: "home"
# For details on the DSL available within this file, see http://guides.rubyonrails.org/routing.html
end
EDIT: Forgot to include the error message log
NameError (undefined local variable or method `to_entity' for #<Post:0x65b0d70>
Did you mean? to_key):
app/models/post.rb:72:in `save'
app/controllers/posts_controller.rb:16:in `create'
NOTE: The create.html.erb is blank so I choose not to include it
Okay, it looks like you are missing a to_entity method. Try adding something similar to this for your post model. I slightly modified the code from the Book documentation example here. You will want to read that entire file to see what their Book model looks like with all of the code.
def to_entity
entity = Google::Cloud::Datastore::Entity.new
entity.key = Google::Cloud::Datastore::Key.new "Post", id
entity["title"] = title
entity["body"] = body
entity
end
I'm a bit of a rails newb here and need some help figuring this out. I have an argument error that is thrown every time I try and create or edit a new "topic."
ArgumentError
Here is the code for the "show:"
<h1><%= #topic.title %></h1>
<%= link_to "Edit", edit_topic_path(#topic), class: 'btn btn-success' %>
<%= link_to "Delete Topic", #topic, method: :delete, class: 'btn btn-danger', data: {confirm: 'Are you sure you want to delete this topic?'} %>
<% if policy(Bookmark.new).create? %>
<%= link_to "New Bookmark", new_topic_bookmark_path(#topic), class: 'btn btn-success' %>
<% end %>
<% #topic.bookmarks.each do |bookmark| %>
<div class="media-body">
<div class="row">
<div class="col-md-2">
<div class="container">
<img src="http://icons.better-idea.org/icon?url=<%= bookmark.url %>&size=120">
<div class="media-heading">
<%= link_to bookmark.name, topic_bookmark_path(#topic, bookmark) %>
</div>
</div>
</div>
</div>
<div class="col-md-1">
<%= render partial: 'likes/like', locals: {bookmark: bookmark} %>
</div>
</div>
<% end %>
Here is the "topics" controller:
class TopicsController < ApplicationController
def index
#topics = Topic.all
end
def show
#topic = Topic.find(params[:id])
end
def new
#topic = Topic.new
end
def create
#topic = Topic.new(topic_params)
if #topic.save
flash[:notice]= "Topic was saved."
redirect_to #topic
else
flash.now[:alert]= "The topic could not be saved. Please try again"
render :new
end
end
def edit
#topic = Topic.find(params[:id])
end
def update
#topic = Topic.find(params[:id])
#topic.assign_attributes(topic_params)
if #topic.save
flash[:notice]= "The topic was saved sucessfully."
redirect_to #topic
else
flash.now[:alert]= "There was an error saving the topic. Please try again."
render :edit
end
end
def destroy
#topic = Topic.find(params[:id])
if #topic.destroy
flash[:notice]= "\"#{#topic.title}\" was deleted successfully."
redirect_to topics_path
else
flash.now[:alert] = "There was an error in deleting this topic."
render :show
end
end
def topic_params
params.require(:topic).permit(:title)
end
end
Update(1): New error after deleting "policy" New Error
Here is the "application policy that uses Pundit:
class ApplicationPolicy
attr_reader :user, :record
def initialize(user, record)
raise Pundit::NotAuthorizedError, "must be logged in" unless user
#user = user
#record = record
end
def index?
false
end
def show?
scope.where(:id => record.id).exists?
end
def create?
user.present?
end
def new?
create?
end
def update?
user.present? && ( record.user == user )
end
def edit?
update?
end
def destroy?
user.present? && (record.user == user)
end
def scope
Pundit.policy_scope!(user, record.class)
end
class Scope
attr_reader :user, :scope
def initialize(user, scope)
#user = user
#scope = scope
end
def resolve
scope
end
end
end
Problem is with Policy made with the Pundit gem. Can you check something called the BookmarkPolicy or something similar, or at least post that here. Did you forget to include Pundit in your controller ?
You're using the Pundit gem but I don't see the authorize method in your controller. From Pundit's documentation:
Supposing that you have an instance of class Post, Pundit now lets you
do this in your controller:
def update
#post = Post.find(params[:id])
authorize #post
if #post.update(post_params)
redirect_to #post
else
render :edit
end
end
The authorize method automatically infers that Post will have a
matching PostPolicy class, and instantiates this class, handing in the
current user and the given record.
The source code in the screenshot and the code you posted is not the same. Your code:
<% if policy(Bookmark.new).create? %>
Screenshot:
<% if (Bookmark.new).create? %>
Rails is correctly reporting that Bookmark.new does not have a create? method, because it's missing the policy() method call.
Is your file saved? Are you sure you're changing the correct file?
EDIT: I managed to delete! i had to define teh instance variable #movies = Movie.find(params[:id]) to the delete method in the controller.
I still can't update though. I get "param is missing or the value is empty: movie"
I forgot to add my contrller! sorry!
I'm trying to replicate one of my in class exercises into another app, for practice.
The idea is to be able to add new movies into a database, and get the option to update their info, and delete them as well. I can add new content, but I can't update them or delete them.
Appreciate any inputs I can get.
Routes.rb
Rails.application.routes.draw do
root "movies#index"
get "/movies", to: "movies#index"
get "/movies/new", to: "movies#new", as: "new_movie"
get '/movies/:id', to: "movies#movie_page", as: "movie_page"
post "/movies", to: "movies#add"
get "/movies/:id/edit", to: "movies#edit", as: "movie_edit"
put "/movies/:id", to: "movies#update"
patch "/movies/:id", to: "movies#update", as: "update_movie"
delete "/movies/:id", to: "movies#delete", as: "delete_movie"
end
Controller
class MoviesController < ApplicationController
def index
#movies = Movie.all
end
def movie_page
#movies = Movie.find(params[:id])
end
def new
#movies = Movie.new
end
def add
#movies = Movie.create(movie_params)
redirect_to movies_path
end
def edit
#movies = Movie.find(params[:id])
end
def update
#movies.update(movie_params)
redirect_to #movies, notice: "Shirt was updated."
end
def delete
#movies = Movie.find(params[:id])
#movies.destroy
# flash[:notice] = "Shirt was deleted."
redirect_to root_path, notice: "Shirt was deleted."
end
def movie_params
params.require(:movie).permit(:title, :description, :year_released)
end
# def set_movie
# #movies = Movie.find(params[:id])
# end
end
Form partial
<%= form_for #movies do |m| %>
<p>
<%= m.label :title %><br>
<%= m.text_field :title %>
</p>
<p>
<%= m.label :description %><br>
<%= m.text_field :description %>
</p>
<p>
<%= m.label :year_released %><br>
<%= m.text_field :year_released %>
</p>
<p>
<%= m.submit %>
</p>
<% end %>
Movie page html (individual movies, labeled by IDs)**I can't update or Delete, no route matches Delete.
When I press Update - I get param is missing or the value is empty: movie
<h1><%= #movies.title %></h1>
<h2>Released on : <%= #movies.year_released %> </h2>
<p> <%= #movies.description %> </p>
<%= link_to "Update", movie_edit_path(#movies) %>
<%= link_to "Delete", movies_path, method: :delete %
Edit page *I cant access this link. the form is the problem
<h1>Edit <%= #movies.title %> Info </h1>
<%= render "form" %>
<%= link_to "Cancel Edit", movie_edit_path(#movies) %>
Many thanks guys
def update
#movie = Move.find(params[:id])
#movie.update(movie_params)
redirect_to movie_path(#movie)
end
on your routes. all you need is resources :movies
you are getting param is empty because you have to pass in the id of the movie to update.
The major issue is that you do not load the variable #movies from the DB before you use it.
def update
#movies.update(movie_params)
redirect_to #movies, notice: "Shirt was updated."
end
def update
#movies.find(params[:id])
#movie.update(movie_params)
redirect_to #movies, notice: "Shirt was updated."
end
Aside from that you have tons of duplication and quite a few idiosyncrasies.
Rails uses these naming conventions for actions:
index
show (not movie_page)
new
create (not add)
edit
update
destroy (not delete)
You should follow them unless you have a damn good reason not to.
class MoviesController < ApplicationController
# cuts the duplication
before_filter :set_movie, except: [:new, :index]
def index
#movies = Movie.all
end
# GET /movies/:id
def show
end
# GET /movies/new
def new
#movie = Movie.new
end
# POST /movies
def create
#movie = Movie.create(movie_params)
redirect_to movies_path
end
# GET /movies/edit
def edit
end
# PUT|PATCH /movies/:id
def update
#movie.update(movie_params)
redirect_to #movie, notice: "Shirt was updated."
end
# DELETE /movies/:id
def destroy
#movie.destroy
redirect_to action: :index
end
private
def movie_params
params.require(:movie).permit(:title, :description, :year_released)
end
def set_movie
# Use the singular form when naming a variable with a single record
# failure to do so may result in tarring and feathering
#movie = Movie.find(params[:id])
end
end
All,
I just created markdown_title and markdown_body methods. When I go to the show view page for my Post model I am getting the error: Wrong number of arguments.
I believe my markdown_title method(also for markdown_body) might be constructed incorrectly below in the post.rb file. Is this the culprit?:
class Post < ActiveRecord::Base
has_many :comments
has_one :summary
belongs_to :user
belongs_to :topic
#has_one :summary
default_scope {order('created_at DESC')}
validates :title, length: {minimum: 5}, presence: true
validates :body, length: {minimum: 20}, presence: true
validates :topic, presence: true
validates :user, presence: true
def markdown_title
(render_as_markdown).render(self.title).html_safe
end
def markdown_body
(render_as_markdown).render(self.body).html_safe
end
private
def render_as_markdown
renderer = Redcarpet::Render::HTML.new
extensions = {fenced_code_blocks: true}
redcarpet = Redcarpet::Markdown.new(renderer, extensions)
#return redcarpet
end
end
Here is my code for my show.html.erb file where the error is appearing while calling my markdown_title method:
<h1><%= #post.markdown_title #post.title %></h1>
<div class="row">
<div class="col-md-8">
<p><%= #post.body %></p>
</div>
<div class="col-md-4">
<% if policy(#post).edit? %>
<%= link_to "Edit", edit_topic_post_path(#topic, #post), class: 'btn btn-success' %>
<% end %>
</div>
</div>
<% if #post.summary.present? %>
<h1>Post Summary</h1>
<p><%= #post.summary.body %></p>
<% else %>
<%= form_for [#topic, #post, #post.build_summary] do |f| %>
<%= f.text_field :body %>
<%= f.submit %>
<% end %>
<% end %>
This is the Post controller:
class PostsController < ApplicationController
#def index #for the index page
##posts = Post.all
#authorize #posts
#end
def show
#post = Post.find(params[:id])
#topic = Topic.find(params[:topic_id])
end
def new
#topic = Topic.find(params[:topic_id])
#post = Post.new
authorize #post #authorize() will check the policy on new post resources
# if user is present it wll let it render if no user present itll give exception
end
def create
##post = Post.new(params.require(:post).permit(:title, :body))
#require and permit make sure only certain keys are passed to Post.new
#topic = Topic.find(params[:topic_id])
##post = current_user.posts.build(params.require(:post).permit(:title, :body))
#post = current_user.posts.build(post_params)
#post.topic = #topic
authorize #post #authorize() will check if user is logged in if not itll give an exception
if #post.save
flash[:notice] = "Your new post was created and saved."
redirect_to [#topic, #post] #takes you to the new post you created
else
flash[:error] = "There was an error saving the post. Please try again."
render :new # it grabs the new.html.erb file and pastes it in the view
end
end
def edit
#topic = Topic.find(params[:topic_id])
#post = Post.find(params[:id])
authorize #post
end
def update
#topic = Topic.find(params[:topic_id])
#post = Post.find(params[:id])
##post_check = current_user.posts.build(post_params)
authorize #post
if #post.update_attributes(post_params)
flash[:notice] = "Post was updated and captured your new update."
redirect_to [#topic, #post]
else
flash[:error] = "There was an error saving the post. Please try again."
render :new
end
end
private
def post_params
params.require(:post).permit(:title, :body)
end
end
Here is the error I get on my view:
You're calling your markdown_title method with a parameter, in this case, #post.title.
<h1><%= #post.markdown_title #post.title %></h1>
In the definition of your Post class, the markdown_title method doesn't take any parameters.
def markdown_title
(render_as_markdown).render(self.title).html_safe
end
That's why you're seeing the Wrong number of arguments (1 for 0) error.
Since you're already referencing self.title in the markdown_title method, there's no reason to pass #post.title to it. Just remove #post.title from where you're calling markdown_title, and you should be good to go.