简体   繁体   中英

What is the right way to print data from my model in Rails?

In my controller, I have:

def show
  @adjuster = Adjuster.find(params[:id])
end

form_for let's me print the data, but I don't want to use it because it is limited in certain respects. I want to use the regular form method.

I tried this:

 <%= form_for (@adjuster) do |f| %>
  <div class="row">
    <div class="large-4 columns">
      <b>Adjuster Name</b></br>
        <%= @adjuster.adjuster_name %>
     </div>
    </div>

I get an error saying:

@adjuster is empty

I just tested out this code (Just copy & paste), it's just completely ok, I don't seem anything wrong, working perfectly. I believe in your code not showing any error, you just make the adjuster_name column has a value, I think the value is empty for adjuster_name .

I just change the column_name such as adjuster_name to my own, and look generated HTML is like this

<form class="edit_adjuster" id="edit_adjuster_20" action="/adjusters/20" accept-charset="UTF-8" method="post">
    <input name="utf8" type="hidden" value="✓">
    <input type="hidden" name="_method" value="patch">
    <input type="hidden" name="authenticity_token" value="q/DPVxBU5gU5Gc1McT3USYRu21P6YBKeTfnwpOILO3xOAxKu25XqWJavy5alRxD1wMSVrtXCvb9l5ttsWzskAw==">
    <div class="row">
        <div class="large-4 columns">
            <b>Adjuster Name</b><br>
            Adjuster Name
        </div>
    </div>
</form>

That means the code is completely ok

<%= form_for(@adjuster) do |f| %> #=> or <%= form_for (@adjuster) do |f| %>
    <div class="row">
        <div class="large-4 columns">
            <b>Adjuster Name</b></br>
            <%= @adjuster.adjuster_name %>
        </div>
    </div>
<% end %>

#=> controller
def show
  @adjuster = Adjuster.find(params[:id])
end

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM