Rails通过循环正确传递ID

时间:2018-11-09 22:50:49

标签: ruby-on-rails ruby

尝试将id从我的rails前端传递到后端。我的Rails控制器中有以下代码:

/** @return {@link CompletableFuture} which when cancelled will interrupt the supplier
 */
public static <T> CompletableFuture<T> supplyAsyncInterruptibly(Supplier<T> supplier, Executor executor) {
    return produceInterruptibleCompletableFuture((s) -> CompletableFuture.supplyAsync(s, executor), supplier);
}

// in case we want to do the same for similar methods later
private static <T> CompletableFuture<T> produceInterruptibleCompletableFuture(
        Function<Supplier<T>,CompletableFuture<T>> completableFutureAsyncSupplier, Supplier<T> action) {
    FutureTask<T> task = new FutureTask<>(action::get);
    return addCancellationAction(completableFutureAsyncSupplier.apply(asSupplier(task)), () ->
            task.cancel(true));
}

/** Ensures the specified action is executed if the given {@link CompletableFuture} is cancelled.
 */
public static <T> CompletableFuture<T> addCancellationAction(CompletableFuture<T> completableFuture,
                                                             @NonNull Runnable onCancellationAction) {
    completableFuture.whenComplete((result, throwable) -> {
        if (completableFuture.isCancelled()) {
            onCancellationAction.run();
        }
    });
    return completableFuture;  // return original CompletableFuture
}

/** @return {@link Supplier} wrapper for the given {@link RunnableFuture} which calls {@link RunnableFuture#run()}
 *          followed by {@link RunnableFuture#get()}.
 */
public static <T> Supplier<T> asSupplier(RunnableFuture<T> futureTask) throws CompletionException {
    return () -> {
        try {
            futureTask.run();
            try {
                return futureTask.get();
            } catch (ExecutionException e) {  // unwrap ExecutionExceptions
                final Throwable cause = e.getCause();
                throw (cause != null) ? cause : e;
            }
        } catch (CompletionException e) {
            throw e;
        } catch (Throwable t) {
            throw new CompletionException(t);
        }
    };
}

我的名册的Rails视图具有以下代码:

def count_vote
roster_id = params[:roster_id]
roster = Roster.find(roster_id)
newvote = roster.vote + 1
if roster.update({vote: newvote})
  redirect_to rosters_path
end
end

我正在尝试使用隐藏字段标签传递ID,但在Rails服务器日志中出现以下错误:

<% @rosters.each do |roster| %>
  <div class='each'>
    <%= image_tag(roster['image_url'], class: 'image') %>
    <%= hidden_field_tag(:roster_id, @roster.id) %>
    <p class='name'> <%= roster['name'] %> </p>
    <p class='title'> <%= roster['title'] %> </p>
    <p> <%= roster['bio'] %> </p>
    <p> <b> Want to work with <%= roster['name'] %>? </b> <%= link_to image_tag('thumbs-up.svg', class: 'thumbsup'), rosters_path, method: :patch %>
    <br>
    <%= roster['vote'] %> People have said Yes! </p>
    <br>
  </div>
<% end %>

在错误控制台中搜索Roster.all会返回以下内容,为简便起见,我将仅返回前两个:

Started PATCH "/rosters" for 127.0.0.1 at 2018-11-09 17:43:56 -0500


(0.2ms)  SELECT "schema_migrations"."version" FROM "schema_migrations" ORDER BY "schema_migrations"."version" ASC
  ↳ /Users/sohel/.rbenv/versions/2.5.1/lib/ruby/gems/2.5.0/gems/activerecord-5.2.1/lib/active_record/log_subscriber.rb:98
Processing by RostersController#count_vote as HTML
  Parameters: {"authenticity_token"=>"DJAtI8yNTlP1kQK/g/6o9jbdqxWEBBgKinzLtf8v8WrDnYCmrH+HQI5wTEjJ0T6rkKbBz0KA/q2M0yirluozQg=="}
Completed 404 Not Found in 14ms (ActiveRecord: 0.6ms)



ActiveRecord::RecordNotFound (Couldn't find Roster without an ID):

app/controllers/rosters_controller.rb:22:in `count_vote'

1 个答案:

答案 0 :(得分:4)

hidden_field_tag(:roster_id, @roster.id)

应该是

hidden_field_tag(:roster_id, roster.id)

因为没有定义的实例变量@roster

也请尝试将此行更改为

<p> <b> Want to work with <%= roster['name'] %>? </b> <%= link_to image_tag('thumbs-up.svg', class: 'thumbsup'), rosters_path(roster), method: :patch %