Ruby on Rails: Create record from link - ruby-on-rails

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.

You will only be able to create a record when you call a controller, so what you want to do in your case is make the link_to just render the url to the controller that have the create action, once the user clicks the link that will trigger a request to your controller which will run the create record.

Related

Can I query against the DEVISE current_user helper in my application menu?

The problem is, In the menu of my app I want to check if the current user has a book. If they do I will show a link to the edit book path, if not, I will show a link to the create book path.
<% if current_user.book? %>
<% else %>
<% end %>
Yes, you should be able to access current_user from any controller. But always make sure you handle if current_user returns nil.
You can use a try or safe navigation.
<% if current_user.try(:book?) %>
<% else %>
<% end %>
Or
<% if current_user&.book? %>
<% else %>
<% end %>

Redirect to new view with submit_tag in Rails

I have the following submit_tag button
<%= submit_tag("Save Email".upcase, name:"email_change","data-target":"email_change.submit") %>
I am trying to have it redirect to a new view, similar to what is done with using link_to. Is there a way to do something similar using the submit button?
You can do:
<% link_to some_path do %>
<%= submit_tag("Save Email".upcase, name:"email_change","data-target":"email_change.submit") %>
<% end %>
The 'Rails' way to do this would probably be to wrap the redirect in some conditional logic in your controller action, especially if you're submitting data from a form

Can't get if and else statement to work

I've been playing around with my if and else statement but it's always just providing one.
First try
<% if #albumable == #user %>
<%= link_to "Edit", edit_community_album_path(#albumable, album), class: "album_edit" %>
<% else %>
<%= link_to "Ediiiit", edit_user_album_path(#albumable, album), class: "album_edit" %>
<% end %>
Second try
<% if #community == #community_id %>
Albumable will either be a user_id or community_id. When I play around with the code I only get 1 result from the two for both the user album edit page and the community album edit page. I don't understand how I'll be able to making an if statement if it isn't within the community page, it should produce the else statement. All help is appreciated, thank you.
I would suggest refactoring this code to remove the if/else statement altogether. For example, the following provides the exact same functionality:
<%= link_to "Edit", [:edit, #albumable, album], class: "album_edit" %>
You can read more about this at http://guides.rubyonrails.org/routing.html#creating-paths-and-urls-from-objects
Compare the id values instead. Depending on how you loaded those variables with records, Ruby might not think they're equal.

Creating a formula in a Rails view?

I would like to create a small formula in my view/home page. Basically it should say if current user signed in (got that part right) AND USER CREATED A REGISTRY = TRUE then this
or that.
Here is the code below:
<% if user_signed_in? and?????? %>
<%= link_to "Show My Registry", current_user.registry %>
<% else %>
<%= link_to "Create a new registry", new_registry_path %>
<% end %>
Thanks for any help.
Assuming Registry is a model that belongs to a User, you could use <% if user_signed_in? && current_user.registry.exists? %>
I assume you all ready have the relationship between Users and Registries established. If so your if so you could create a method in your users controller that looks something like.
def user_registry
User.current.registry
end
Then if your view your if statement should look something like
<% if user_signed_in? and user_registry? %>
<%= link_to "Show My Registry", current_user.registry %>
<% else %>
<%= link_to "Create a new registry", new_registry_path %>
<% end %>
You might want to nest the two decisions. What happens if the user is not signed in? Do you still want the user be able to create a registry without signing in? Other option would be to consider using a before_filter to ensure user is always signed in before she gets to this decision point in your app.

What's the cleanest way to add a class attribute to an html element in a view in rails

I'm writing some Rails code for a partial view, and I want it to only show a comment field if somebody is already logged onto a site here.
If the page is viewed by someone who isn't a member of the site yet, the shared/comment_not_logged_in fragment should be passed in.
However, I'm totally stumped as to why I can't run the same check to decide if the page should add the class attribute "missing_your_voice" to the enclosing div element here:
<li class="user_submission_form bubble comment_form <% "missing_your_voice" if not current_user %>">
<% if current_user %>
<%= image_tag(current_user.avatar(:comment), :class => "profile_pic") %>
<% form_for [parent, Comment.new] do |f| %>
<%= render "comments/form", :f => f %>
<% end %>
<% else %>
<%= render :partial => 'shared/comment_not_logged_in' %>
<% end %>
</li>
The same idiom, "missing_your_voice" if not current_user returns the string in irb, and also in the console debugger.
What am I doing wrong here?
You forgot an =. Replace <% by <%=, so that you get:
<%= "missing_your_voice" if not current_user %>
Remember that <% ... %> will only run Ruby code, but not display anything. Using <%= ... %> will run the code and display the result of the expression.
As molf already pointed out, there's a missing = on your view.
It should be <%=.
Other than that, be sure to make your controller method available to your view by calling helper_method in your controller.
Take a look on the documentation if needed.

Resources