Note that there are some explanatory texts on larger screens.

plurals
  1. POHow to instantiate object of class B from show.html of class A in Rails?
    primarykey
    data
    text
    <p>The class 'sessions' belongs_to class_users. A 'session' may have_many 'distractions'.</p> <p>I'd like to be able to instantiate a 'distraction' object from the show view of the distraction class with the click of a button. How do I do this?</p> <p>TO CLARIFY: I know reloading the page is necessary since I'm not using AJAX. That's ok. I just want the show page to reload after the user clicks the button and reflect the newly instantiated 'distraction' object.</p> <p><strong>Session Model</strong></p> <pre><code>class Session &lt; ActiveRecord::Base belongs_to :user has_many :distractions, dependent: :destroy validates :name, presence: true end </code></pre> <p><strong>Distraction Model</strong></p> <pre><code>class Distraction &lt; ActiveRecord::Base belongs_to :session end </code></pre> <p><strong>SessionsController</strong></p> <pre><code>class SessionsController &lt; 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 </code></pre> <p><strong>DistractionsController</strong></p> <pre><code>class DistractionsController &lt; 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 </code></pre> <p><strong>Views/sessions/show</strong></p> <pre><code>&lt;h1&gt;&lt;%= @session.name %&gt;&lt;/h1&gt; &lt;%= render './distractions/form' %&gt; &lt;br&gt; &lt;p&gt; You have been distracted &lt;%= @session.distractions.size %&gt; times. &lt;/p&gt; &lt;%= button_to 'End Session', session_path(@session, ended_at: Time.now) %&gt; </code></pre> <p><strong>views/distractions/_form.html.erb</strong></p> <pre><code>&lt;%= button_to 'One Distraction', action: 'create' %&gt; </code></pre> <p>I've been struggling with this but continue to get a variety of routing errors etc. Here's my config/routing.rb:</p> <pre><code>Distracker::Application.routes.draw do resources :distractions resources :sessions devise_for :users do get '/users/sign_out' =&gt; 'devise/sessions#destroy' end # The priority is based upon order of creation: first created -&gt; 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' =&gt; 'catalog#view' # Example of named route that can be invoked with purchase_url(id: product.id) # get 'products/:id/purchase' =&gt; '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 </code></pre> <p>Finally, in case I missed something relevant, here is the github repo:</p> <p><a href="https://github.com/justuseapen/distraction" rel="nofollow">https://github.com/justuseapen/distraction</a></p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload