I have a table user_followers that has: user_id and follower_id
I'm trying to save a new record where the logged in user follows another user via AJAX.
this is the button:
<%= link_to "Add friend", teste_user_follower_path(:params => #friend.id), remote: true, class: 'btn' %>
And the action teste where I try to save:
def teste
if params[:query]
#friend = User.where(id: params[:query]).first
#user_follower = current_user.user_followers.new(follower: #friend)
else
flash[:error] = "Friend required"
end
end
I have the associantions set up correctly I think
If you're creating a record I assume that you've set up your teste route as POST. If this is the case you'll have to add method: :post to your link_to
<%= link_to "Add friend", teste_user_follower_path(:params => #friend.id), method: :post, remote: true, class: 'btn' %>
It also doesn't look like you're actually saving the record anywhere. You're calling new but never calling save. If you want to do both at once you can use create
def teste
if params[:query]
#friend = User.where(id: params[:query]).first
#user_follower = current_user.user_followers.create(follower: #friend)
else
flash[:error] = "Friend required"
end
end
You are not actually creating anything in your code.
Here is the documentation about associations: http://guides.rubyonrails.org/association_basics.html#detailed-association-reference
You should do something like:
#user_follower = current_user.user_followers.create(follower: #friend)
Related
I'm working on a RoR backend for a big mobile app, currently with the admin panel.
I have two models: Activity and Deal, joined by HMT ActivitiesDeal. The join is tested both ways in rails console and works like a charm.
Activity is the model the app is built around, so admins need to be able to add deals to activity from the "Edit activity" form in some intuitive way.
I tried this for creating activities_deal:
<%=select("deal", #deal_id, Deal.all.collect {|d| [d.title, d.id]}, {})%>
<%= link_to "Add", link_activity_deal_path(activity_id: #activity.id, deal_id: #deal_id), method:'post' %>
But it doesn't work as I thought. Any ideas on how to send the correct deal_id to link_activity_deal_path? This seems like a problem that has been solved many times, but I can' find anything that fits.
ActivitiesDealsController:
class ActivitiesDealsController < ApplicationController
def create
#activity = Activity.find(params[:activity_id])
render file: 'public/404.html' and return unless #activity && Deal.find(params[:deal_id])
#activity_deal = ActivitiesDeal.new
#activity_deal.activity_id = params[:activity_id]
#activity_deal.deal_id = params[:deal_id]
if #activity_deal.save
redirect_to proc {activity_url #activity}
end
render file: 'public/500.html'
end
def destroy
p params
#activity = Activity.find(params[:activity_id])
render file: 'public/404.html' and return unless #activity
#activity_deal = ActivitiesDeal.where("activity_id == ? AND deal_id == ?", params[:activity_id], params[:deal_id])
render file: 'public/404.html' and return unless #activity_deal
ActivitiesDeal.destroy(#activity_deal)
redirect_to proc {activity_url #activity}
end
end
Fixed the problem by making a form_for outside of the edit page.
If anyone needs the code:
<%= form_for #activity, as: :deal, :url => link_activity_deal_path(activity_id: #activity.id), method:'post' do |f|%>
<%= f.collection_select :id, #deals, :id, :title %>
<%= f.submit "Add Deal", class: "btn btn-primary" %>
I need to create new Event from User's guest page and the Event.visitor_id should be User.id
Event.rb
def create
#event = current_user.owner_events.new(event_params)
end
protected
def event_params
params.require(:event).permit(:visitor_id)
end
I need to correct the view below which is not working:
<%= link_to "Create event with this user", events_path(visitor_id: #user.id), method: :post %>
I get: ActionController::ParameterMissing in EventsController#create
Try that (you forgot to add event key):
<%= link_to "Create event with this user", events_path(event: { visitor_id: #user.id }), method: :post %>
For more details read 'strong parameters' gem documentation.
I just had to add .to_i to #user.id
<%= link_to "Create event", events_path(:event =>{:visitor_id => #user.id.to_i} ), :method => :post %>
I have a button that i need disabling if the data it was going to submit already exists in the database.
Bare in mind ive only been using this for like a month. so im a super noob!! haha!
The button is this <%= link_to 'Table', admin_table_statuses_path(table_id: #table.id), class: "btn btn-primary", method: :post, remote: true%>
my controller for this button is pretty basic also. see here
def create
table_id = params[:table_id] #Keep the same
#table_status = tableStatus.new
#table_status.table_id = params[:table_id]
#table_status.invoiced_out_user_id = current_user.id
#table_status.invoiced_out_datetime = DateTime.now
if #table_status.save
# Success
flash[:notice] = "Done!"
else
flash[:notice] = "Fail"
end
Give an ID to the button:
<%= link_to 'Table', admin_table_statuses_path(table_id: #table.id), class: "btn btn-primary", id: 'create_button',method: :post, remote: true%>
Inside create method do:
def create
.
.
.
render :update do |page|
if #table_status.save
page << "document.getElementById('create_button').setAttribute('href', '')"
end
end
end
Since, the element is a link not button, you can just remove it's href, so that it doesn't hit create method again.
My form is creating a new record but I want it to check if the user_id exists first and send to update if it does.
My form in my edit web page:
<%= form_for #trained, :url => certificates_path, :method => :post do |f| %>
<p> Non-Trained Users </p>
<%= select_tag "certificate[user_id]", options_for_select(#non_trained.collect{|x| [x.name, x.id]}), {:multiple => :multiple} %>
<%= f.submit "Train", class: "btn btn-large btn-primary" %>
<% end %>
My edit() in the controller, it seems to ignore my if statement and errors if it can't find the record. How do I check for the record existing first and send to update() if it does?
if #trained = Certificate.find(params[:certificate])
else
#trained = Certificate.new(params[:certificate])
end
This is my resource controller that is actually doing the work.
class CertificatesController < ApplicationController
def create
#trained = Certificate.new(params[:certificate])
if #trained.save
#trained.update_attributes(attend: "Yes")
end
redirect_to grandstreamers_resellers_path
end
def update
#trained = Certificate.where(user_id: params[:certificate][:user_id])
#trained.first.update_attributes(attend: "No")
#untrained = Certificate.where(user_id: params[:certificate][:user_id])
#untrained.first.update_attributes(attend: "No")
redirect_to grandstreamers_resellers_path
end
end
I generally tend to find code cleaner if variable assignment is not performed as part of a conditional statement. However, if you want to avoid a second assignment after the initial find you can do it as follows.
I'd suggest changing these lines:
if #trained = Certificate.find(params[:certificate])
else
#trained = Certificate.new(params[:certificate])
end
To something like this: (note the change in params ... you should be finding the certificate using an ID or some other attribute, not the entire params hash)
#trained = Certificate.find_by_id(params[:id]) || Certificate.new(params[:certificate])
Of course, you could also use the find_or_create_by method ...
So I think this is simple but I haven't figured it out yet.
I have a super simple form_for:
<%= form_for #redemption do |f| %>
<%= f.text_field :code %>
<%= f.submit 'Submit' %>
<% end %>
a User enters a code and that is sent to the redemptions#create action
def create
#code = Code.find_by_id('6')
#redemption = #code.redemptions.find_or_create_by_code_id_and_user_id(#code.id, current_user.id)
if #redemption.save
redirect_to bands_url, :notice => "Redemption Successful."
else
redirect_to bands_url, :notice => "Could not redeem code"
end
end
this code works but not at all for production (obviously) due to hard coding an ID in there.
The piece I don't get is: How to send a Code.code through the form and somehow get that code's.id. I know how to pass it into a Redemption...
ideas?
Your form will pass params. You mention "How to send a Code.code" does that mean your redemption[:code] is the same as this Code.code where you get the Code.id from? (the 6 in your example).
If it's the same use something like
def create
#code = Code.find_by_code(params[:redemption][:code]) # just to show you how to use it
#redemption = #code.redemptions.find_or_create_by_user_id(current_user.id) # that part looked redundant but i don't know what you're trying to do.
if #redemption.save
redirect_to bands_url, :notice => "Redemption Successful."
else
redirect_to bands_url, :notice => "Could not redeem code"
end
end