if current_user.id = #game.user_id - ruby-on-rails

I have links to the show pages for each game in my project and if the
games user_id matches the id of the currently signed in user then I want
it to display the edit button if they are not then it shouldn't display.
I currently have the following code set but it doesn't work. Every game
has the edit button display. The code is as followed:
<% if current_user.id = #game.user_id %>
<div id="text3"><%= link_to 'Edit', edit_game_path(#game) %></div><br />
<% end %>
Any ideas?

MrDanA's answer is most probably the error, but you may want to make this code better. Checking like that is not the Rails way of doing it. Instead, make a User instance method like :
def has_game?(game)
self.games.exists?(:id => game.id)
end
and then in your view :
<% if current_user.has_game?(#game) %> ...
(can even be better by further delegating exists into the game model, as a scope or so, if you like)

You want ==
So:
<% if current_user.id == #game.user_id %>

Related

Ruby on Rails: Record Creation from view error

I have an isolated issue.
I have a table that populates from several different models, it creates links to follow to each respective view.
The code that I have made for each link should be the same, but for some reason, the link isn't showing up under 'Baseline'. I've checked the :create methods for each model, and they mimic each other, and the code from the view is also just a copy - so I'm at a loss as to where to look next. I'm sure that the problem is that the create method is failing, but I don't know where/how.
Here is the code from my view (I'm also pasting the code from FollowUp3Week, because it works):
<% if Baseline.where(subject_id: sub.subject_id).first != nil %>
<%= link_to "edit", baseline_path([Baseline.where(subject_id: sub.subject_id).first]) %>
<% else %>
<%= Baseline.create(subject_id: sub.subject_id) %> #I left the equal for the screenshot.
<% end %>
</td>
<td>
<% 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 %>
</td>
And here is the create method from baselines_controller.rb
def create
#baseline = Baseline.new(params[:baseline])
if #baseline.save
flash[:success] = "Baseline added from create method"
redirect_to baselines_url
else
render 'new'
end
end
I'm also attaching an image of what it looks like. If I remove the equal sign from <%=, the cell will be blank.
EDIT. I'm in the process of removing all of my database queries from the view. Thank you for your comments.
You should really get that Baseline.where out of your view and into the model. AR scopes from the view is a serious no-no in Rails.
In your baseline mode you could do something like:
def empty_subject(subject_id)
where(subject_id: subject_id).first != nil
end
Also, it looks like you're passing arrays into baseline_path and follow_up3_week_path.
Ditch the square brackets.
on Baseline model, put this
def display_name
"#{name}" #whatever you like to show including link
end

Rails if statement for current user profile view and other users

Hi I'm having some trouble getting different views to display based on whether or not the user being viewed is the logged in user.
I'm using Rails 3.2 and Devise
I've tried the code below but it always returns the template for the logged in user (where I have edit info links) rather than the one for the other users.
<% if user = current_user %>
<%= render 'users/myprofile' %>
<% else %>
<%= render 'users/viewprofile' %>
<% end %>
Thanks very much
The statement should be..
<% if user == current_user %>
The reason being, = is for assignment, == is for comparison.
Check the sign of comparison. You have used equals to sign.
<% if user == current_user %>
= sign assigns the current user to user variable. Hence the condition will always be true unless there is no current user.

Refactoring from nested ifs to link_to_if

I had the following if statement in a shared view:
<% if activity.holder.user.profile_type == "Manager" %>
DAMANAGER
<% elsif profile == "managers" %>
<%= link_to activity.holder.name, managers_enterprise_path(activity.holder) %>
<% else %>
<%= activity.holder.name %>
<% end %>
profile is a local string variable that will be "enterprises" or "managers" depending which profile is logged in!
activity is a local variable too from an #each statement
The view is shared between only two users with differents profiles (Enterprise and Manager), with this constraint:
If the managers is the owner of activity it should display DAMANAGER
When I'm seeing as manager unless the holder is the manager it should display a link to show the enterprise that owns this activity.
When I'm seeing as enterprise it should display, who owns the activity, the holder name. Without links.
So I struggled myself and refactor from that to this, that's what I got by now:
<%= link_to_if (profile == 'managers'), activity.holder.name, managers_enterprise_path(activity.holder) %>
But this doesn't display DAMANAGER and furthermore it shows the enterprise's id equal to manager's id, since it pass the manager to managers_enterprise_path. The question is:
How I can improve it?
Thanks in advance, tell me if you need more code!
I don't see a maintainable way to do it without if ... else ... end. The elsif ... else ... end part can be replaced with a link_to_if:
<% if activity.holder.user.profile_type == "Manager" %>
DAMANAGER
<% else %>
<%= link_to_if profile == "managers", activity.holder.name, managers_enterprise_path(activity.holder) %>
<% end %>

How do I use another value of a key in a hash if the first one doesn't exist, like some type of fallback in ruby/rails?

Basically I have a follow button and when click the page refreshes and I show an unfollow button in place. Below is the code I use to render the particular form needed:
follow_forms partial:
<% unless current_user?(#user) %>
<% if current_user.following?(#user) %>
<%= render 'relationships/partials/unfollow' %>
<% else %>
<%= render 'relationships/partials/follow' %>
<% end %>
<% end %>
Any I changed the form to an ajax form because I don't want the page refresh and on success of the form submission I'd like to replace the follow button/form with an unfollow button/form. This isn't straight forward because only 1 form shows at a time so I can't use my jquery selector to find this form anyway.
What I decided to do was create a new action that renders the follow_form partial this way the appropriate form will be available for me to manipulate with my jquery selector.
The new action:
class RelationshipsController < ApplicationController
def get_follow_form
respond_to do |format|
format.html { render :partial => 'relationships/partials/follow_form_ajax' }
end
end
end
The problem now is that I don't have access to the #user instance variable. That doesn't matter to much because I can get the user who was just followed via the jquery success data then pass that as data in the new ajax call to get_follow_form_url and then pass that info into the partial as a local variable.
I still have an issue with the #user instance variable not being available. Which brings me to my question.
How can I make another value be used if the instance variable isn't nil/doesn't exist?
The form for following:
<%= form_for current_user.relationships.build(:followed_id => #user.id), :remote => true do |f| %>
<%= f.hidden_field :followed_id %>
<%= f.submit "Follow", :class => 'followButton' %>
<% end %>
Can I do something like this
:followed_id => #user.id <-if this doesn't exist use this-> user.id
There are other ways around this like creating new partials that are only used for this whole situation or creating some messy if statements but I feel like creating duplicate forms should be my very very very last option.
I look forward to you solutions thanks
Kind regards
There's a very simple way to do this, assuming you have your 'fallback' ID:
:followed_id => #user.present? ? #user.id : fallback_id
Use something like the andand gem or just try and a logic expression:
:followed_id => #user.andand.id || user.id
Even without that you can use identical logic, and certainly don't need multiple partials:
:followed_id => (#user && #user.id) || user.id
But as Frederick says, if you have a replacement value for the object already, couldn't you just set it?

Testing against the 'current_user' with Devise in Rails 3

I'm just starting out with Ruby and Rails, trying out Devise with Rails 3. I've got a loop around a list of Posts, each of which has an associated user. I only want to display editing controls for those posts which are associated with the current user.
<% #posts.each do |post| %>
<%= link_to "show" %>
<% if current_user = post.user %>
<%= link_to "edit" %>
<% end %>
<% end %>
(The above is simplified, and from memory, so I'm sure the syntax isn't entirely right - but you get the gist.)
If no user is logged in, the posts show as intended - there's a Show link, but no Edit link. However, if I am logged in at all, all of the Edit links show up, even fir posts created by a different user.
I've verified in the console that User.find(1) != User.find(2), but for some reason the current_user = post.user evaluates to true no matter who is currently logged in. Is this to do with current_user being a helper as opposed to a "real" user object? How can I use current_user to get at the ACTUAL current user to make my comparison?
Thanks,
Dan
You're assigning rather than testing - use == - i.e.
<% if current_user == post.user %>

Resources