我正在尝试构建一个简单的Rails应用程序,用户可以在其中添加许多图像。我在桌子上设置了多对多的ImageIdeas和图像可以有很多想法,想法可以有很多图像。
很难理解这个错误
资产管道中不存在资产“”。
这是我的想法控制器
class IdeasController < ApplicationController
before_action :logged_in?
before_action :idea_current_user, only: [:show, :edit, :update, :destroy]
def new
@idea = Idea.new
@user = User.find(params[:user_id])
@categories = Category.all
end
def create
@user = User.find(params[:user_id])
@idea = @user.ideas.new(idea_params)
@images = Image.all
if @idea.save
redirect_to user_idea_path(@user, @idea)
else
render :new
end
end
def index
@ideas = Idea.all
end
def show
@idea = Idea.find(params[:id])
@user = @idea.user
end
def edit
@idea = Idea.find(params[:id])
@user = @idea.user
@categories = Category.all
end
def update
@idea = Idea.find(params[:id])
@user = @idea.user
if @idea.update(idea_params)
flash[:notice] = "Success"
redirect_to user_idea_path(@user, @idea)
else
flash[:notice] = "Try again"
render :edit
end
end
def destroy
@idea = Idea.find(params[:id])
@user = @idea.user
@idea.destroy
redirect_to user_ideas_path(@user)
end
private
def idea_params
params.require(:idea).permit(:user_id, :name, :category_id)
end
def idea_current_user
@user = current_user
end
end
这是app / views / ideas / new.html.erb
<%= link_to "Home", root_path %>
<%= link_to "My Profile", user_path(@user) %>
<%= link_to "My Ideas", user_ideas_path %>
<div class = 'new-idea-form'>
<%= form_for [@user, @idea] do |f| %>
<%= f.label :name %>
<%= f.text_field :name %>
<%= select(:idea, :category_id, options_for_select(@categories.collect do |c|
[c.name, c.id] end)) %>
<% @images.each do |image| %>
<div class="image_<%= image.id %>">
<%= check_box_tag image.title %>
<%= image_tag image.image.to_s%>
</div>
<% end %>
<%= f.submit%>
<% end %>
</div>
<div class="image-selection">
</div>
和节目
<%= link_to "Home", root_path %>
<% @user.ideas.each do |idea| %>
<%= idea.name %><br>
<%= image_tag idea.images %>
<%= link_to "Edit Idea", edit_user_idea_path(@user)%><br>
<%= link_to "Delete Idea", user_idea_path(@user), method: :delete %>
<% end %>
亏本,截止日期是早上。希望夜猫子能抓住这个,但如果不是,就会喜欢看它是如何解决的。感谢。