ruby on rails - Simple_form_for, comment don't appear as expected -
i have problem comments. if comment status whatever is, comment displayed on last status.
hope help, can edit more code if needed, let me know need.
_form.html.erb
<%= simple_form_for([status, status.comments.new]) |f|%> <%= f.input :content %> <%= f.button :submit, "comment", class:"btn btn-primary"%> <% end %>
index.html.erb
<% @statuses.each |status| %> <%= image_tag status.user.avatar.thumb if status.user.avatar?%> <%= status.user.full_name%> <%= simple_format(status.content) %> <%= link_to time_ago_in_words(status.created_at) + " ago", status %> <% if status.user == current_user %> <span class="admin"> <%= link_to "edit", edit_status_path(status) %> | <%= link_to "delete", status, method: :delete, data: {confirm: "are sure?"} %> </span> <% end %> <% status.comments.each |comment| %> <%= comment.content %> <% end %> <%= render partial: "comments/form", locals: {status: status} %> <% end %>
routes.rb
rails.application.routes.draw 'profiles/show' devise_for :users devise_scope :user 'register', to: 'devise/registrations#new', as: :register 'login', to: 'devise/sessions#new', as: :login 'logout', to: 'devise/sessions#destroy', as: :logout end resources :statuses resources :comments end resources :comments 'feed', to: "statuses#index", as: :feed root "statuses#index" '/:id', to: "profiles#show" end
statuses_controller.rb
class statusescontroller < applicationcontroller before_filter :authenticate_user!, only: [:new, :create, :edit, :update] before_action :set_status, only: [:show, :edit, :update, :destroy] def index @users = user.all @statuses = status.all @comments = comment.all end def show @status = status.find(params[:id]) @comments = @status.comments.all end def new @status = status.new @comment = @status.comments.build end def create @status = status.new(status_params) @status.user = current_user respond_to |format| if @status.save format.html { redirect_to @status, notice: 'status created.' } format.json { render :show, status: :created, location: @status } else format.html { render :new } format.json { render json: @status.errors, status: :unprocessable_entity } end end end def update respond_to |format| if @status.update(status_params) format.html { redirect_to @status, notice: 'status updated.' } format.json { render :show, status: :ok, location: @status } else format.html { render :edit } format.json { render json: @status.errors, status: :unprocessable_entity } end end end def destroy @status.destroy respond_to |format| format.html { redirect_to statuses_url, notice: 'status destroyed.' } format.json { head :no_content } end end private def set_status @status = status.find(params[:id]) end def status_params params.require(:status).permit( :content) end end
comments_controller.rb
class commentscontroller < applicationcontroller def create @status = status.find_by(params[:status_id]) @comment = @status.comments.create(params_comment) @comment.save redirect_to statuses_path end def index @statuses = status.all @comments = comment.all @comment = comment.find_by(params[:id]) end def new @status = status.find(params[:status_id]) @comment = comment.new end private def params_comment params.require(:comment).permit(:content) end end
my models
class comment < activerecord::base belongs_to :status belongs_to :user end class status < activerecord::base belongs_to :user has_many :comments default_scope -> { order(created_at: :desc)} validates :content, presence: true, length: {minimum: 2} validates :user_id, presence: true end class user < activerecord::base # include default devise modules. others available are: # :confirmable, :lockable, :timeoutable , :omniauthable devise :database_authenticatable, :registerable, :recoverable, :rememberable, :trackable, :validatable mount_uploader :avatar, avataruploader validates_presence_of :avatar validates_integrity_of :avatar validates_processing_of :avatar has_many :statuses has_many :users validates :first_name, presence: true validates :last_name, presence: true validates :profile_name, presence: true, uniqueness: true, format: { with: /\a[a-za-z0-9_-]+\z/, message: "must formatted correctly." } def full_name first_name + " " + last_name end def gravatar_url stripped_email = email.strip end end
thank you
Comments
Post a Comment