我在使用 rails 的表单时遇到问题

Posted

技术标签:

【中文标题】我在使用 rails 的表单时遇到问题【英文标题】:I am having trouble with the form in rails 【发布时间】:2021-07-13 12:40:05 【问题描述】:

如何在标签模型中存储图像标签。我有相册控制器,其中我在显示页面上有图像.. 在那些特定的图像中,我想在输入时保存标签。 我的相册控制器:

class AlbumsController < ApplicationController
    before_action :authenticate_user!
    def index
        @albums = current_user.albums.all
    end
    def show 
        
        @album= current_user.albums.find(params[:id])
    end 
    def new
        @album = current_user.albums.new
    end
    def create 
        @album = current_user.albums.new(album_params)
        if @album.save
            redirect_to albums_path 
        else
            render :new
        end
    end
    def edit
        @album =current_user.albums.find(params[:id])
    end
    def update
        @album = current_user.albums.find(params[:id])
        if @album.update(album_params)
            redirect_to albums_path
        else
            render :edit
        end
    end
    def destroy
        @album = current_user.albums.find(params[:id])
        @album.destroy 
        redirect_to albums_path
    end
    def delete_image_attachment
        @image = ActiveStorage::Attachment.find(params[:id])
        @image.purge
        redirect_to albums_path
    end
      
    
    private
        def album_params
            params.require(:album).permit(:title, :desciption, images: [])
        end
end

我的 routes.rb 文件:

Rails.application.routes.draw do

  devise_for :users 

  root to: "albums#index"
  resources :albums do 
    member do
      delete :delete_image_attachment
    end
    resources :comments
    resources :tags
  end
    
end

铁路路线是:

