简体   繁体   English

Rails has_many / belongs_to无法正常工作

[英]Rails has_many/belongs_to Not Working

I have an app in which I have user , request , and proposal models. 我有一个其中具有userrequestproposal模型的应用程序。 They're structured like this. 它们的结构是这样的。 A user has many proposals and requests : user有许多proposalsrequests

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

A request belongs to a user and has many proposals : 一个request属于一个user ,有很多proposals

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

A proposal belongs to a user and a request . proposal属于userrequest Whatever user the request belongs to should also have the proposal : 无论request属于哪个user ,都应该有proposal

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

I have it set up like this in my schema.rb : 我在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"

I have a proposal#new / proposal#edit view form in which I set the :request using a hidden_field : 我有一个proposal#new / proposal#edit视图表单,在其中使用hidden_field设置:request

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

Then, when I try to call the project_title of the request that has a proposal on the proposal#show page ( <%= @proposal.request.project_title %> ) it comes out as nil . 然后,当我尝试在proposal#show页面上调用具有proposalrequestproject_title<%= @proposal.request.project_title %> )时显示为nil

Can anyone help me restructure things to get this working right? 谁能帮助我重组事情以使其正常工作? The only complicated thing I'm trying to do is, following this blog I'm trying to get the value of the request by which link is clicked. 我要做的唯一复杂的事情是,在访问此博客后,我试图获取单击链接的request的值。 Here's my proposals_controller : 这是我的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

And here are my routes: 这是我的路线:

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

Anyone see where I'm going wrong? 有人看到我要去哪里了吗?

have you try doing something like this in your routes 您是否尝试过在路线中做类似的事情

   resources:requests do
     resources :proposals
   end

response 响应

                  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

to make a new proposals you would do: new_request_proposal_path(@request) 提出新建议,您可以这样做: new_request_proposal_path(@request)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM