如何在Rails中从A类的show.html实例化B类对象?

时间:2013-12-25 12:53:40

标签: ruby-on-rails ruby

班级'会话'属于class_users。 “会话”可能会有很多'干扰'。

我希望能够通过单击按钮从分心类的show视图中实例化一个'distraction'对象。我该怎么做?

要澄清: 我知道重新加载页面是必要的,因为我没有使用AJAX。没关系。我只是希望在用户单击按钮后重新加载显示页面并反映新实例化的“分心”对象。

会话模型

class Session < ActiveRecord::Base
    belongs_to :user
    has_many :distractions, dependent: :destroy
    validates :name, presence: true
end

分心模型

class Distraction < ActiveRecord::Base
    belongs_to :session
end

SessionsController

class SessionsController < ApplicationController
    before_action :set_session,:authenticate_user!, except: [:index, :show], only: [:show, :edit, :update, :destroy]

    def index
        @sessions = current_user.sessions
    end

    def show
        @session = Session.find(params[:id])
    end

    def new
        @session = current_user.Session.build
    end

    def edit
        @session
    end

  def new_distraction
    @distraction = Distraction.create
  end

    def create
    @session = current_user.sessions.build(session_params)

    if @session.update(session_params)
      redirect_to @session, notice: 'Session was successfully created.'
    else
      render action: 'new'
    end
  end

  def update
    @session.ended_at = Time.now

    if @session.save
      redirect_to root_path, notice: 'Session was successfully ended.'
    else
      render action: 'edit'
    end
  end

  def destroy
    @session.destroy
    respond_to do |format|
      format.html { redirect_to root_path }
      format.json { head :no_content }
    end
  end

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

  # Never trust parameters from the scary internet, only allow the white list through.
  def session_params
    params.require(:session).permit(:name, :ended_at)
  end

end

DistractionsController

class DistractionsController < ApplicationController

    def index
        @distractions = current_session.Distraction.all
    end

    def new
        @distraction = Distraction.new
    end

    def create
        @distraction = Distraction.build
        if @distraction.save
            redirect_to '/sessions/show', notice: "Distraction saved successfully."
        else
            redirect_to '/sessions/show', notice: "Distraction could not be saved!"

        end
    end

end

视图/会话/显示

<h1><%= @session.name %></h1>

<%= render './distractions/form' %>

<br>

<p>

You have been distracted
<%= @session.distractions.size %>
 times.

</p>

<%= button_to 'End Session', session_path(@session, ended_at: Time.now) %>

视图/分心/ _form.html.erb

<%= button_to 'One Distraction', action: 'create' %>

我一直在努力解决这个问题,但继续遇到各种路由错误等。这是我的config / routing.rb:

Distracker::Application.routes.draw do
  resources :distractions
  resources :sessions
  devise_for :users do get '/users/sign_out' => 'devise/sessions#destroy' end
  # The priority is based upon order of creation: first created -> highest priority.
  # See how all your routes lay out with "rake routes".

  # You can have the root of your site routed with "root"
   root 'pages#home'

  # Example of regular route:
  #   get 'products/:id' => 'catalog#view'

  # Example of named route that can be invoked with purchase_url(id: product.id)
  #   get 'products/:id/purchase' => 'catalog#purchase', as: :purchase

  # Example resource route (maps HTTP verbs to controller actions automatically):
  #   resources :products

  # Example resource route with options:
  #   resources :products do
  #     member do
  #       get 'short'
  #       post 'toggle'
  #     end
  #
  #     collection do
  #       get 'sold'
  #     end
  #   end

  # Example resource route with sub-resources:
  #   resources :products do
  #     resources :comments, :sales
  #     resource :seller
  #   end

  # Example resource route with more complex sub-resources:
  #   resources :products do
  #     resources :comments
  #     resources :sales do
  #       get 'recent', on: :collection
  #     end
  #   end

  # Example resource route with concerns:
  #   concern :toggleable do
  #     post 'toggle'
  #   end
  #   resources :posts, concerns: :toggleable
  #   resources :photos, concerns: :toggleable

  # Example resource route within a namespace:
  #   namespace :admin do
  #     # Directs /admin/products/* to Admin::ProductsController
  #     # (app/controllers/admin/products_controller.rb)
  #     resources :products
  #   end
end

最后,如果我错过了相关内容,这里是github repo:

https://github.com/justuseapen/distraction

0 个答案:

没有答案