Rails has_many / belongs_to不工作

时间:2016-12-16 01:43:31

标签: ruby-on-rails

我有一个应用,其中有userrequestproposal型号。他们的结构是这样的。 user有许多proposalsrequests

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

request属于user且有许多proposals

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

proposal属于userrequestuser request所属的proposal也应该包含class Proposal < ActiveRecord::Base belongs_to :user belongs_to :request end

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"

中设置了这样的内容
proposal#new

我有一个proposal#edit / :request查看表单,我使用hidden_field设置<%= f.hidden_field :request, value: @request %>

project_title

然后,当我尝试拨打request proposalproposal#show<%= @proposal.request.project_title %> nil的{​​{1}}时,它出现了request

任何人都可以帮助我重组一些东西以使其正常工作吗?我尝试做的唯一复杂事情是,在this blog之后,我试图获取点击链接的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

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

这是我的路线:

clientData

任何人都会看到我出错的地方?

1 个答案:

答案 0 :(得分:1)

你有没有尝试在路线上做这样的事情

   resources:requests do
     resources :proposals
   end

响应

                  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

制作新提案:new_request_proposal_path(@request)