I need to modify the issue's start_date and due_date some how,
But I haven't used Rails before, so I don't know how the it run in server,
And I need to implement it in a short time.
So I add these code in the controller,
def date
issue = Issue.find(params[:id])
issue.start_date = params[:start_date]
issue.due_date = params[:due_date]
ntc_str = "Fail to save!" + params[:id]
if issue.save
ntc_str = 'Issue saved!'
end
flash[:notice] = ntc_str;
redirect_to :controller => 'gantts', :action => 'show', :project_id => params[:p_id]
end
It runs when I access it by myself
It always failed and the "ntc_str" always is "Fail to save!" if I use javascript to access it.
For example:
It runs when I input the url "http://xxx.xxx.xxx.xxx/date?id=6&project_id=test&start_date=2016-06-08&due_date=2016-06-30" by hands,
But it failed when I use javascript "window.location.href='/date?id=6&project_id=test&start_date=2016-06-08&due_date=2016-06-30'"
It runs when I input the params in the form I create and click to submit it,
But it failed when I use javascript "document.getElementById('start_date').value = '2016-06-30'; /..../ $('#test-form').submit()"
Could you tell me why it always fails and how can I use the issue model? I have be crazy now.
It would be useful, if you provide some logs with each cases you try.
Also, you can see what goes wrong with issue, when you try to save it, with:
if issue.save
ntc_str = 'Issue saved!'
else
Rails.logger.error(issue.errors.full_messages)
end
Related
I have a create method in Rails where I am trying to create multiple objects in a while loop. For some reason it doesn't seem to be hitting the while loop so no objects are being created. The code is below:
def create
#user = User.find(params[:participant][:user_id])
#activity = Activity.find(params[:activity_id])
weeks = #activity.weeks
i = 1
while i <= weeks do
puts "Test"
participant = Participant.new
participant.user_id = #user.id
participant.activity_id = #activity.id
participant.attended = false
participant.paid = false
participant.week = i
participant.save
i = i+1
end
redirect_to user_activities_path(#user, :id => #activity.id)
end
The form I am using to submit is working fine as I can see from the console, and the redirect_to method at the end is working, so it just seems to be missing the loop. If it helps, the value of weeks is 10. Any help would be greatly appreciated.
If multiple Test have been output, try participant.save!, i think the participant save might fail, like some column not valid, so no objects are being created.
Please check if activity record is get fetched. I think your 3rd statement should be as follow.
#activity = Activity.find(params[:participant][:activity_id])
I'm trying to delete a record by passing id of that record. The code looks like this:
def destroy_catalogue_entry
#catalogue_entry = CatalogueEntry.find(params[:catalogue_entry_id])
if #catalogue_entry.destroy
flash[:success] = 'Catalogue entry deleted successfully.'
else
flash[:error] = 'Failed...'
end
end
I'm getting an interesting error. When my function destroy_catalogue_entry is called it shows:
Couldn't find CatalogueEntry with 'id'=16
but as I comment If condition section and render #catalogue_entry as json, the output is printed successfully. So how is it possible? Am I making some silly mistake or is there logical reason. Please enlighten me.
Solved! All I did is this:
def destroy_catalogue_entry
#catalogue_entry = CatalogueEntry.find(params[:catalogue_entry_id])
if #catalogue_entry.destroy
flash[:success] = 'Catalogue entry deleted Successfully'
redirect_to action: :view_catalogue_entries, dc_id: #catalogue_entry.dc_id
else
flash[:success] = 'Failed...'
end
end
When I notice the console, the record was getting deleted successfully but after that there was a SELECT query for the same record, that is why it was throwing the error Couldn't find CatalogueEntry with 'id'=16. As I redirected it, the problem was solved.
I think destroy method is returning an object. In ruby anything other than false or null will be taken to true in if statement. You can do puts on destroy method and see what its returning.
i presume your,
#catalogue_entry = CatalogueEntry.find(params[:catalogue_entry_id])
is returning that error because it cant find CatalogueEntry with id 6, make sure you have CatalogueEntry with that id.
I'm trying to add the user id to 2 columns in the db after a user successfully submits a form. The commands run fine in the console but wont work in the controller. I haven't added info to the db straight from the controller before so I'm thinking that I'm doing something wrong.
Here is the create method
def create
#game = Game.friendly.find(params[:game_id])
#game_category = Game.friendly.find(#game.id).game_categories.new(game_category_params)
if current_user.mod_of_game? params[:game_id] && #game_category.save
Game.find(#game.id).game_categories.find(game_category.id).update(submitted_by: current_user.id)
Game.find(#game.id).game_categories.find(game_category.id).update(approved_by: current_user.id)
flash[:info] = "Game category added succesfully!"
redirect_to #game
else
render 'new'
end
end
These 2 lines are supposed to add the user id to the submitted_by and approved_by columns but they don't. I don't get any error messages, they simply just don't add anything to those columns
Game.find(#game.id).game_categories.find(game_category.id).update(submitted_by: current_user.id)
Game.find(#game.id).game_categories.find(game_category.id).update(approved_by: current_user.id)
If I replace the lines with coding that works in the console to see if its a variable or something thats not right it still doesn't work
Game.find(12).game_categories.find(55).update(submitted_by: 1)
Game.find(12).game_categories.find(55).update(approved_by: 1)
I'm building an app to learn rails and I guess this is something I just don't know.
Can anyone enlighten me on what I'm doing wrong?
Update:
Ok it is now giving me an error - Couldn't find GameCategory without an ID
So the #game_category.id isn't working?
It is a small typo in your query you missed #.
Game.find(#game.id).game_categories.find(#game_category.id).update(submitted_by: current_user.id)
After playing around tweaking it here and there it turns out to be this line
if current_user.mod_of_game? params[:game_id] && #game_category.save
when changed to this it works
if #game_category.save && current_user.mod_of_game? #game
When I load my action CarsController#show in browser, I get this error message:
Timeout::Error in CarsController#show
execution expired
And the error pointing out on this line:
country = GeoIp.geolocation(ip, :precision => :country)
The whole action:
def show
#car = Item.find_by_car_key(params[:car_key])
ip = request.remote_ip
geo_key = 'my geo key'
GeoIp.api_key = geo_key
country = GeoIp.geolocation(ip, :precision => :country)
puts country.inspect
respond_to do |format|
format.html # show.html.erb
format.json { render json: #item }
end
end
How can I avoid this error message and use it always when this action will be loaded?
Thank you
Assuming you are using the GeoIP gem found here
The error is related with the time out, maybe geoip gem is taking to much time to fetch the ip info, so after your Api Key set GeoIp timeout
According to the gem readme,
It is possible to set a timeout for all requests. By default it is one second, but you can easily set a different value. Just like you would set the api_key you can set the timeout:
Try
GeoIp.timeout = 5 # In order to set it to five seconds
Update
I will recommend you another gem, because geoip is nor uptodate, take a look at Geocoder
https://github.com/alexreisner/geocoder, it can take the ip address from the request automatically like
request.location.country
request.location.city
I would recommend using the geocoder gem in combination with maxmind or freegeoip
works well in my production app (freegeoip sometimes detects the wrong country, but its free)
I'm trying to figure out an inconsistency between what's happening in a functional test and what is happening in my development environment. I have a custom validation method unique_entry that is essentially a specialized version of validates_uniqueness_of. It looks like this:
def unique_entry
matched_entry = Entry.first(:conditions => ['LOWER(field_one) = LOWER(?) AND LOWER(field_two) = LOWER(?)', self.field_one, self.field_two])
errors.add_to_base('Duplicate detected') if matched_entry && (matched_entry.id != self.id)
end
The update action in the controller is very basic:
def update
if #entry.update_attributes(params[:entry])
flash.now[:success] = 'Success'
render :action => 'show'
else
flash.now[:error] = 'Error'
render :action => 'edit'
end
end
This works just fine when I'm creating a new record. When I update a record, however, I get inconsistent behavior. If I test it from a browser in my development environment, it correctly renders the edit action with an error message, but in my functional test, it accepts the update as successful. Here is the test:
test "should not update entry and should render edit view if invalid update" do
put :update, { :id => 1, :field_one => 'new_value', :field_two => 'new_value' } # 'new values' are the same as another existing record to trigger the duplication check
assert_template :edit
assert_not_nil flash[:error]
end
I looked at the test log and discovered that the values unique_entry is using are the record's original values instead of the values it should be attempting to update with. That is, the first line of unique_entry generates an SQL query like this:
SELECT * FROM "entries" WHERE (LOWER(field_one) = LOWER('original_value_of_field_one') AND LOWER(field_two) = LOWER('original_value_of_field_two')) LIMIT 1
What am I missing here? Why do my validations seem to be running against the original record instead of the new values only in the test environment?
In your test, shouldn't there be some reference to :entry, since that is what you are looking for in the controller params[:entry] ?