Mistake with my routes for uploading an image - ruby-on-rails

I'm trying to solve a really simple problem with my code. I want to upload image into a post, I use paperclip, and the last step is not working.
That is my controller :
class PostsController < ApplicationController
def index
#posts = Post.all
end
def new
#post = Post.new
end
def create
#post = Post.new(post_params)
if #post.save
flash[:success] = "uccess!"
redirect_to post_path(#post)
else
flash[:error] = #post.errors.full_messages
redirect_to new_post_path
end
end
def show
#post = Post.find(params[:id])
end
private
def post_params
params.require(:post).permit(:title, :image, :prix, :adress, :description)
end
end
that my form :
<%= simple_form_for #post, url: root_path do |f|%>
<%= f.input :title, label: "Nom du plat" %>
<br>
<%= f.input :image, as: :file %>
<br>
<%= f.input :prix %>
<%= f.input :adress, label: "Localisation" %>
<%= f.input :description %>
<br>
<%= f.button :submit %>
<% end %>
and that my view :
<%= image_tag (#post.image.url(:medium)) %>
<br>
<%= #post.description %>
<br>
<button>
<%= link_to "Home", root_path %>
</button>
So if you go through, and you spot a stupid mistake, please let me know.

You're calling root_path as the helper in your form when you need to call the route for post. Run rake routes and look for the create action related to post, which should be the same as the get route for the show action.
This should not be root_path:
<%= simple_form_for #post, url: root_path do |f|%>

Related

ForbiddenAttributesError in Rails 5

I am getting Forbidden Attributes Error even though I have used strong parameters in Rails. I have two models: Posts and Categories.
Here is my Post Controller:
class PostsController < ApplicationController
def index
#posts=Post.all
end
def new
#post=Post.new
#category=Category.all
end
def create
#post = Post.new(params[:post])
if #post.save
redirect_to posts_path,:notice=>"Post saved"
else
render "new"
end
end
def allowed_params
params.require(:post).permit(:title, :body, :category_id)
end
end
And here is my view for posts/new:
<%= form_for #post do |f| %>
<p>
<%= f.label :title %></br>
<%= f.text_field :title%><br/>
</p>
<p>
<%= f.label :body %></br>
<%= f.text_area :body%><br/>
</p>
<p>
<%= f.select :category_id, Category.all.collect{|x| [x.name,x.id]},{:include_blank =>"Select one"}%><br/>
</p>
<p>
<%= f.submit "Add Post" %>
</p>
<% end %>
But I am still getting Error.
You need to use allowed_params instead of params[:post]:
#post = Post.new(allowed_params)

how to edit comment within article's page - keep getting error about nil

I have typical article and comment blog format. It's a typical has_many/belongs_to, ie, the Guide's blog type.
However, I'm trying to edit the comment within the article, and I'm clueless about building the right form for this.
It's also in a partial which makes it more complicated for me.
Any help and/or educating me would be appreciated.
Comment's model
class Comment < ApplicationRecord
belongs_to :article
end
Article's model
class Article < ApplicationRecord
has_many :comments
validates :title, presence: true, length: { minimum: 5}
end
Article's Show page
<p>
<strong>Title:</strong>
<%= #article.title %>
</p>
<p>
<strong>Text:</strong>
<%= #article.text %>
</p>
<h2>Comments</h2>
<%= render #article.comments %>
<h2>Add a comment:</h2>
<%= render 'comments/form' %>
<%= link_to 'Edit', edit_article_path(#article) %> |
<%= link_to 'Back', articles_path %>
Comment's _comment.html.erb page
<p>
<strong>Commenter:</strong>
<%= comment.commenter %>
</p>
<p>
<strong>Comment:</strong>
<%= comment.body %>
</p>
<p>
<%= link_to 'Destroy Comment', [comment.article, comment],
method: :delete,
data: { confirm: 'Are you sure?' } %>
</p>
<p>
<%= link_to 'Edit', edit_article_comment_path(#article, comment) %>
</p>
Comment's _form.html
<%= form_for([#article, #comment]) do |f| %>
<p>
<%= f.label :commenter %><br>
<%= f.text_field :commenter %>
</p>
<p>
<%= f.label :body %><br>
<%= f.text_area :body %>
</p>
<p>
<%= f.submit %>
</p>
<% end %>
Error
It comes from Comment's _form.html.erb page in reference to this line: <%= form_for([#article, #comment]) do |f| %> ... and the error is: First argument in form cannot contain nil or be empty ...
Articles Controller
class ArticlesController < ApplicationController
def index
#articles = Article.all
end
def new
#article = Article.new
end
def edit
#article = Article.find(params[:id])
end
def create
#render plain: params[:article].inspect
##article = Article.new(params[:article])
##article = Article.new(params.require(:article).permit(:title, :text))
#article = Article.new(article_params)
if #article.save
redirect_to #article
else
render 'new'
end
end
def update
#article = Article.find(params[:id])
if #article.update(article_params)
redirect_to #article
else
render 'edit'
end
end
def show
#article = Article.find(params[:id])
end
def destroy
#article = Article.find(params[:id])
#article.destroy
redirect_to articles_path
end
private
def article_params
params.require(:article).permit(:title, :text)
end
end
Comments Controller
class CommentsController < ApplicationController
def create
#article = Article.find(params[:article_id])
#comment = #article.comments.create(comment_params)
redirect_to article_path(#article)
end
def destroy
#article = Article.find(params[:article_id])
#comment = #article.comments.find(params[:id])
#comment.destroy
redirect_to article_path(#article)
end
private
def comment_params
params.require(:comment).permit(:commenter, :body)
end
end
First argument in form cannot contain nil or be empty
The error is due to #article and #comment being nil in the form. Since you are rendering the form with a partial, you need send variables to the partial as well
Change <%= render 'comments/form' %> to <%= render 'comments/form', article: #article, comment: #comment %>
and in the form have this <%= form_for([article, comment]) do |f| %>
Also in the show method of articles controller, you should define #comment
def show
#article = Article.find(params[:id])
#comment = Comment.new
end

Trying to to add a nested post with the ancestry gem

I have topics and posts and they both have relationships. I've been following the guide on how to add a nested post but when i try to reply to a post i get Couldn't find Topic with 'id'=.
In my Posts controller
def create
#topic = Topic.find params[:topic_id]
#post = Post.new(post_params)
#post.user_id = current_user.id
#post.topic_id = #topic.id
if #post.save
redirect_to #topic
else
render :new
end
end
In my Topics controller
def show
#topic = Topic.find params[:id]
#post = Post.new(:parent_id => params[:parent_id])
#posts = #topic.posts
#topic.punch(request)
end
I'll keep it short for how i have my reply button in my topics/show.html.erb page
<% #posts.each do |post| %>
<%= link_to "Reply", new_topic_post_path(#topic, :parent_id => post) %>
<% end %>
Now this is my form
<%= simple_form_for [#topic, #post] do |f| %>
<%= f.hidden_field :parent_id %>
<%= f.input :content, as: :pagedown, input_html: { preview: true, rows: 10 }, label: 'Markdown' %>
<%= f.submit "Post", class: 'button expanded' %>
<% end %>
#Kristján Answered my question. I was missing params[:topic_id]

Variables not displaying in rails blog post

I'm building a simple blog tool using rails and having trouble with variables displaying from a form. What is most confusing is that the post times are showing correctly, but the title and text aren't coming through.
My new form page looks like:
<h1>New post</h1>
<%= form_for :blog, url: blogs_path do |f| %>
<p>
<%= f.label :title %><br>
<%= f.text_field :title %>
</p>
<p>
<%= f.label :body %><br>
<%= f.text_area :body %>
</p>
<p>
<%= f.submit %>
</p>
<% end %>
The controller is currently:
class BlogsController < ApplicationController
def index
end
def new
end
def create
#blog = Blog.new(post_params[:id])
#blog.save
redirect_to #blog
end
def show
#blog = Blog.find(params[:id])
end
private
def post_params
params.require(:blog).permit(:title, :body)
end
end
And the show page is:
<div id="blog">
<h1>
<%= #blog.title %>
</h1>
<p class="date">
Submitted <%= time_ago_in_words(#blog.created_at) %> ago
</p>
<p>
<%= #blog.body %>
</p>
</div>
Any ideas?
Change:
def create
#blog = Blog.new(post_params[:id])
#blog.save
redirect_to #blog
end
to:
def create
#blog = Blog.new(post_params)
#blog.save
redirect_to #blog
end
The reason why it wasn't working is that you were only trying to savepost_params[:id]. You need to pass the whole param as an argument when creating a new blog post Blog.new(post_params)

Show user's email when commenting rather than asking for input

I created a blog in which users can create comments under posts. Registered users, Admins and Guests will be able to post comments.
So in the _form.html.erb I wrote this:
<%= simple_form_for([#post, #post.comments.build], html: {class: 'form-horizontal' }) do |f| %>
<% if current_user || current_admin %>
<% #comment.commenter = current_user.try(:email) || current_admin.try(:email) %>
<% else %>
<%= f.input :commenter %>
<% end %>
<%= f.input :body %>
<%= f.button :submit %>
<% end %>
However I get this error: undefined local variable or method `comment'.
When I try to change #comment.commenter to #post.comments I get the error: undefined method `each' for "example#person.com":String.
Is there a way set the commenter's name if its registered. Like in the controller maybe?
The controller code is:
class CommentsController < ApplicationController
def create
#post = Post.find(params[:post_id])
#comment = #post.comments.create(comments_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 comments_params
params.require(:comment).permit(:commenter, :body)
end
end
If you need any other information please let me know.
Thanks
<%= simple_form_for(#comment),url: [#post,#comment], html: {class: 'form-horizontal' }) do |f| %>
<%= f.input :commenter, value: comment_by_user %>
<%= f.input :body %>
<%= f.button :submit %>
Helper
def comment_by_user
current_user.try(:email) || current_admin.try(:email) if current_user || current_admin
end
Controller
class CommentsController < ApplicationController
before_filter :find_post
def new
#comment = #post.comments.build
end
def create
#comment = #post.comments.create(comments_params)
redirect_to post_path(#post)
end
def destroy
#comment = #post.comments.find(params[:id])
#comment.destroy
redirect_to post_path(#post)
end
private
def find_post
#post = Post.find(params[:post_id])
end
end
Found a solution:
Controller:
def create
#post = Post.find(params[:post_id])
#comment = #post.comments.create(comments_params)
if current_admin || current_user
#comment.commenter = current_user.try(:email) || current_admin.try(:email)
#comment.save
end
redirect_to post_path(#post)
end
View:
<%= simple_form_for([#post, #post.comments.build], html: {class: 'form-horizontal' }) do |f| %>
<% if current_admin || current_user %>
<%= f.input :body %>
<% else %>
<%= f.input :commenter %>
<%= f.input :body %>
<% end %>
<%= f.button :submit %>
<% end %>
Thanks for the help.

Resources