ruby-on-rails 将使用旧版Rails with Turbolinks构建的应用程序重新配置为Rails 7 with Turbo

bgibtngc  于 2023-04-22  发布在  Ruby
关注(0)|答案(1)|浏览(114)

我用旧版本的Rails和它附带的Turbolinks库构建了一个twitter克隆。我现在试图用新版本的Rails,版本7,并发现与它,我必须处理涡轮增压,而不是涡轮增压。我还没有任何成功的实施所需的修改,在我的项目,使推文显示在涡轮增压的方式。这是以前该功能的工作方式:
tweets_controller.rb创建了tweet并以turbo_stream格式发送数据

def create
    @tweet = Tweet.new(tweet_params.merge(user: current_user))

    if @tweet.save
      respond_to do |format|
        format.html { redirect_to dashboard_path }
        format.turbo_stream
      end
    end
  end

来自控制器的数据被发送到views/tweets/create.turbo_stream.erb

<%= turbo_stream.prepend "tweets" do %>
  <%= render partial: "tweets/tweet", locals: { tweet_presenter: TweetPresenter.new(@tweet) } %>
<% end %>

...在发送到显示器之前,它被调度到presenters/tweet_presenter.rb进行处理:

class TweetPresenter
  include ActionView::Helpers::DateHelper
  include Rails.application.routes.url_helpers 

  def initialize(tweet:, current_user:)
    @tweet = tweet
    @current_user = user
  end

  attr_reader :tweet, :current_user

  delegate :user, :body, :likes, :likes_count, :retweets_count, to: :tweet
  delegate :username, :display_name, :avatar, to: :user

...最后,tweet被发送到tweets/tweet.html.erb显示,并被 Package 在以下标签中:

<%= turbo_frame_tag dom_id(tweet_presenter.tweet) do %>

这是不工作,现在与涡轮增压.我得到以下错误消息在控制台:

Started POST "/tweets" for ::1 at 2023-04-13 14:29:24 -0700
Processing by TweetsController#create as TURBO_STREAM
  Parameters: {"authenticity_token"=>"[FILTERED]", "tweet"=>{"body"=>"GSd"}, "commit"=>"Tweet"}
  User Load (0.3ms)  SELECT "users".* FROM "users" WHERE "users"."id" = $1 ORDER BY "users"."id" ASC LIMIT $2  [["id", 1], ["LIMIT", 1]]
  ↳ app/controllers/application_controller.rb:2:in `block in <class:ApplicationController>'
  TRANSACTION (0.1ms)  BEGIN
  ↳ app/controllers/tweets_controller.rb:11:in `create'
  Tweet Create (0.5ms)  INSERT INTO "tweets" ("user_id", "body", "created_at", "updated_at", "likes_count", "retweets_count") VALUES ($1, $2, $3, $4, $5, $6) RETURNING "id"  [["user_id", 1], ["body", "GSd"], ["created_at", "2023-04-13 21:29:24.980899"], ["updated_at", "2023-04-13 21:29:24.980899"], ["likes_count", 0], ["retweets_count", 0]]
  ↳ app/controllers/tweets_controller.rb:11:in `create'
  TRANSACTION (0.6ms)  COMMIT
  ↳ app/controllers/tweets_controller.rb:11:in `create'
  Rendering tweets/create.turbo_stream.erb
  Rendered tweets/create.turbo_stream.erb (Duration: 27.5ms | Allocations: 1184)
Completed 500 Internal Server Error in 40ms (ActiveRecord: 1.5ms | Allocations: 5382)

  
ActionView::Template::Error (wrong number of arguments (given 1, expected 0; required keywords: tweet, current_user)):
    1: <%= turbo_stream.prepend "tweets" do %>
    2:   <%= render partial: "tweets/tweet", locals: { tweet_presenter: TweetPresenter.new(@tweet) } %>
    3: <% end %>
  
app/presenters/tweet_presenter.rb:5:in `initialize'
app/views/tweets/create.turbo_stream.erb:2:in `new'
app/views/tweets/create.turbo_stream.erb:2
app/views/tweets/create.turbo_stream.erb:1

有什么办法可以补救吗?
非常感谢!

twh00eeo

twh00eeo1#

这看起来像是一个参数传递问题。
您的TweetPresenter需要2个keyword参数,并且都是必需的:

def initialize(tweet:, current_user:)

但是您初始化演示者时没有使用关键字arg:

tweet_presenter: TweetPresenter.new(@tweet)

这就是为什么你会看到错误:ActionView::Template::Error (wrong number of arguments (given 1, expected 0; required keywords: tweet, current_user)):(注意该消息中的“required keywords”。)。这反过来会导致周围视图失败。
尝试将该部分更改为如下内容:

tweet_presenter: TweetPresenter.new(tweet: @tweet, current_user: current_user)

另外,我认为您的TweetPresenter中可能存在次要错误。

def initialize(tweet:, current_user:)
    @tweet = tweet
    @current_user = user
  end

第3行尝试将user赋值给示例变量@current_user,但此方法不接受user,而是接受current_user关键字参数。它应该是:@current_user = current_user

相关问题