new_user_session GET    /users/sign_in(.:format)                                                                          devise/sessions#new
                            user_session POST   /users/sign_in(.:format)                                                                          devise/sessions#create
                    destroy_user_session DELETE /users/sign_out(.:format)                                                                         devise/sessions#destroy
                       new_user_password GET    /users/password/new(.:format)                                                                     devise/passwords#new
                      edit_user_password GET    /users/password/edit(.:format)                                                                    devise/passwords#edit
                           user_password PATCH  /users/password(.:format)                                                                         devise/passwords#update
                                         PUT    /users/password(.:format)                                                                         devise/passwords#update
                                         POST   /users/password(.:format)                                                                         devise/passwords#create
                cancel_user_registration GET    /users/cancel(.:format)                                                                           devise/registrations#cancel
                   new_user_registration GET    /users/sign_up(.:format)                                                                          devise/registrations#new
                  edit_user_registration GET    /users/edit(.:format)                                                                             devise/registrations#edit
                       user_registration PATCH  /users(.:format)                                                                                  devise/registrations#update
                                         PUT    /users(.:format)                                                                                  devise/registrations#update
                                         DELETE /users(.:format)                                                                                  devise/registrations#destroy
                                         POST   /users(.:format)                                                                                  devise/registrations#create
                                    root GET    /                                                                                                 albums#index
           delete_image_attachment_album DELETE /albums/:id/delete_image_attachment(.:format)                                                     albums#delete_image_attachment
                          album_comments GET    /albums/:album_id/comments(.:format)                                                              comments#index
                                         POST   /albums/:album_id/comments(.:format)                                                              comments#create
                       new_album_comment GET    /albums/:album_id/comments/new(.:format)                                                          comments#new
                      edit_album_comment GET    /albums/:album_id/comments/:id/edit(.:format)                                                     comments#edit
                           album_comment GET    /albums/:album_id/comments/:id(.:format)                                                          comments#show
                                         PATCH  /albums/:album_id/comments/:id(.:format)                                                          comments#update
                                         PUT    /albums/:album_id/comments/:id(.:format)                                                          comments#update
                                         DELETE /albums/:album_id/comments/:id(.:format)                                                          comments#destroy
                              album_tags GET    /albums/:album_id/tags(.:format)                                                                  tags#index
                                         POST   /albums/:album_id/tags(.:format)                                                                  tags#create
                           new_album_tag GET    /albums/:album_id/tags/new(.:format)                                                              tags#new
                          edit_album_tag GET    /albums/:album_id/tags/:id/edit(.:format)                                                         tags#edit
                               album_tag GET    /albums/:album_id/tags/:id(.:format)                                                              tags#show
                                         PATCH  /albums/:album_id/tags/:id(.:format)                                                              tags#update
                                         PUT    /albums/:album_id/tags/:id(.:format)                                                              tags#update
                                         DELETE /albums/:album_id/tags/:id(.:format)                                                              tags#destroy
                                  albums GET    /albums(.:format)                                                                                 albums#index
                                         POST   /albums(.:format)                                                                                 albums#create
                               new_album GET    /albums/new(.:format)                                                                             albums#new
                              edit_album GET    /albums/:id/edit(.:format)                                                                        albums#edit
                                   album GET    /albums/:id(.:format)                                                                             albums#show
                                         PATCH  /albums/:id(.:format)                                                                             albums#update
                                         PUT    /albums/:id(.:format)                                                                             albums#update
                                         DELETE /albums/:id(.:format)                                                                             albums#destroy
           rails_postmark_inbound_emails POST   /rails/action_mailbox/postmark/inbound_emails(.:format)                                           action_mailbox/ingresses/postmark/inbound_emails#create
              rails_relay_inbound_emails POST   /rails/action_mailbox/relay/inbound_emails(.:format)                                              action_mailbox/ingresses/relay/inbound_emails#create
           rails_sendgrid_inbound_emails POST   /rails/action_mailbox/sendgrid/inbound_emails(.:format)                                           action_mailbox/ingresses/sendgrid/inbound_emails#create
     rails_mandrill_inbound_health_check GET    /rails/action_mailbox/mandrill/inbound_emails(.:format)                                           action_mailbox/ingresses/mandrill/inbound_emails#health_check
           rails_mandrill_inbound_emails POST   /rails/action_mailbox/mandrill/inbound_emails(.:format)                                           action_mailbox/ingresses/mandrill/inbound_emails#create
            rails_mailgun_inbound_emails POST   /rails/action_mailbox/mailgun/inbound_emails/mime(.:format)                                       action_mailbox/ingresses/mailgun/inbound_emails#create
          rails_conductor_inbound_emails GET    /rails/conductor/action_mailbox/inbound_emails(.:format)                                          rails/conductor/action_mailbox/inbound_emails#index
                                         POST   /rails/conductor/action_mailbox/inbound_emails(.:format)                                          rails/conductor/action_mailbox/inbound_emails#create
       new_rails_conductor_inbound_email GET    /rails/conductor/action_mailbox/inbound_emails/new(.:format)                                      rails/conductor/action_mailbox/inbound_emails#new
      edit_rails_conductor_inbound_email GET    /rails/conductor/action_mailbox/inbound_emails/:id/edit(.:format)                                 rails/conductor/action_mailbox/inbound_emails#edit
           rails_conductor_inbound_email GET    /rails/conductor/action_mailbox/inbound_emails/:id(.:format)                                      rails/conductor/action_mailbox/inbound_emails#show
                                         PATCH  /rails/conductor/action_mailbox/inbound_emails/:id(.:format)                                      rails/conductor/action_mailbox/inbound_emails#update
                                         PUT    /rails/conductor/action_mailbox/inbound_emails/:id(.:format)                                      rails/conductor/action_mailbox/inbound_emails#update
                                         DELETE /rails/conductor/action_mailbox/inbound_emails/:id(.:format)                                      rails/conductor/action_mailbox/inbound_emails#destroy
new_rails_conductor_inbound_email_source GET    /rails/conductor/action_mailbox/inbound_emails/sources/new(.:format)                              rails/conductor/action_mailbox/inbound_emails/sources#new
   rails_conductor_inbound_email_sources POST   /rails/conductor/action_mailbox/inbound_emails/sources(.:format)                                  rails/conductor/action_mailbox/inbound_emails/sources#create
   rails_conductor_inbound_email_reroute POST   /rails/conductor/action_mailbox/:inbound_email_id/reroute(.:format)                               rails/conductor/action_mailbox/reroutes#create
                      rails_service_blob GET    /rails/active_storage/blobs/redirect/:signed_id/*filename(.:format)                               active_storage/blobs/redirect#show
                rails_service_blob_proxy GET    /rails/active_storage/blobs/proxy/:signed_id/*filename(.:format)                                  active_storage/blobs/proxy#show
                                         GET    /rails/active_storage/blobs/:signed_id/*filename(.:format)                                        active_storage/blobs/redirect#show
               rails_blob_representation GET    /rails/active_storage/representations/redirect/:signed_blob_id/:variation_key/*filename(.:format) active_storage/representations/redirect#show
         rails_blob_representation_proxy GET    /rails/active_storage/representations/proxy/:signed_blob_id/:variation_key/*filename(.:format)    active_storage/representations/proxy#show
                                         GET    /rails/active_storage/representations/:signed_blob_id/:variation_key/*filename(.:format)          active_storage/representations/redirect#show
                      rails_disk_service GET    /rails/active_storage/disk/:encoded_key/*filename(.:format)                                       active_storage/disk#show
               update_rails_disk_service PUT    /rails/active_storage/disk/:encoded_token(.:format)                                               active_storage/disk#update
                    rails_direct_uploads POST   /rails/active_storage/direct_uploads(.:format)                                                    active_storage/direct_uploads#create

我的标签控制器:

class TagsController < ApplicationController
    def create
        @image = ActiveStorage::Blob.find(params[:image_id])
        tags = params[:tag_name]
        tags.each do |tag|
            image = params[:image_id]
            @tag = Tag.create(tag_name:tag,image_id:image)
            redirect_to album_path(@album)
        end 
    end    
end

我的标签模型:

class Tag < ApplicationRecord
    belongs_to :image
end

这是我的显示页面(其中一半)出现错误:

<%= form_for(@tag, url: album_tags_path(@album) ) do |form|%>
<div class="modal fade" id="exampleModal" tabindex="-1" aria-labelledby="exampleModalLabel" aria-hidden="true">
    <div class="modal-dialog">
      <div class="modal-content">
        <div class="modal-header">
          <h5 class="modal-title" id="exampleModalLabel">Enter Tags for your Image here</h5>
          <button type="button" class="close" data-dismiss="modal" aria-label="Close">
            <span aria-hidden="true">&times;</span>
          </button>
        </div>
        <div class="modal-body">
          <div class="tag-container d-flex flex-wrap">
              <input class="form-control py-4 px-3" />
          </div>       
        </div>
        <div class="modal-footer">
          <button type="button" class="btn btn-secondary" data-dismiss="modal">Close</button>
          <%= form.submit class:"btn btn-info"%>
        </div>
      </div>
    </div>
  </div>
<% end %>

form_for url 有什么问题??? 我收到错误

ArgumentError in Albums#show

【问题讨论】:

【参考方案1】:

让 Rails 推断路线怎么样

<%= form_for([@album , @tag]) do |form| %>

【讨论】:

以上是关于我在使用 rails 的表单时遇到问题的主要内容,如果未能解决你的问题,请参考以下文章

rails + angularjs 在编辑时将值加载到文本字段中

我在apache中使用乘客的rails时遇到403错误

在使用 Rails 3 时遇到一些问题,继续使用 Rails 4

带有文件上传的 Rails 3 多步骤表单

Rails - 安装 capybara-webkit 时遇到问题

将 Rails 表单发布到 s3 上传的策略和签名问题