如何让我创建的奖励对象被分配给汽车对象?

时间:2018-02-20 19:18:14

标签: ruby-on-rails

我是rails和构建我的第一个应用程序的新手。我无法想象如何将car_id分配给使用views / awards / new.html.erb文件创建的新奖励实例。谁能看一看,看看我哪里出错?

奖励管理员:

class AwardsController < ApplicationController
  before_action :set_award, only: [:show]

  def new
    @award = Award.new
  end

  def index
    @awards = Award.all
  end

  def create
    @car = Car.params
    @award = Award.new(award_params)
    @award.save
    redirect_to user_path(current_user)
  end

  def show
  end

  def destroy
  end

  private

  def set_award
    @award = Award.find(params[:id])
  end

  def award_params
    params.require(:award).permit(:title, :year, :description)
  end
end

汽车控制器

class CarsController < ApplicationController
  before_action :set_car, only: [:show, :edit, :update, :destroy]
  def new
    @car = Car.new
  end

  def index
    @cars = Car.all
  end

  def create
    @car = Car.new(car_params)
    @car.save
    current_user.cars << @car
    redirect_to current_user, :flash => { :success => "car created!" }
  end

  def edit
  end

  def update
    @car.update(car_params)
    if @car.valid?
      @car.save
      redirect_to user_path(current_user)
    else
      render :edit
    end
  end

  def show
    @car = Car.find(params[:id])
  end

  def destroy
    @car.destroy
    redirect_to user_path(current_user)
  end

  private

  def set_car
    @car = Car.find(params[:id])
  end

  def car_params
    params.require(:car).permit(:make,:model,:year,:color)
  end

end

car.rb

class Car < ApplicationRecord
  belongs_to :user
  has_many :awards 
end

award.rb

class Award < ApplicationRecord
  belongs_to :car
  has_one :user, through: :cars
end

路由

Rails.application.routes.draw do
  resources :awards
  resources :cars
  devise_for :users, :controllers => { :omniauth_callbacks => "users/omniauth_callbacks" }
  # For details on the DSL available within this file, see http://guides.rubyonrails.org/routing.html
  resources :users
  root 'welcome#home'
  resources :users do
    resources :cars
  end

  resources :cars do
    resources :awards
  end
end

@awards表格

<%= form_for @award do |f| %>

  <%= f.label :title %>
  <%= f.text_field :title %><br>

  <%= f.label :year %>
  <%= f.text_field :year %><br>

  <%= f.label :description %>
  <%= f.text_area :description %><br>

  <%= f.submit %>

<%end%>

我需要能够为特定车辆分配奖励,是否有人知道如何做到这一点?

1 个答案:

答案 0 :(得分:0)

由于您使用的是嵌套路由:

from django.conf.urls import url, include
from django.contrib import admin

# A custom view of my own
from app.views import AdvanceCustomSearchView

urlpatterns = [
  url(r'^search/$', AdvanceCustomSearchView(), name='index_search'),
  url(r'^app/', include('app.urls')),
  url(r'^admin/', admin.site.urls),
]

然后:

应用/控制器/ awards_controller.rb

resources :cars do
  resources :awards
end

应用/视图/奖励/ new.html.erb

class AwardsController < ApplicationController
  # add more into the array as you need
  before_action :set_car, only: [:new, :create]

  def new
    @award = @car.awards.new
  end

  def create
    @award = @car.awards.new(award_params)
    @award.save
    redirect_to user_path(current_user)
  end

  private

  def set_car
    @car = Car.find(params[:car_id])
  end
end

花絮:

  • 因为您使用的是嵌套路线,所以您的链接类似于:

    <%= form_for [@car, @award] do |f| %>
      ...
    

...只需将{{1>}和<%= link_to 'New Award', new_car_award_path(SOMECAR) %> <%= link_to 'Awards', car_awards_path(SOMECAR) %> <%= link_to 'Edit Award', edit_car_award_path(SOMECAR, SOMEAWARD) %> <%= link_to 'Delete Award', car_award_path(SOMECAR, SOMEAWARD), method: :delete %> 分别替换为您希望加入的 Car Award 实例那个链接。

建议: