Federico Calarco

Joined

160 Experience
0 Lessons Completed
0 Questions Solved

Activity

Posted in Add a coment in a post ( how can I do)?

I'm coding my personal website , I have a backoffice where I can write blog_post ( title , content , category and date ).
I would like to make user comment my blog post without being logged in .
This is the code in /layouts/home.html.erb.

<section class="blog id col-sm-12">




<h1 class="col-sm-10 col-sm-offset-2 text-left">Blog post</h1>



      <div class="col-sm-2"></div>

                    <% @blog_posts.each do |blog_post| %>

                      <div class="col-sm-5  blog_post ">

                                <div class="col-sm-12 post">

                        <h3 class="col-sm-12 text-left"><%= blog_post.title.capitalize  %></h3>

                            <span class="col-sm-1" style="background-color:white;"></span>

                              <p class="col-sm-12 text-left content">"<%= blog_post.description.capitalize %>"</p>

                              <p class="col-sm-8 text-left"> <i class="fa fa-circle" style="color:#F0F4C3;"> <%= blog_post.category.capitalize %></i></p>

                              <p class="col-sm-4 text-right"> <%= blog_post.post_date  %> </p>

                              <p class="col-sm-12 text-left content">"<%= blog_post.comments.count %>"</p>

                              </div>


                <p class="col-sm-6  btn-default text-center post_button"><%= link_to 'Visualizza', blog_post %></p>

                <p class="col-sm-6 btn-default text-center post_button comment"><a href="#0">Commenta</a></p>


            </div>

<div class="col-sm-2"></div>

      <% end %>

<% if admin? %>

<%= link_to 'New Blog post', new_blog_post_path %>
<%= link_to 'Logout', destroy_admin_session_path, method: :delete %>

<% end %>



This is the section that comes out if an user click on "blog" , at the ond of if there is a button with "commenta", clicking it appears  a modal with 3 fields , name , email and comment. 

Comments belong to blog post and blog post has many comments obviously, I know I should set something in the controller but i really don't know what .
I had some suggestions but everybody told me how to make user add a comment in blog_post #show , but i would like to make them comment directly from the homepage.

Every advice is good :D 

Posted in How can I solve it?

Perfect , I will search how to do that, thank you!

Posted in How can I solve it?

OOOOOOK , it works , but now if I write an empty blog_post there are no error like " title can't be blank" , it's a half success :D

Posted in How can I solve it?


class BlogPostsController < ApplicationController
  before_action :set_blog_post, only: [:show, :edit, :update, :destroy]



  # GET /blog_posts
  # GET /blog_posts.json
  def index
    @blog_posts = BlogPost.all

  end

  # GET /blog_posts/1
  # GET /blog_posts/1.json
  def show
  end

  # GET /blog_posts/new
  def new
    @blog_post = current_admin.blog_posts.new
  end

  # GET /blog_posts/1/edit
  def edit
  end

  # POST /blog_posts
  # POST /blog_posts.json
  def create

    @blog_post = current_admin.blog_posts.new(blog_post_params)

    respond_to do |format|
      if @blog_post.save
        format.html { redirect_to @blog_post, notice: 'Blog post was successfully created.' }
        format.json { render :show, status: :created, location: @blog_post }
      else
        format.html { render :new }
        format.json { render json: @blog_post.errors, status: :unprocessable_entity }
      end
    end

  end

  # PATCH/PUT /blog_posts/1
  # PATCH/PUT /blog_posts/1.json
  def update
    respond_to do |format|
      if @blog_post.update(blog_post_params)
        format.html { redirect_to @blog_post, notice: 'Blog post was successfully updated.' }
        format.json { render :show, status: :ok, location: @blog_post }
      else
        format.html { render :edit }
        format.json { render json: @blog_post.errors, status: :unprocessable_entity }
      end
    end
  end

  # DELETE /blog_posts/1
  # DELETE /blog_posts/1.json
  def destroy
    @blog_post.destroy
    respond_to do |format|
      format.html { redirect_to blog_posts_url, notice: 'Blog post was successfully destroyed.' }
      format.json { head :no_content }
    end
  end

  private
    # Use callbacks to share common setup or constraints between actions.
    def set_blog_post
      @blog_post = BlogPost.find(params[:id])
    end

    # Never trust parameters from the scary internet, only allow the white list through.
    def blog_post_params
      params.require(:blog_post).permit(:title, :description, :status, :category, :post_date)
    end
end

The render ( of the form ) is inside a modal in /layouts/application.html.erb
Should I declare the variable in other controller?

Posted in How can I solve it?

I thought the same thing and I immediately wrote the code in the controllers but it still does not work , I really don't know where is my error

Posted in How can I solve it?

I tried to copy and past the code block but it doesn't work

Posted in How can I solve it?


<div class="col-sm-6 col-sm-offset-3"  >
  <div class="modal fade " id="myModal" tabindex="-1" role="dialog">
    <div class="modal-dialog">
      <div class="modal-content">
        <div class="modal-header">
          <button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">&times;</span></button>
          <h4 class="modal-title">Modal title</h4>
          <%= form_for(BlogPost.new) do |f| %>
            <% if @blog_post.errors.any? %>
              <div id="error_explanation">
                <h2><%= pluralize(@blog_post.errors.count, "error") %> prohibited this blog_post from being saved:</h2>

                <ul>
                <% @blog_post.errors.full_messages.each do |message| %>
                  <li><%= message %></li>
                                        <div class="col-sm-2  pull-left" id="socialize">
                                <p>I'm a social persons find me :<br>fb tw insta</p>

                                        </div>

                <% end %>
                </ul>
              </div>
            <% end %>

            <div class="field">
              <%= f.label :title %><br>
              <%= f.text_field :title %>
            </div>
            <div class="field">
              <%= f.label :description %><br>
              <%= f.text_area :description %>
            </div>
            <div class="field">
              <%= f.label :status %><br>
              <%= f.check_box :status %>
            </div>
            <div class="field">
              <%= f.label :category %><br>
              <%= f.text_field :category %>
            </div>
            <div class="field">
              <%= f.label :post_date %><br>
              <%= f.date_select :post_date %>
            </div>
            <div class="actions">
              <%= f.submit %>
            </div>
          <% end %>

        </div>
        <div class="modal-body">
        <div class="modal-footer">
          <button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
          <button type="button" class="btn btn-primary">Save changes</button>
        </div>
      </div><!-- /.modal-content -->
    </div><!-- /.modal-dialog -->
  </div><!-- /.modal -->
</div>


            </div>

Posted in How can I solve it?

Hello guys I have a problem with this!

I'm making my website where user can give feedback to my projects, contact me and make comment to a blog_post.
The problem is when i make the render 'blog_posts/form' the errors are
"First argument in form cannot contain nil or be empty" - (@blog_post) ( solvable with -BlogPost.new-)
the second error after that is
"undefined method `errors' for nil:NilClass" - <% if @blog_post.errors.any? %>

If i remove the block with the if statement for errors everything will work but there will be no parsing error after the create/new action (I set up the validations of the presence of name and content in the model )
I'm waiting for your help , thank you