2016-12-16 2 views
0

Ich habe eine App, in der ich user, request und proposal Modelle habe. Sie sind so strukturiert. Ein user hat viele proposals und requests:Schienen has_many/gehört_to funktioniert nicht

class User < ActiveRecord::Base 
    has_many :requests 
    has_many :proposals 
end 

A request gehört zu einer user und hat viele proposals:

class Request < ActiveRecord::Base 
    belongs_to :user 
    has_many :proposals 
end 

A proposal gehört zu einer user und einem request. Was auch immer user die request gehört sollte auch die proposal:

class Proposal < ActiveRecord::Base 
    belongs_to :user 
    belongs_to :request 
end 

Ich habe es eingerichtet wie dies in meinem schema.rb:

create_table "proposals", force: :cascade do |t| 
    t.integer "user_id" 
    t.integer "request_id" 
    ... 
    t.string "status",    default: "Proposal Created" 
    t.datetime "created_at",          null: false 
    t.datetime "updated_at",          null: false 
    end 

    add_index "proposals", ["request_id"], name: "index_proposals_on_request_id" 
    add_index "proposals", ["user_id"], name: "index_proposals_on_user_id" 

    create_table "requests", force: :cascade do |t| 
    t.string "project_title" 
    ... 
    t.integer "user_id" 
    end 

    add_index "requests", ["user_id"], name: "index_requests_on_user_id" 

Ich habe eine proposal#new/proposal#edit Ansicht Form, in der ich die :request mit einem hidden_field:

<%= f.hidden_field :request, value: @request %> 

Dann, wenn ich versuche, die project_title der request, die eine proposal auf der proposal#show Seite (<%= @proposal.request.project_title %>) hat, ruft es als nil heraus.

Kann mir jemand helfen, die Dinge umzustrukturieren, damit das funktioniert? Die einzige komplizierte Sache, die ich versuche zu tun, ist nach this blog Ich versuche, den Wert der request, durch den Link geklickt wird, zu erhalten. Hier ist mein proposals_controller:

class ProposalsController < ApplicationController 
    before_action :set_proposal, only: [:show, :edit, :update, :destroy] 

    # GET /proposals 
    def index 
    @proposals = Proposal.all 
    end 

    # GET /proposals/1 
    def show 
    end 

    # GET /proposals/new 
    def new 
    @request = Request.find(params[:request_id]) 
    @proposal = Proposal.new 
    end 

    # GET /proposals/1/edit 
    def edit 
    @request = Request.find(params[:request_id]) 
    @proposal = Proposal.find(params[:id]) 
    end 

    # POST /proposals 
    def create 
    @request = Request.find(params[:request_id]) 
    @proposal = @request.proposals.new 
    @proposal.user = @request.user 

    if @proposal.save 
     redirect_to request_proposal_path(@request, @proposal), notice: 'Proposal was successfully created.' 
    else 
     render :new 
    end 
    end 

    # PATCH/PUT /proposals/1 
    def update 
    if @proposal.update(proposal_params) 
     redirect_to @proposal, notice: 'Proposal was successfully updated.' 
    else 
     render :edit 
    end 
    end 

    # DELETE /proposals/1 
    def destroy 
    @proposal.destroy 
    redirect_to proposals_url, notice: 'Proposal was successfully destroyed.' 
    end 

    def unarchive 
    @proposal = Proposal.find(params[:id]) 
    if @proposal.update_attributes(status: "Sent to Client") 
     FavoriteMailer.send_proposal_to_client(@proposal, @proposal.user).deliver_now 
     flash[:notice] = "That proposal has sent to the client." 
     redirect_to :back 
    else 
     flash[:alert] = "That proposal could not be sent right now." 
    end 
    end 

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

    # Only allow a trusted parameter "white list" through. 
    def proposal_params 
     params.require(:proposal).permit(:user_id, :request_id, :executive_summary, :situation_analysis, :strategy_online, :strategy_real_world, :strategy_credentials, :strategy_timeline, :respond_by, :timetable, :financials, :conditions, :acceptance, :status) 
    end 
end 

Und hier sind meine Routen:

get 'proposal/:request', to: 'proposals#new', as: 'new_proposal' 
resources :proposals 

Wer sehen, wo ich falsch gehe?

Antwort

1

haben Sie so etwas in Ihrer Routen

resources:requests do 
    resources :proposals 
    end 

Antwort

    request_proposals GET  /requests/:request_id/proposals(.:format)          proposals#index 
            POST  /requests/:request_id/proposals(.:format)          proposals#create 
       new_request_proposal GET  /requests/:request_id/proposals/new(.:format)         proposals#new 
       edit_request_proposal GET  /requests/:request_id/proposals/:id/edit(.:format)        proposals#edit 
        request_proposal GET  /requests/:request_id/proposals/:id(.:format)         proposals#show 
            PATCH  /requests/:request_id/proposals/:id(.:format)         proposals#update 
            PUT  /requests/:request_id/proposals/:id(.:format)         proposals#update 
            DELETE  /requests/:request_id/proposals/:id(.:format)         proposals#destroy 
          requests GET  /requests(.:format)                requests#index 
            POST  /requests(.:format)                requests#create 
         new_request GET  /requests/new(.:format)               requests#new 
         edit_request GET  /requests/:id/edit(.:format)              requests#edit 
          request GET  /requests/:id(.:format)               requests#show 
            PATCH  /requests/:id(.:format)               requests#update 
            PUT  /requests/:id(.:format)               requests#update 
            DELETE  /requests/:id(.:format)               requests#destroy 

Versuchen Sie, eine neue Vorschläge zu machen, was Sie tun würden: new_request_proposal_path(@request)

+0

ich denke, es andersrum wäre herum (Vorschläge gehören zu Anfragen). Wie würde dies meine Routing-Links ändern (z. B. Verknüpfung mit einem neuen Angebot)? – Liz

+0

Danke, ich werde heute Nacht Updates machen – MZaragoza

+0

Und sagen Vorschlag gehört zu einer Anfrage – MZaragoza

Verwandte Themen