简体   繁体   中英

Ruby on Rails: Create record from link

how can I issue a create command from a link? I have an if/else statement that displays edit or create, but I haven't found the right way to create the record.

I had this, but then I have to refresh the page to get it to function, which I can't have:

  <% if FollowUp3Week.where(subject_id: sub.subject_id).first != nil %>
    <%= link_to "edit", follow_up3_week_path([FollowUp3Week.where(subject_id: sub.subject_id).first]) %>
  <% else %>
    <%= FollowUp3Week.create(subject_id: sub.subject_id) %>
  <% end %>

And I'm trying this, but no luck so far (where subjects_path is a link to the current page):

  <% if Baseline.where(subject_id: sub.subject_id).first != nil %> 
    <%= link_to "edit", baseline_path([Baseline.where(subject_id: sub.subject_id).first]) %>              
  <% else %>
    <%= link_to "create", subjects_path(Baseline.create(subject_id: sub.subject_id)) %> 
  <% end %>

Any tips, or references I should read through, would be greatly appreciated.

I really just want the link 'create' to generate the working 'edit' link.

Thank you for your time.

您只能在调用控制器时创建一条记录,因此您要做的是,一旦用户单击将触发链接的链接,就使link_只需将URL呈现给具有create动作的控制器即可。给您的控制器的请求,该请求将运行创建记录。

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