【问题标题】:Rails update action/ / edit form not updating /not updating correctlyRails 更新操作//编辑表单未更新/未正确更新
【发布时间】:2020-12-26 02:02:36
【问题描述】:

所以,我有一个 Rails 应用程序,其中包含所有戏剧名称/类型的种子,我正在为它们进行评论的 CRUD。出于某种原因,我的创建/新表单运行良好,但当我更新时,它会将我之前评论过的那部戏剧替换为我刚刚制作的当前剧...SOS!

编辑:我更新了这个,所以有我的所有模型以及戏剧种子!我希望能够更新我的评论表,而不会重复或接管我对其他戏剧的评论!

所以对于我提供的第一个表单,我对其进行了测试,它适用于创建新评论,但不适用于更新。第二种形式适用于 UPDATING,但我想让它只是一种形式,因为 form_for 就在那里!

这是我的评论控制器:

    class ReviewsController < ApplicationController
before_action :find_by_review, only: [:show, :edit, :update, :destroy]
before_action :current_user, only: [:create, :update, :edit, :destroy]

    def new 
        @review = Review.new 
    end 

    def show 
    end 

    def create 
        @review = Review.new(review_params)
        @drama = Drama.find(session[:drama_id])
        if @review.save
            redirect_to drama_review_path(@drama, @review), info: "You've created a new review!"
        else 
            @errors = @review.errors.full_messages
            render :new 
        end 
    end 

    def edit 
        
    end 

    def update 
        @review.update(review_params)
        if @review
        redirect_to user_path(@user), info: "You've successfully updated your review!"
        else 
            @errors = @user.errors.full_messages
            render :edit
        end 
    end 

    def destroy 
        @review.destroy
        redirect_to user_path(@user), info: "You've successfully deleted your review!"
    end 

    private 

    def review_params 
        params.require(:review).permit(:title, :content, :rating, :user_id, :drama_id)
    end 
end

这是我的新/编辑表单:

    <% @errors && @errors.each do |m|%>
     <li><%= m %></li>
    <%end%>

    <%= form_for @review do |f| %>
    <%= f.label :title %>
    <%= f.text_field :title %><br>
    <%= f.label :rating %>
    <%= f.number_field :rating %><br>
    <%= f.label :content %>
    <%= f.text_area :content %><br>
    <%= f.hidden_field :drama_id, value: session[:drama_id]%>
    <%= f.hidden_field :user_id, value: current_user.id%>
    <%= f.submit%>
    <%end%>
    
    <%=button_to "Main Page", dramas_path, method: :get%>

这是我的戏剧模型:

class Drama < ApplicationRecord
    has_many :reviews 
    has_many :users, :through => :reviews
    validates :name, presence: true, uniqueness: true
    validates :genre, presence: true 
    before_validation :make_title_case  
    accepts_nested_attributes_for :reviews
    

    def self.alphabetized
        all.order('name asc')
    end
    private

    def make_title_case
    self.name = self.name.titlecase
    end
end

这是我的评论模型:

class Review < ApplicationRecord
    belongs_to :user 
    belongs_to :drama 
    validates :rating, presence: true, length: { maximum: 10 }
    validates :content, presence: true 
    validates :title, presence: true 

    def self.rating_limit
        all.where('rating desc').limit(10)
    end 
end

这是我的用户模型:

class User < ApplicationRecord
    has_many :reviews 
    has_many :dramas, :through => :reviews
    validates :username, uniqueness: true, presence: true 
    validates :email, uniqueness: true, presence: true 
    has_secure_password 
end

这是戏剧种子:

dramas = [
    {name: "Goblin", genre: "Comedy, Romance, Fantasy, Melodrama, Supernatural"},
    {name: "Its Okay, thats Love", genre: "Friendship, Psychological, Comedy, Romance, Drama"},
    {name: "Its Okay to Not be Okay", genre: "Psychological, Comedy, Romance, Drama, Family"},
    {name: "Start-Up", genre: "Business, Comedy, Romance, Youth, Drama"},
    {name: "Secret", genre: "Romance, Drama, Melodrama"},
    {name: "Secret Garden", genre: "Comedy, Romance, Drama, Melodrama, Supernatural"},
    {name: "Flower of Evil", genre: "Thriller, Mystery, Psychological, Romance, Crime, Melodrama"},
    {name: "Strong Woman Do Bong Soon", genre: "Action, Thriller, Comedy, Romance, Drama, Supernatural"},
    {name: "W", genre: "Action, Suspense, Thriller, Comedy, Romance, Drama, Fantasy, Melodrama"},
    {name: "Pinocchio", genre: "Thriller, Mystery, Comedy, Romance, Melodrama"},
    {name: "Healer", genre: "Action, Thriller, Mystery, Comedy, Romance, Drama"},
    {name: "While You were Sleeping", genre: "Thriller, Mystery, Comedy, Romance, Drama, Fantasy"},
    {name: "The Smile has Left your Eye", genre: "Thriller, Mystery, Romance, Drama"},
    {name: "Kill me, Heal me", genre: "Psychological, Comedy, Romance, Drama"},
    {name: "I'm not a robot", genre: "Friendship, Comedy, Romance, Drama, Sci-Fi"},
    {name: "The Beauty inside", genre: "Comedy, Romance, Family, Melodrama, Supernatural"},
    {name: "Angel's last mission: Love", genre: "Comedy, Romance, Drama, Fantasy"},
    {name: "King: Eternal Monarch", genre:  "Mystery, Romance, Drama, Fantasy"},
    {name: "My Girlfriend is a Gumhiho", genre: "Comedy, Romance, Drama, Fantasy, Supernatural"},
    {name: "Tale of the Nine-Tailed", genre: "Action, Suspense, Thriller, Horror, Romance, Fantasy"},
    {name: "Weightlifting Fairy Kim Bok Joo", genre: "Friendship, Comedy, Romance, School, Youth, Sports"},
    {name: "Boys over Flowers", genre: "Friendship, Comedy, Romance, School, Youth, Drama"},
    {name: "What's Wrong with Secretary Kim", genre: "Friendship, Business, Comedy, Romance"},
    {name: "Cheese in the Trap", genre: "Friendship, Psychological, Romance, Life, Drama"},
    {name: "Dream High", genre: "Friendship, Music, Comedy, Romance, School, Drama"},
    {name: "Oh My Venus", genre: "Friendship, Comedy, Law, Romance, Life, Drama, Sports"}
 ]

