开发者

NoMethodError in one of my models #show actions

I've got the following code in my show.html.erb file:

<p>
 <b>Status:</b>

 <% if @server.serverUp?.to_s == "Up" %>
  <% @server.update_attribute(@server.serverStatus, 'Up') %>
      <span style="color: green;"> <%= @server.serverUp? %></span>
      <% else %>
        <span style="color: #ff0000;"> <%= @server.serverUp? %></span>
      <% end %>
</p>

My main concerns is with the line <% @server.update_attribute(@server.serverStatus, 'Up') %> which breaks my app from working. This line is supposed to save the status of the Server, whether it is "Up" or "Down", and save it in 'servers' table under the 'serverStatus' column.

However, when I go to http://localhost:3000/servers/id_of_server, where 'id_of_server' is a number from 1-300, the following error message appears:

NoMethodError in Servers#show

Showing C:/SIS/app/views/servers/show.html.erb where line #18 raised:

undefined method `=' for #<Server:0x60b9358>
Extracted source (around line #18):

15: <p>
16:   <b>Status:</b>
17:      <% if @server.serverUp?.to_s == "Up" %>
18:       <% @server.update_attribute(@server.serverStatus, 'Up') %>
19:           <span style="color: green;"> <%= @server.serverUp? %></span>
20:           <% else %>
21:             <span style="color: #ff0000;">开发者_如何学编程 <%= @server.serverUp? %></span>

I don't seem to understand the error message, as I haven't used an equal sign in the extract of code provided. Any help would be appreciated.

If you guys need any more info let me know.

Thanks in advance

Edit: While I'm at it, I just wanted to ask whether I'm not following the RoR conventions by putting that type of code into the show.html.erb file as opposed to somewhere else? Because in the same file I also have another algorithm which reads all the attributes of a model, puts it into an array, and displays only the unique values.

Thanks


You should write:

<% @server.update_attribute(:serverStatus, 'Up') %>

And yes: you should not update models in view. In your case it could cleanly live in a before_save or why not in an after_initialize callback in the model I guess.

Lastly: in Ruby, convention is snake case so server_status instead of serverStatus

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