Home>

I'm new to rails.

I created a new post screen and an edit screen with a partial template, but I want to change the characters displayed by the submit button depending on the file. Is it possible to display Create Book when posting a new post and Update Book when editing?
I want to specify the place to write the code in the if statement, but it doesn't work. What should I do?

<% = form_with model: book, local: true do | f |%>
  
  <% = f.label: title%>
  <% = f.text_field: title%>
  
  
  <% = f.label: body%>
  <% = f.text_area: body%>
  
  
  <% = f.submit "Create Book"%>
  
<% end%>


* Since it is a partial template, the @ mark of the instance variable is deleted.

  • Answer # 1

    I remember a little, but I think you can do it below.

    <% if f.new_record?%>
    <% = f.submit "Create Book"%>
    <% else%>
    <% = f.submit "Update Book"%>
    <% end%>

    If the above doesn't work, you can do so by passing the new or edit flag when calling partial.

    Postscript

    I'm sorry, I made a mistake.

    f.new_record?


    not

    f.object.new_record?


    I think.
    I remember getting the specified model object in the form_with block.
    If you can not refer to it even above, it may be better to judge from the book object obediently.

    if book.new_record?


    like. .. ..
    I've only been developing in API mode these days, so I've forgotten a lot ...