dramas.each do |drama|
    Drama.create drama 
end

这是能够正确更新评论的表格:

<%= form_for @review do |f| %>
<%= f.label :title %>
<%= f.text_field :title %><br>
<%= f.label :rating %>
<%= f.number_field :rating %><br>
<%= f.label :content %>
<%= f.text_area :content %><br>
<%= f.hidden_field :drama_id, value: @review.drama_id%>
<%= f.hidden_field :user_id, value: current_user.id%>
<%= f.submit%>
<%end%>

这是我的 routes.rb :

Rails.application.routes.draw do



resources :dramas do
    resources :reviews, only: [:new, :show, :edit, :update]
  end

  resources :dramas 
  resources :users
  resources :reviews 

  get '/auth/facebook/callback', to: 'sessions#create_with_fb'
  get '/', to: 'sessions#home'
  get '/signup', to: 'users#new'
  post '/signup', to: 'users#create'
  get '/login', to: 'sessions#new'
  post '/login', to: 'sessions#create'
  post '/logout', to: 'sessions#destroy'
  # For details on the DSL available within this file, see https://guides.rubyonrails.org/routing.html
end

【问题讨论】:

  • 尝试在 update @drama = Drama.find(session[:drama_id]) under review.update 中添加这一行
  • @nourza 我之前也补充过,它仍然不断取代我所做的其他评论。这些都是戏剧和重复。你很漂亮你很漂亮你很漂亮咖啡天使王子的最后任务:爱你很漂亮你很漂亮
  • 如果您能提供有关您的模型/关联的更多信息会很有帮助,并提供您期望的行为示例@Katie Le
  • 在任何情况下,似乎您在 review_params 中发送了错误的 Drama_id,这就是为什么当您调用方法更新以进行评论时,您会得到该结果,尝试简单地从该哈希中删除 Drama_id更新前
  • 对不起,我添加了对我要问的问题很重要的所有内容!你的意思是把 @drama = Drama.find(session[:drama_id]) 部分从创建操作中取出或

标签: ruby-on-rails ruby forms form-for


【解决方案1】:

您的路线中有两个resources :reviews 条目。一个嵌套在dramas 内,另一个嵌套在外部。理想情况下,您应该只保留一个 (Nested routes)

  resources :dramas do
    resources :reviews, only: [:new, :show, :edit, :update]
  end

戏剧 ID 将在 ReviewController 内的 drama_id 参数中提供。

因此,您需要在 ReviewsController 中进行的一些更改是

  before_action :init_drama

  def new 
    @review = @drama.reviews.new
  end

  def create 
    @review = Review.new(review_params)
    if @review.save
      redirect_to drama_review_path(@review.drama, @review), info: "You've created a new review!"
    else 
      @errors = @review.errors.full_messages
      render :new
    end
  end

  private

  def init_drama
    @drama = Drama.find(params[:drama_id])
  end

【讨论】:

  • 它给了我这个错误:ReviewsController#edit 中的 ActiveRecord::RecordNotFound 找不到没有 ID 的戏剧 提取源(在第 45 行附近):43 44 45 46 47 48 def init_drama @drama = Drama.find(params[:drama_id]) end def review_params Rails.root: /Users/Katiethile/Flatiron/Korean-Dramas-Rails 应用程序跟踪 |框架跟踪 |完整跟踪 app/controllers/reviews_controller.rb:45:in `init_drama' 请求参数:{"id"=>"32"}
  • 你能分享完整的请求网址吗?
  • 对不起,我怎么能得到那个? @anuj
  • 哦,我的意思是您从浏览器中点击的网址(或者您可以从服务器日志中获取)
  • 这是点击创建评论后浏览器中的网址:localhost:3000/reviews,这是点击更新评论的链接:localhost:3000/reviews/32/edit哈哈,所以我最初只是在更新@anuj时遇到问题跨度>
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-10-26
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2022-07-20
相关资源
最近更新 更多