私は船と仕事を作り出すことができる船積みプロジェクトを持っています。私は仕事や船との多対多の関係を持っていますが、私は船の新しい仕事を創造するのに苦労しています。多対多関係として船にジョブを割り当てる方法
チェックボックスを使用して特定のボートのジョブを割り当てる新しいリンクが必要ですが、get "/ship/all_jobs"
と一致するルートがない場合にジョブを割り当てるリンクをクリックするとエラーが表示されます。
これは私の船のモデルです:
class Ship < ApplicationRecord
has_and_belongs_to_many :jobs
has_attached_file :avatar, :styles => { :medium => "300x300>", :thumb => "100x100#" }, :default_url => "/images/:style/missing.png"
validates_attachment_content_type :avatar, :content_type => /\Aimage\/.*\Z/
validates :name, uniqueness: true
end
これは私の仕事のモデルである:
class Job < ApplicationRecord
has_and_belongs_to_many :ships
validate :price_has_to_be_greater_than_minimum
validates :title, uniqueness: true
validates :description, length: { minimum: 50 }
def price_has_to_be_greater_than_minimum
errors.add(:cost, "price has to be greater than 1000") if
!cost.blank? and cost > 1000
end
end
これはJobshipがテーブルを結合です:
class Jobship < ApplicationRecord
belongs_to :ships
belongs_to :jobs
end
と私の船コントローラ:
def all_jobs
@ship = Ship.find(params[:id])
end
def create
@ship = Ship.new(ship_params)
if @ship.save
flash[:notice] = 'Ship record was successfully created.'
redirect_to(@ship)
else
render :action => "new"
end
end
def save
@ship = Ship.find(params[:id])
@job = Job.find(params[:job])
if params[:show] == "true"
@ship.jobs << @ship
else
@ship.jobs.delete(@ship)
end
@ship.save!
render :nothing => true
end
private
def ship_params
params.require(:ship).permit(:name, :location, :avatar)
end
end
これはALL_JOBS図である:
<h1>jobs for <%= @ship.name %></h1>
<table>
<tr>
<th>assignl</th>
<th>job</th>
</tr>
<%= form_for (@ship) do |f| %>
<%= f.label "jobs" %><br />
<%= f.collection_check_boxes :job_ids, Job.all, :id, :title do |b| %>
<div class="collection-check-box">
<%= b.check_box %>
<%= b.label %>
<%= f.submit %>
<%end%>
<%end%>
これは、インデックス・ビューからすべてのジョブへのリンクです:
<%= link_to 'New ship', new_ship_path %>
<% @ships.each do |ship| %>
<h1>ship name</h1><%=ship.name%><h1> ship location</h1> <%= ship.location %> <%= link_to 'Show', ship %>
<%= link_to 'jobs', ship_all_jobs_path(ship) %>
<%= link_to "all jobs", jobs_path %>
<% end %>
そして、私のルート:すべての
Rails.application.routes.draw do
devise_for :users
resources :ships
resources :jobs
root :to => "ships#index"
post "ship/all_jobs", :to=> "ships#save"
get "ship/all_jobs/:id", :to => "ships#all_jobs"
end
ようこそスタックオーバーフロー。適切な書式設定と文法を理解してください。 Stack Overflowはディスカッションリストではなく、スペルや文法が重要なオンラインリファレンスブックです。あなたのブラウザのスペルチェックを使用すると多くの助けになります。 「[スマートウェイに質問する方法(http://catb.org/esr/faqs/smart-questions.html)]には、このようなサイトやコミュニティを扱うためのヒントがいくつかあります。 –