How to Return a Message from the Controller Without Changing the Display of the Rails Screen

Asked 2 years ago, Updated 2 years ago, 46 views

What should I do if Rails returns a message from the controller without changing the display?

We have created a screen with the following specifications:

  • Display a list of items
  • Select the radio button in the list and use the "Next" button to transition to the detail screen
  • Display a message on the list screen if there are exceptions to the transition preprocessing

=>When displaying messages, the contents of the list screen should be in the state before pressing the Next button

Currently, the code is as follows. I am using Rails 4.1.5.

#controller
  def index
    @items=items
  end

  def show
    @select_no=params[:select_no]
    # transition pretreatment
  rescue HogeError=>e
    flash.now.alert="Error Occurred"
    render —index
  end

  definitions
    # Items creation process; non-ActiveRecord.
  end


# view index.erb
<%=render "error_message_area" %>
<%[email protected] event?%>
  <%=form_tag(item_path, method::get)do%>
    <table class="table" id="item_select">
      <thead>
        <tr>
          <th>Select </th>
          <th>No>/th>
          <th>Name</th>
        </tr>
      </thead>
      <tbody>
        <%@items.each_with_index do|item,index|%>
          <tr>
            <td class="ctrl">%=radio_button_tag("select_no", item[:no], index==0)%>/td>
            <td class="no">%=item[:no]%>/td>
            <td class="name">%=item[:name]%>/td>
          </tr>
        <%end%>
      </tbody>
    </table>
    <div>
      <%=button_tag type:"submit" do%>
        next
      <%end%>
    </div>
  <%end%>
<%end%>

Currently, if an error occurs, the list of items will disappear.

If you use @select_no to select the radio button, you can use

to get a list of items.
  • A. Send a list of items to #show
  • B.Re-create the item list with #show (call #items)
  • C.Ajax manages to update only messages

I came up with the idea that none of them fit well. Proposition B is dynamic in the list and may differ from before pressing the button.

Is there any good way?

ruby ruby-on-rails

2022-09-30 20:33

3 Answers

I don't have enough credit, so I'll give you a different answer.
If AJAX handles it, there is also a way called RJS.


button in pressed events without submitting Disable (or disable) all items in the form
Skip the form.serialized data and
If the processing results are normal, you can return the code to submit.
Should I display a message and enable it if it fails?


2022-09-30 20:33

It's a generalization because it's impossible to share the sensuous story of "it doesn't fit well" with this much information.

Regarding the solution that the content should not be changed due to the screen transition,

either of the .

2. You can either use Ajax in C. or open a separate window in window.open, and you can use window.opener to handle the original window.


2022-09-30 20:33

Since you did not set @items in the show action, the <%[email protected] event?%> decision is false and the display is empty.


2022-09-30 20:33

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.